With more and more computing power available on servers, OS-level virtualization has become the norm in managing and scaling infrastructure. This comprehensive Docker tutorial for beginners will cover how to run, start, and stop Docker containers.
What is Docker?
Docker is an OS-level virtualization that isolates software into containers. Docker usually uses fewer resources than traditional virtualization techniques as it interacts directly with the Linux host’s kernel. Docker also solves the problem of having software with different library versions on a single server. It fits very well in the DevOps workflow allowing for easy deployment and integration of system and software.
Ensure you have the latest version of Ubuntu installed on your system. Docker: Install Docker or follow Docker's official documentation. Make sure your user is a member of the docker group. This will give you enough permission to use docker commands without having to type the admin password on every execution.
Docker: start container
We now have a basic introduction to Docker and everything necessary to work with it, so let's download our first Docker image and run it into a container.
Option 1: Downloading an image and run the container
As with most platforms, Docker has its own way to say “hello world” that helps you quickly download and start your first container. Run the following command:
docker run hello-world
This will download the hello-world image from Docker Hub, a large repository of container images. Docker will then create and run a container from the downloaded image. Once the main process of the container completes, Docker will exit the container and return to the Ubuntu shell.
Option 2: Start a stopped Docker container with docker start
We may already have stopped/exited containers on our server that you want to start. This can be done using the name of the container:
docker container start mywebserver
We can then verify the status of the container by listing all the containers using the
docker ps command:
docker ps -a
We can see that the container is up and running in the background.
Docker: stop container
We now have running containers and will learn how to stop Docker containers.
Option 1: Ending containers with the docker container stop command
The simplest way to stop a running container is to use the
docker container stop command followed by the container name:
docker container stop mywebserver
We can also stop multiple containers at the same time by adding their names to the
docker container stop command:
docker container stop mywebserver mywebserver1 mywebserver2
Option 2: Exiting containers immediately using the docker kill command
The previous command, docker container stop, sends a SIGTERM signal to the main process of the container. And after a grace period which is 10 seconds, it sends a SIGKILL signal to the main process, ending the container.
In case we need to stop a container immediately, we can send a SIGKILL, skipping the SIGTERM using the following command:
docker kill mywebserver
Option 3: Stopping and removing a container with the docker rm command
We can also stop and remove a container using
docker rm. Trying to remove a running container will generate an error:
We can either stop the container and proceed to the removal or simply use the force switch,
-f, to stop and remove the container in a single command:
docker rm -f mywebserver
Removing the container does not remove the volumes that were attached to the container. Adding the
-v switch to the command will remove those volumes.
Option 4: Stopping all running containers
On a server containing multiple containers, we can stop all of them by getting the list of running ones and passing their container IDs to the
docker stop command.
To list the ID of all running containers, we will use the following command:
docker ps -q
-q (quiet) flag displays only the numeric ID, hiding all the other details.
We can now use this input as an argument for
docker stop $(docker ps -q)
Docker: list containers with filters
Now that we have at least one container on our server and have learned how to start and stop them, we will dig deeper into listing containers with filters. Filters allow us to list containers matching the exact criteria we are looking for and give more options than using default docker flags.
The syntax is as follows:
docker ps --filter "FILTER_KEY=FILTER_VALUE"
Option 1: By Status
The most commonly used filter is the status. We can list all exited containers with the following command:
docker ps -a --filter "status=exited"
The same can be done with running ones: docker ps -a --filter "status=running"
Option 2: By Port
Another useful filter is the exposed port. We often identify our services and containers by port number. For example, we know that a container with port 8080 published, meaning accessible outside the container, is a web server.
We can list those containers with:
docker ps -a --filter "publish=8080/tcp"
It also works with exposed ports. Exposed ports are ports that are open on the container. Simply replace publish with expose:
docker ps -a --filter "expose=80/tcp"
Option 3: By Name
Imagine a scenario where we have several dev containers and want to list them all. We can use the name filter to do so:
docker ps -a --filter "name=dev"
Note that the filter matches all containers containing ‘dev’ in their names.
Combining filters with start/stop
Now that we know how to list containers with multiple filters, we can combine those filters with the start and stop commands we learned in the previous sections. We use the
-q flag to make sure only the numeric IDs are returned:
Start all containers with specific filters:
docker start $(docker ps -aq --filter "FILTER_KEY=FILTER_VALUE")
Stop all containers with specific filters:
docker stop $(docker ps -aq --filter "FILTER_KEY=FILTER_VALUE")
In this tutorial, we have covered the basics of Docker, focusing on how to start and stop containers and how to list containers based on filters. We have seen that most of the operations can affect multiple containers at once, thus helping you in complex infrastructure with several containers running. You can learn more about docker container commands in the official Docker documentation.