How to Start and Stop Docker Containers | Step-by-Step

May 29th, 2023
How to Start and Stop Docker Containers | Step-by-Step

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.

Prerequisites

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.

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

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. docker hello-world

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

docker container start

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

docker container stop

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

docker kill container

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:

docker remove container

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

docker list running containers

The -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 stop $(docker ps -q)

docker stop multiple containers

Explore how web hosting service provider Debesis improved its service quality, performance, and reliability by migrating to Cherry Servers' bare-metal servers. "Cherry Servers engineers always help when we need them, while their customer service quality is a blast!"

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")

Conclusion

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.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: af2b7cbf.630