Docker Stop Container

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will experience a scenario in the midst of a desert storm, where you encounter a menacing figure known as the "Desert Demon." Your goal is to navigate through the storm and learn the essential skill of stopping Docker containers in order to escape the demon's wrath.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") subgraph Lab Skills docker/stop -.-> lab-271501{{"`Docker Stop Container`"}} end

Understanding Docker Container Execution

In this step, you will learn about the basics of Docker container execution and how to stop running containers.

  1. First, let's run a simple Docker container using the nginx image by executing the following command in your terminal:

    docker run -d --name my-nginx nginx
  2. Now, to stop the running container, use the following command:

    docker stop my-nginx
  3. After executing the docker stop command, the container will be stopped, and you can verify this by running the following command:

    docker ps -a

    You should see that the stopped container is listed with a status of "Exited."

Graceful Shutdown of Containers

In this step, you will learn about the graceful shutdown of containers using the docker stop command.

  1. Let's run a new container using the alpine image and execute a long-running command within the container:

    docker run -d --name long-running alpine sh -c "while :; do echo 'Long-running command...'; sleep 5; done"
  2. Now, initiate a graceful shutdown of the container by stopping it with the docker stop command:

    docker stop long-running
  3. Verify the status of the container by running:

    docker ps -a

    The container should be in a "Exited" state.

Summary

In this lab, the focus was on mastering the skill of stopping Docker containers. By following the scenarios presented and executing the provided steps, you gained practical experience in stopping running containers, ensuring a deeper understanding of container management within Docker. This will be invaluable in your journey to becoming proficient in Docker container management.

Other Docker Tutorials you may like