乐闻世界logo
搜索文章和话题

How do you start and stop a Docker container?

1个答案

1

Starting and stopping Docker containers are fundamental skills in daily Docker operations. Below, I will explain how to perform both operations.

Starting Docker Containers

Starting Docker containers typically involves using the docker start command. For launching a new container for the first time, use docker run. Here are the specific steps and examples:

  1. Using docker run to start a new container:

    • Command syntax: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

    • For example, if I want to run the ubuntu image and execute the bash command within it, I can use the following command:

      bash
      docker run -it ubuntu bash

      This command creates a new container from the ubuntu image and starts an interactive terminal running the bash command.

  2. Using docker start to start an existing container:

    • Command syntax: docker start [OPTIONS] CONTAINER [CONTAINER...]

    • For example, if I already have a container named mycontainer, I can use the following command to start it:

      bash
      docker start mycontainer

      This command only starts the container without entering it. If you need to enter the container, you can use the docker attach or docker exec commands.

Stopping Docker Containers

Stopping Docker containers primarily involves using the docker stop command. Here are the specific steps and examples:

  • Using docker stop to stop a container:
    • Command syntax: docker stop [OPTIONS] CONTAINER [CONTAINER...]

    • For example, if I want to stop the container named mycontainer, I can use the following command:

      bash
      docker stop mycontainer

      This command sends a stop signal to the container, which then performs cleanup tasks before stopping.

Important Notes

  • When using docker run, you can control container behavior with various options, such as memory allocation (-m), CPU (--cpus), and network configuration.
  • When stopping a container, if the application inside is slow to respond to stop signals or does not respond, you can use the docker kill command to forcibly stop the container.
2024年8月9日 13:44 回复

你的答案