We’ve already seen the docker run command in action, a combined instruction that sequentially orchestrates the entire instantiation process in Docker.
It’s the most famous command in the tool, the one that features in almost every tutorial, and the one you’ll use 90% of the time to start a service.
But it’s not the only command involved. In this article, we’ll look at the lifecycle of our applications, and the difference between creating (create), starting (start), and running (run) 👇.
docker run is a “shortcut”
Internally, when you execute this command, Docker performs several sequential operations for you.
docker run = docker
pull(if needed) + dockercreate+ dockerstart
When we launch a container, three distinct phases occur:
- Acquisition: The image is downloaded (if you don’t have it).
- Creation: The container is prepared on disk (ID, network, volumes are assigned), but it does not consume CPU or RAM. It is “asleep”.
- Startup: The main process runs, and the container comes to life.
Docker Create
The docker create command creates the writable layer of the container on top of the image and prepares the configuration, but does not start it.
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
- OPTIONS: Parameters to configure the container.
- IMAGE: The Docker image from which the container will be created.
- COMMAND: Optional command to run inside the container.
- ARG…: Additional arguments for the command.
For example,
docker create --name my-web-server -p 8080:80 nginx
If you run this, you’ll see Docker returns a long ID and… nothing else. Nothing happens. If you go to your browser at localhost:8080, nothing will load.
If we check the status with docker ps -a (the -a flag is to also see stopped containers), we’ll see:
CONTAINER ID IMAGE STATUS NAMES
a1b2c3d4e5 nginx Created my-web-server
Notice the status: Created. The container exists, has a name, has ports assigned, but it’s “powered off”.
What is this for? It’s very useful in orchestration or advanced scripts where you want to “pre-provision” containers so they are ready, and start them all together later in milliseconds.
Docker Start
Now that we have the container created (but stopped), let’s start it.
docker start my-web-server
Now it works. Docker finds the container that already existed and simply starts the process. If we check the status again:
CONTAINER ID IMAGE STATUS NAMES
a1b2c3d4e5 nginx Up 4 seconds my-web-server
Status: Up. Now it consumes RAM and CPU.
There is also, logically, docker stop. This command sends a stop signal (SIGTERM) to the container. It shuts it down, but does NOT destroy it.
The container returns to the “Exited” state, keeping its data and configuration ready for a new docker start.
Docker Run
As we’ve said, docker run is simply the convenience of doing everything above in a single line.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- OPTIONS: Parameters to configure the container.
- IMAGE: The Docker image from which the container will be created.
- COMMAND: Optional command to run inside the container.
- ARG…: Additional arguments for the command.
For example,
docker run --name my-other-web -p 8081:80 -d nginx
Docker checks if it has the image, creates the container, and starts it immediately.
When to use each?
Don’t use docker run every time or you’ll create duplicate containers.
docker run: 95% of the time. When you want to start a new service from scratch.docker start: When you have an existing container you created previously (perhaps a database with persistent data) and you simply want to “resume” work where you left off.
Practical examples
Run a web server in a container
This example shows how to run an Nginx web server in a container:
docker run -d -p 8080:80 --name my_nginx nginx
-d: Run container in background (detached mode).-p 8080:80: Map port 8080 of the host to port 80 of the container.--name my_nginx: Assign a custom name to the container.nginx: The Docker image used.
Copy files between host and container
To copy a file from the host to the container:
docker cp file.txt <CONTAINER_ID>:/destination/path/
To copy a file from the container to the host:
docker cp <CONTAINER_ID>:/source/path/file.txt /destination/path/
Run a container with environment variables
This example shows how to run a container with custom environment variables:
docker run -e "MY_VARIABLE=value" ubuntu env
-e "MY_VARIABLE=value": Defines an environment variable inside the container.env: Displays all environment variables in the container.
