Updated: Jan 25, 2022
In this tutorial, you'll learn how to start and stop docker containers, display information about containers running on your system, as well as container images downloaded to the host system. Finally, I will review some tips on using the built-in Docker help system.
Note: Docker commands are the same on all systems, including Linux, Windows, and Mac.
Let's start with some basics of running commands on Docker, and in my case, I use Docker on Windows 10. So to write some commands, we first need to open up a "Command prompt" as administrator. One way to do it is to open the search bar and type "Command" -> Run as administrator
Another option to work with docker commands is to use PowerShell:
How to run a container?
Will create a new container using the widespread Linux distribution of Debian as an example of how to run a container. When you first run a container, the image that you are using will be automatically downloaded if it hasn't been already; if the image exists on the Host, then there is nothing to download, and the DDocker will do nothing,
so let's run our first command and create a container with Debian:
PS C:\> docker run -dit debian
As you can see, the first two lines of output state that Docker doesn't have the image in the repository, so it needs to download the image from the default image repository (AKA: Docker HUB, which is s a service provided by Docker for finding and sharing container images):
After the image is downloaded, it starts a container using that image; the last line of the output is the unique container ID which we will use to manage this particular container:
Before continuing, I want to discuss the options I used in the command when creating this image: docker run -dit Debian. The 'd' options stand for 'detach' and allow a container to run in the background. When you run a container in the background Docker will print out a container ID as it did in the PS command screen.
The 'i' option stands for 'interactive`'. with this option, Docker keeps a standard input open even if you're not attached to the container. This allows you to attach to a container and send standard input to it, and in simple words, it allows you to run commands in the container. The last option is 't,' which allocates a pseudo-terminal to communicate with the container shell.
Note: We can use the same "dit" with different syntax such as -d -i -t Or -it -d, which all provide the same result.
How to validate that a container is running?
After creating a container, the first thing to notice is that it returns the container ID. If you fail to see it, then the container is not operational, and something was got wrong along the way. IF you get the container ID as we saw in the images above, then you can validate its state by running the following command:
PS C:\> docker ps
As you can see, the output sent to the console is indicating for all containers that are currently running on the host system:
How to stop a container from running?
If, for some reason, you want to stop a running container, type the following command based on the container ID:
PS C:\> docker stop 36725ede3040
As you can see in the docker desktop platform, our container is not stopped:
Note: you can also stop a container or perform other tasks using the container name (instead of its ID). Let's demonstrate by quickly creating a new container:
So the container name is "frosty_burnell," which is set randomly by Docker (you can override, as I will show in future posts). So if we want to stop the container from running using the container name:
PS C:\> docker stop frosty_burnell
We can also run the docker ps command; as you can see, no containers are running, and we have only headers from the command:
How to check what images are available on the Host?
Another standard command you'll often use is displaying what images have been downloaded and stored on the localhost:
PS C:\WINDOWS\system32> docker images
As you can see, the command returns different data about the image, including its name, size on disk, and when it was created.
Now, let's take a closer look at some of the complexities of a running container. Well, use the 'Inspect' command and give Docker the hash (always a unique ID of the container) of the container we're interested in examining.
docker inspect 'Container ID'
As you can see, there are sorts of information shared in the console as input that we can use for troubleshooting issues or generating some information on a container.
Working with the Docker Help system
One final thing to note about Docker is how it provides help on the command line. To open the Docker help menu, just type:
PS C:\> docker --help
As you can see, this is just a flat list of commands. So to gain more information on a specific command, let's say we want to use the 'Images' command:
PS C:\> docker images --help