You are currently viewing How to Start And Stop Docker Container?

How to Start And Stop Docker Container?

As server processing power has increased, operating system-level virtualization has become the standard for managing and expanding infrastructure. Docker containers have revolutionized the way we develop, deploy, and manage applications. They provide a lightweight and efficient way to package and run applications, making it easier to ensure consistency across different environments. 

With our Docker Consulting services, we offer complete support for all your Docker queries. You can easily get assistance starting, or stopping a Docker container from our certified server administrators.

For now, let’s understand docker containers, how to start docker container and how to stop docker container.

Understanding Docker Containers

Docker containers are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including the code, runtime, system tools, and libraries. Containers are isolated from each other and the host system, ensuring consistent behaviour regardless of where they are run.

How to start Docker container?

The process to start Docker container is a fundamental operation when working with Docker. You can follow these steps to start Docker container:

  1. Install Docker: You must make sure to have Docker installed on your system. If you don’t have it installed, just download and install Docker from the official website.
  1. Pull a Docker Image (if necessary): Docker containers are created from images. If you haven’t already pulled the Docker image you want to run, you can do so using the ‘docker pull’ command. For example, to pull the latest Ubuntu image, you can run:
How to start and stop Docker container
  1. Run the Docker Container: To start Docker container, use the docker ‘run command’ followed by the image name. You can specify additional options and settings as needed. For example, to start an interactive shell session in an Ubuntu container, you can run:
How to start and stop Docker container
  • -it: This option allows you to run the container interactively and allocate a pseudo-TTY for the session.
  • ubuntu:latest: This is the name of the Docker image you want to use.
  • /bin/bash: This specifies the command you want to run inside the container. In this case, it’s launching a Bash shell.
  1. Container Execution: After running the ‘docker run’ command, you’ll be inside the container’s shell (in this example, a Bash shell). You can now execute commands and perform tasks within the container. When you’re finished, you can exit the container by typing ‘exit’.
How to start and stop Docker container

This will exit the container and return you to your host system’s shell.

Remember that Docker containers are temporary. When you exit a container, it stops running. If you want to start a stopped container again, you can use the docker start command, followed by the container ID or name:

If you want to start docker container in the background (detached mode), you can add the ‘-d’ or ‘–detach’ option to the ‘docker run’ command, like this:

How to start and stop Docker container
How to start and stop Docker container

This will start docker container in the background, and you’ll get the container’s unique ID as output. To interact with a detached container later, you can use the ‘docker exec’ command to run commands inside the running container.

Starting Docker containers is a fundamental operation in working with Docker, and you can customize the behaviour of containers by specifying additional options and settings during the ‘docker run’ command.

Also Check: Our blog post on how to update docker container

How to stop Docker container?

To stop Docker container is an important part of managing containers. To stop Docker container, you can use the ‘docker stop’ command.

Here are the steps to stop Docker container:

  • Identify the Container: First, you need to identify the container you want to stop. You can use either the container’s ID or its name.

To list all running containers and their details, run:

How to start and stop Docker container
How to start and stop Docker container

Note down the container ID or name of the container you want to stop.

  • To stop Docker container, use the ‘docker stop’ command followed by the container ID or name. This allows the container to perform cleanup tasks before shutting down:
How to start and stop Docker container

For example, if the container’s name is “my-container,” you can stop it like this:

How to start and stop Docker container

Docker will send a SIGTERM signal to the container, allowing it to perform any necessary cleanup tasks. If the container gracefully stops, it will change its status to “Exited.”

Also Read: Our blog post on how to list docker containers

How to stop all docker containers?

To stop all Docker containers running on your system, you can use a simple one-liner that uses the ‘docker stop’ command in combination with a list of all running containers. Here’s how to do it:

Open a terminal or command prompt, and run the following command:

How to start and stop Docker container

Let’s take a look at what functions this command performs

  • docker ps -q: This command lists all running containers and returns their container IDs (short form).
  • $(docker ps -q): This part of the command substitutes the output of docker ps -q, which are the container IDs of all running containers.
  • docker stop: This is the command that actually stops the containers. It takes the list of container IDs as arguments and stops each container.

After running this command, Docker will send a SIGTERM signal to each running container, allowing them to perform any necessary cleanup tasks before shutting down.

Forceful way to stop Docker container

If a container is unresponsive or you want to forcefully terminate it without allowing it to perform cleanup, you can use the ‘docker kill’ command. To understand how to do it, you can read our blog on how to kill a docker container.

Conclusion

The process to start and stop Docker container is simple. You must know it if you want to navigate easily in the world of containerization. These Docker containers have transformed the way we package, deploy, and manage applications, and understanding how to use this technology effectively can greatly enhance your development and deployment workflows. If you’re still left with some doubts, contact our 24×7 support team. We’re always here to help