Docker has revolutionized the way we build, ship, and run applications, offering developers an efficient way to manage containerized environments. Among the numerous Docker commands, docker run and docker start are two of the most commonly used, yet they often cause confusion among beginners. This blog post will clarify the difference between the docker run and docker start commands, provide walkthroughs for their usage, and discuss use cases where each is applicable.
Docker Run Command
The docker run command creates and starts a new container from an image. Think of it as a two-in-one command: it initializes a container (if one doesn’t already exist) and starts it immediately. The syntax is docker run, followed by the options, image name, startup command, and arguments.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Some key options you will often encounter include:
-d: Runs the container in detached mode (in the background).
-it: Runs the container interactively with a terminal.
--name: Assigns a custom name to the container.
-p: Maps host and container ports.
-v: Mounts a volume to persist data.
Running a Docker Container
Let’s create a new container to run an Nginx web server. I’m going to use the integrated terminal in Docker Desktop. First, we need to pull the Nginx image using the docker pull command.
docker pull nginx
Next, we will run the container for the nginx image using the docker run command. We will give the container the name my-nginx, then map port 8080 on the Docker host to TCP port 80 in the container. We will run the container in detached mode and then specify the image name.
docker run --name my-nginx -p 8080:80 -d nginx
We can verify that the container is running in Docker Desktop. Clicking the link in the Ports column will open the Nginx start page.
We can stop the container by clicking stop next to the container name. The container will exit but still exist in a “stopped” state.
Docker Start Command
The docker start command is used to restart an existing container that was previously stopped. Unlike docker run, it does not create a new container.
docker start [OPTIONS] CONTAINER
Some options you may encounter frequently include:
-i: Attaches STDIN for interactive mode.
-a: Attaches the container’s output (logs).
Now let’s start our stopped nginx container. You can either click the start button next to the stopped container in Docker Desktop or use the docker start command with the name of the container.
docker start my-nginx
You should see a green bullet indicating that the container is running. Click on the port link and you should see the nginx welcome page.
Differences Between Docker Run vs Docker Start Commands
The key difference between Docker run and start is that Docker run creates and starts a new container whereas Docker start starts an existing stopped container.
Some use cases for the Docker run command include:
- First-time setup: Use docker run when creating a container from an image for the first time.
- Different configurations: If you need a container with a different setup (e.g., custom ports, environment variables), docker run allows you to configure these at runtime.
- Stateless apps: For containers where you don’t need to retain any data, docker run is ideal since it creates a fresh instance every time.
Some use cases for the Docker start command include:
- Reusing containers: When you want to resume a container that was previously stopped without losing its state or data.
- Preserving logs and files: Stopped containers retain their logs and file systems. Use docker start to pick up where you left off.
- Faster startup: Starting an existing container is quicker than creating a new one.
Understanding the difference between docker run vs docker start is crucial for efficient Docker usage. While docker run is the go-to for creating new containers, docker start shines in scenarios where you need to resume work on an existing one. Mastering these commands will help streamline your workflow and improve container management.
Get Valuable Learning Resources Delivered To Your Inbox Every Wednesday!
You can learn more about this topic in my Tech Career Newsletter. This newsletter will give you insight into multiple stages of your career so that you can form your career plan, overcome learning blockers, and stay competitive in today’s crowded job market. It includes featured content from authors and influencers in my network to help you navigate your career and learn new skills. Subscribe today at hoffstech.com/newsletter and set yourself up for career success!
Top comments (0)