Now that we have verify that Docker is installed and working we can learn more about how to create containers and manage them using the Docker CLI. In this part of the module we will look the some of the top Docker CLI Commands. Each command will have a description and a demo video that you can watch to see how the output should look. The command will be the heading , the description will below and the video will be placed to the left. The videos were all done in macOS, but everything should be exactly the same in Windows. Also, the videos do not have any audio, but I do suggest watching them in full screen.
This command is used to display the current installed version of docker. This can help you determine if you need to update your docker version. Also, you might wish to check your version to see if your current installed version supports new features.
This command is helpful if you wish to determine what version of docker you are using.
This command is used to download docker images from the docker repository (hub.docker.com) Make sure you replace <image> with the container image you wish to download. An example that you can run is:
docker pull ubuntu
This will download the latest image of ubuntu.
This command is helpful when you wish to just download an image.
This command is used to create a container from an image. When using this command we like to do the following:
docker run -it -d <image name>
docker run -it <image name> <command>
What are the extra options on the command line?
-d: This places the container in the background.
-it: This allows us to access to the command prompt
These commands are useful when you wish to run a container from the command line.
This command will show us running containers.
docker ps
I also like to use to show status of all containers (running and exited)
docker ps -a
These commands are helpful when you are trying to determine the status of containers and what you might have running.
Recall that container running in the background? We can use docker exec to access it.
docker exec -it <container id> /bin/bash
You can get the <container id> from using docker ps.
Even though in my example above I provided /bin/bash you can use another command to access it as well.
This command helpful to gain access to a container that is running in the background.
This command does as you would expect. It just stops a container from running. You also need to provide the container id with this command. When using this command it allows the container to gracefully shutdown.
docker stop <container id>
Recall, you can get the <container id> from docker ps.
This command is very helpful when you are trying to stop a container from running.
This is the same as docker stop, but it does not provide the container time to gracefully shutdown. It stops the container execution immediately.
docker kill <containder id>
Recall, you can get the <container id> from docker ps.
If a container is not responding to docker stop you can be a bit more forceful and try this one :-)
This command is used to delete a stopped container. You must provide the container id.
docker rm <container id>
Very useful to help with making sure your container library stays clean.
This command is used to delete an image. You just need to provide the image name to be deleted. This can be found using the docker images command.
docker rmi <image>
Very helpful to make sure you are only using images that you wish to use and also remove old images that you might no longer use.