The Wayback Machine - https://web.archive.org/web/20240709065956/https://www.geeksforgeeks.org/docker-start-vs-docker-run/
Open In App

Docker Start vs Docker Run

Last Updated : 08 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is a platform as a service(PaaS) that allows engineers to use OS-level virtualization to automate the deployment of applications inside lightweight and portable packages called containers. The containers have a very big advantage as they allow apps to run on different platforms effortlessly, and reduce the old headache of “it runs on my machine”.

Terminologies

Docker run

The docker run command creates and starts a new container, also pulling the image if it is needed to start the container. It is used when we want to create a container for the first time. example of a docker run command is

docker run -d  --name <container-name>
  • -d: run the container in the background and print the new container ID
  • –name: assign the name container-name to the container

Step-by-step procedure for Docker run

  1. Pull the image: If the specified image is not available, docker pulls it from the registry.
  2. Create container: a new container is created from the specified image.
  3. Assign resources: Compute resources like CPU, memory, volumes, and ports are assigned to the container.
  4. Start the container: The container is started, and the given command is executed.

Docker start

The docker start command is used to start an existing, stopped container. It does not create a new container but starts one that was previously created and stopped. example of a docker start command is

docker start <container-name>

Step-by-step procedure for docker start

  1. Identify the container: use the container name or ID to specify which container to start.
  2. Start the container: the container is started with the previous existing configuration.

Difference between docker run and docker start

Feature

docker run

docker start

container creation

creates a new container

does not create a new container

image requirement

requires a docker image to start a new container

uses an existing container

execution

starts the container and executes the given command

only starts the container

use case

on initial deployment of container

restarts a previously stopped container

Example

for this example, we will build a simple flask app and deploy it in a docker container.

create a directory, let's say 'flask_app' and move into it
Example

Create by building a virtual environment to isolate python dependencies

python3 -m venv .venv 
source .venv/bin/activate
virtual environment

Install flask using

pip install flask
Install flask using

create a python file called ‘app.py‘.

Python
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'docker run vs docker start'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

and our Dockerfile looks like

FROM python:3
WORKDIR /server
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD [ "flask", "run", "--host=0.0.0.0", "--port=5000" ]

We must freeze out our python dependencies before building our image of application

pip freeze > requirements.txt

At last, our ‘flask_app’ project structure looks similar to

project structure

Let’s start by building an ‘image’ of our project by running

docker build -t app_image .
building an 'image'

The docker will create our image which we can see by running command

docker image ls 

if our image built successfully we can see our image specified in the list

Now, we have to start our app inside the docker container, this is where our differences come with ‘docker start’ and ‘docker run’. Since we have not created the container out of our image we will use docker run to build the container and start it.

docker run -p 3000:5000 --name app_container app_image
docker run


  • -p: binds the port 3000 of our system to port 5000 of the container
  • –name: to pass custom name of your docker container

The above command will start the flask app in “localhost:3000/”, too see the running container you can use

docker ps

Let’s stop our container using the command

docker stop app_container
docker stop


Our container is now created, now if we want to start it again we have two options either we can ‘docker start’ it or run our previous ‘docker run’ command.

To use docker start, we can start our container by command by passing the container name

docker start app_container
docker start

Scenarios for using docker start and docker run

docker start

docker run

restarting a stopped container

launching a new container

maintaining state of the container

you want to test an image if it runs correctly

you stopped a container temporarily and now want to start it again

you need to execute a specific command in a new container

Common mistakes to avoid

  • docker start is for starting stopped containers
    • docker run is for creating and starting new containers
    • Confusing docker start and docker run
  • Using docker start with non-existent containers: ensuring container exists in a stopped state before using docker start
  • Forgetting option flags in docker run: missing options like –name for customizing the name of the container
  • Failing to remove temporary containers: use –rm to remove then when the temporary container is stopped

Performance factor: docker start vs docker run

  • Startup time
    • docker start: Typically faster because it starts an already existing container without needing to set up a new instance
    • docker run: Slower as it involves creating a new container instance, setting up filesystem layers, and executing the entry point
  • Resource Utilization
    • docker start: Reuses existing resources, which can be more efficient if the container has already been configured and has necessary data
    • docker run: Consumes more resources initially as it allocates new resources and performs setup tasks
  • State preservation
    • docker start: Preserves the state of the stopped container, including the filesystem and running processes
    • docker run: Starts with a clean state, which might be preferable for stateless applications or testing

Best practices and for managing containers and optimizing

  • Use volumes for data persistence and monitor resource usage
  • pre pull images to reduce your startup time for docker run
  • Set CPU and memory resource limits for optimized usage and allocation of resources by the container
  • Implement health checks to ensure are running correctly
  • Regularly clean up stopped containers to free up allocated resources
  • Use docker compose or Kubernetes for managing multi container applications and automating container lifecycle

Troubleshooting common issues

  • Container fails to start
    • check logs using command ‘docker logs <container-name>’ to view logs and find the issue
    • use command ‘docker inspect <container-name>’ to view configuration and status details
  • Resource constraints
    • monitor the resource usage using the command ‘docker stats’
    • modify the resource limits of the container and allocated more resources to the container if needed
  • Permission Issues
    • Ensure the docker is allowed necessary permissions to run correctly
  • Container exits immediately
    • ensure the entry point or command specified in the Dockerfile or run command is correct
    • Start the container in interactive mode using ‘-it’ flag with a shell to manually debug

Docker start vs Docker run – FAQs

What happens if I run docker start on a non-existing container?

An error will be returned indicating the specified container does not exist.

Is there any difference in resource allocation between docker run and docker start?

docker run allocates the resources when creating the container, while docker start uses the previously allocated resources without changes

Can I use docker start to run a command in a container?

No, docker start only starts the container. To run a new command in an existing container, use ‘docker exec’ command.

What is the state of a container after using docker run?

After docker run, the container will be in a running state if the command is executed successfully.

Can I change the port mapping with docker start?

No, docker start only starts an existing container with its original configured port, to change the port value you must reassign again with docker run to modify the settings.



Previous Article
Next Article

Similar Reads

Docker Tips : about /var/run/docker.sock
Docker has thus gained lots of usage today in software development. It allows developers to create, deploy, and run their applications in isolated environments. Such isolation will guarantee consistency between development, testing, and production environments and, finally, app security and scalability. A central part of the functionalities of Dock
8 min read
Run PostgreSQL on Docker and Setting Up pgAdmin
PostgreSQL, an effective tool, is a free­-to-use relational database management system. Docker can quickly construct and orche­strate its instances without bothering about the complexity of setup or depe­ndencies. This step-by-step simple guide will show you how to get Postgre­SQL on Docker, and then, use the pgAdmin extension to look at the databa
7 min read
How To Run A Cron Job Inside A Docker Container ?
In the containerization world, management of resources and scaling the applications are necessary to make our services effective and user-friendly for the users. In this, the cron job helps in a significant way with facilitating features such as isolation, portability, scalability, and resource management. It will be a convenient and efficient solu
5 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can also save that environment for future use also as
7 min read
Docker Compose Tool To Run aMulti Container Applications
The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml file with a single command, you create and start all
8 min read
Docker Run with Environment Variables
Containerization has significantly advanced software development by packaging applications and their dependencies together and containerizing them to ensure consistency across multiple environments. Environment variables are one of the most important features of containerization as they make it possible to manage configuration and behavior. Imagine
7 min read
Docker Compose up, down, stop start difference
Docker Compose has been a technology of tremendous importance in recent DevOps and software development practices because it provides a powerful toolset for the management of multi-container Docker applications. The basic commands for Docker Compose, such as up, down, stop, and start, are at the core of Docker Compose. Its main functionality orches
6 min read
How to Use Docker For Fault Tolerance with Docker Replicas?
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine. Docker ReplicasDocker replicas refer to running multiple instances of a doc
4 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact and portable solution. Deploying containerized app
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
Article Tags :