Create与Run Container怎么区分

2023-04-07 06:57:00 区分 create run

Docker containers can be created and run using the Docker API or CLI. The main difference between the two is that docker create only creates a container, while docker run also starts the container.

Docker containers are created using the docker create command. This command takes a number of options and a command to run inside the container. For example, the following command will create a container with the name my-container and run the command /bin/bash inside it:

$ docker create --name my-container /bin/bash

Once the container is created, it can be started using the docker start command:

$ docker start my-container

Alternatively, the docker run command can be used to create and start a container in one go. For example, the following command will create and start a container with the name my-container and run the command /bin/bash inside it:

$ docker run --name my-container /bin/bash

The main difference between the docker create and docker run commands is that docker run also starts the container, while docker create only creates it.

Another difference is that the docker run command can be used to create a container and start it in detached mode, while the docker create command cannot. Detached mode is useful for running containers in the background. For example, the following command will create and start a container in detached mode with the name my-container and run the command /bin/bash inside it:

$ docker run -d --name my-container /bin/bash

To sum up, the main difference between the docker create and docker run commands is that docker run also starts the container, while docker create only creates it. Additionally, the docker run command can be used to create a container and start it in detached mode, while the docker create command cannot.

相关文章