Community for developers to learn, share their programming knowledge. Register!
Docker Containers

Starting and Stopping Containers in Docker


In this comprehensive article, we'll dive deep into the fundamental concepts of starting, stopping, and managing Docker containers, with a focus on practical examples and real-world scenarios.

Starting Docker Containers

At the heart of Docker's functionality lies the ability to create and manage containers. To start a new Docker container, you can use the docker run command. This command allows you to specify various options and configurations, such as the container image, environment variables, and network settings.

For example, let's say you have a simple Flask application packaged as a Docker image, as shown in the introduction. To start a new container based on this image, you can run the following command:

docker run -d -p 80:80 --name my-flask-app my-flask-app

Here's a breakdown of the command:

  • docker run: This initiates the process of starting a new Docker container.
  • -d: This runs the container in detached mode, meaning it runs in the background.
  • -p 80:80: This maps the host's port 80 to the container's port 80, allowing you to access the application from the host.
  • --name my-flask-app: This assigns the name "my-flask-app" to the container.
  • my-flask-app: This is the name of the Docker image to use for the container.

Once the container is running, you can access the Flask application by navigating to http://localhost in your web browser.

Stopping Docker Containers

To stop a running Docker container, you can use the docker stop command. This command gracefully stops the container, allowing it to perform any necessary cleanup tasks before shutting down.

For example, to stop the "my-flask-app" container, you can run the following command:

docker stop my-flask-app

If you need to forcefully stop a container, you can use the docker kill command instead:

docker kill my-flask-app

This command immediately terminates the container's main process, without allowing it to perform any cleanup tasks.

Listing Running Containers

To see a list of all the Docker containers currently running on your system, you can use the docker ps command:

docker ps

This will display information about each running container, including the container ID, the image used, the command being executed, the time the container has been running, and the ports the container is listening on.

If you want to see all containers, including those that are not running, you can use the docker ps -a command:

docker ps -a

This will show you all the containers that have been created, regardless of their current state.

Restarting Containers

If you need to restart a container, you can use the docker restart command. This command will stop the container and then start it again.

For example, to restart the "my-flask-app" container, you can run the following command:

docker restart my-flask-app

This will stop the container, and then start it again, allowing you to quickly refresh the state of the container without having to stop and start it manually.

Managing Container States

In addition to starting, stopping, and restarting containers, you can also manage the state of a container using other Docker commands.

For example, to pause a running container, you can use the docker pause command:

docker pause my-flask-app

This will temporarily pause the container, suspending its execution without stopping it completely. You can then resume the container using the docker unpause command:

docker unpause my-flask-app

Another useful command is docker rm, which allows you to remove a container from your system. This is useful when you no longer need a particular container and want to free up system resources.

docker rm my-flask-app

Keep in mind that removing a container will also remove any data stored within the container, so be sure to backup or migrate any important data before removing a container.

Summary

In this article, we've covered the fundamental concepts of starting, stopping, and managing Docker containers. We've explored various Docker commands, such as docker run, docker stop, docker ps, docker restart, and docker rm, and how they can be used to effectively control the lifecycle of your Docker containers.

By understanding these core container management techniques, you'll be well on your way to becoming a proficient Docker user, capable of deploying and managing complex applications in a containerized environment. Remember, the key to success with Docker is to practice, experiment, and continuously expand your knowledge of the platform and its capabilities.

Last Update: 21 Jan, 2025

Topics:
Docker