Difference Between `docker run` and `docker start`
In Docker, both docker run
and docker start
are commands used to manage containers, but they serve different purposes and are used in different contexts. Understanding the distinction between these two commands is essential for effectively managing your Docker containers.
1. Overview of `docker run`
The docker run
command is used to create and start a new container from a specified image. This command combines the functionalities of creating a container and starting it in one step. When you run this command, Docker performs the following actions:
- Creates a new container from the specified image.
- Allocates a filesystem for the container.
- Sets up networking for the container.
- Starts the container and executes the specified command.
Example of `docker run`
Here’s an example of using docker run
to create and start a new Nginx container:
docker run -d -p 8080:80 nginx
In this command:
-d
: Runs the container in detached mode (in the background).-p 8080:80
: Maps port 80 of the container to port 8080 on the host.nginx
: Specifies the image to use for creating the container.
2. Overview of `docker start`
The docker start
command is used to start an existing, stopped container. This command does not create a new container; instead, it starts a container that has already been created and is in a stopped state. When you run this command, Docker performs the following actions:
- Starts the specified stopped container.
- Restores the container's state to the point where it was stopped.
Example of `docker start`
Here’s an example of using docker start
to start a previously created container:
docker start <container_id>
</container_id>
In this command:
<container_id>
: Replace this with the actual ID or name of the stopped container you want to start.
3. Key Differences
Feature | docker run | docker start |
---|---|---|
Purpose | Creates and starts a new container from an image. | Starts an existing stopped container. |
Container State | Requires the container to already exist and be in a stopped state. | |
Command Execution | Can specify a command to run in the new container. | Starts the container with the command that was specified when it was created. |
4. Conclusion
In summary, docker run
is used to create and start a new container from an image, while docker start
is used to start an existing stopped container. Understanding the differences between these two commands is crucial for effectively managing your Docker containers and workflows.