Before we begin talking about Docker Run Command, let's briefly understand – What is Docker?
Docker is a platform that enables you to create, test, and deploy applications as flexible, self-sufficient containers that run almost anywhere. The
docker run command creates a container from a specified image and launches it with a specified command.
In this tutorial, you will understand Docker run command. We will also address a few FAQs on Docker run command.
Docker Run Command
The following is the syntax for the
docker run command:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The only argument needed for the
docker run command is the name of the image from which the container should be developed. The image is taken from the registry if it is not already on the local system.
In the absence of a command, the command specified in the Dockerfile’s
ENTRYPOINT instructions is executed when running the container.
The Docker CLI has been restructured starting with version 1.13. The objects that each command interacts with are grouped together.
run command is a sub command of the
docker container since it interacts with containers. The new command has the following syntax:
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
The previous, pre-1.13 syntax is still supported. The
docker container run is an alias for the
docker run command in the background. The new command syntax is recommended for users.
The Docker documentation page contains a list of all the
docker container run options.
Run the Container in the Foreground
When no options are given to the
docker run command, the root process is launched by default in the foreground. This indicates that the root process's standard input, output, and error are attached to the terminal session.
docker container run nginx
On your terminal, you will see the output of the nginx process. The terminal is empty since there are no connections to the web server.
To stop the container, terminate the running Nginx process by entering
Run the Container in Detached Mode
Start the container in a detached mode to keep it running after you exit the terminal session. This is akin to running a Linux process in the background.
Start a detached container with the
docker container run -d nginx
Upon the termination of the root process, the detached container will also stop.
docker container ls command can be used to list the active containers.
docker container attach command to connect your terminal to the detached container root process.
Remove the Container After Exit
By default, when the container shuts down, its file system persists on the host system.
--rm option instructs the
docker run command to remove the container when it automatically exits:
docker container run --rm nginx
The Nginx image might not be the best example of how to clean up the container's file system once it exits. This setting is typically used to foreground containers running quick operations like tests or database backups.
Set the Container Name
Each container in Docker has a unique
UUID and name. The container's name is automatically generated by the Docker daemon by default if it is not explicitly set.
To give the container a unique name, use the
docker container run -d --name my_nginx nginx
The container name needs to be distinctive. You will encounter an error similar to the following if you attempt to start another container with the same name:
docker: Error response from daemon: Conflict. The container name "/my_nginx" is already in use by container "9...c". You have to remove (or rename) that container to be able to reuse that name.
To get a list of all containers, run
docker container ls -a, and see their names:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d695c1f5ef4 nginx "nginx -g 'daemon of…" 36 seconds ago Up 35 seconds 80/tcp my_nginx
The descriptive names are helpful when running docker CLI commands or referring to the container in a Docker network.
Publishing Container Ports
The process that is operating inside the container can only be accessed from inside the container by default if no ports are broadcasted.
Publishing ports entails mapping container ports to host machine ports so that they are accessible to services other than Docker.
-p options as shown below to publish a port:
host_ipis not given,
0.0.0.0is used by default.
- TCP is used by default when no
- Use multiple
-poptions to publish multiple ports.
To map TCP port 80 (nginx) in the container to port 8080 on the host localhost interface, use the following command:
docker container run --name web_server -d -p 8080:80 nginx
You can check if the port is open by going to
http://localhost:8080 in your browser or executing the
curl command on the Docker host:
curl -I http://localhost:8080
The output will resemble this:
HTTP/1.1 200 OK
Date: Tue, 26 Nov 2019 22:55:59 GMT
Last-Modified: Tue, 19 Nov 2019 12:50:08 GMT
Sharing Data (Mounting Volumes)
When a container is stopped, all data it produced is deleted. Docker Volumes are the recommended method for persisting and sharing data across several containers.
-p options as shown below to create and manage volumes:
host_srccan be an absolute path to a file, directory, or named volume on the host.
container_destspecifies the exact path to a file or directory on the container.
- Options include
ro(read-only). If no option is supplied,
rwis used by default.
To demonstrate how this works, we'll establish a directory on the host and place an
index.html file inside:
echo "Testing Docker Volumes" > public_html/index.html
After that, mount the
public_html directory into
/usr/share/nginx/html in the container:
docker run --name web_server -d -p 8080:80 -v $(pwd)/public_html:/usr/share/nginx/html nginx
We are utilizing the
$(pwd) command, which prints the current working directory, in place of specifying the absolute path to the
Now, when you enter
http://localhost:8080 into your browser, the contents of the
index.html file should appear.
curl can also be used:
Testing Docker Volumes
Run the Container Interactively
-t options to start the container when working with interactive programs like
-it options instruct Docker to allocate a pseudo-tty and maintain the standard input attached to the terminal:
docker container run -it nginx /bin/bash
The terminal will be attached to the container's Bash shell, and the command prompt will change to:
You can now interact with the container's shell and issue any command from inside of it.
In this example, a command (
/bin/bash) that was provided as an argument to the
docker run command rather than the one listed in the Dockerfile was executed.
FAQs on Docker run command
Can I specify options or flags with the "docker run" command?
Yes, the "docker run" command has several options and flags you can use to customize container behavior. Some commonly used options include exposing ports, mapping volumes, setting environment variables, and configuring networking.
How can I control resource allocation using the "docker run" command?
You can use the resource control options available with the "docker run" command to specify the CPU shares, memory limits, and other resource constraints for the container.
Can I run multiple containers with a single "docker run" command?
By default, a single "docker run" command creates and runs a single container. However, you can use Docker Compose, Docker Swarm, or other container orchestration tools to define and run multiple containers as part of a single application or service.
Can I specify a name for the container created with the "docker run" command?
Yes, you can provide the
--name option followed by a chosen name to assign a specific name to the container. For example:
docker run --name my_container image_name.
How can I attach to the container's console or access its shell?
You can use the
docker exec command to access a running container's shell or execute commands inside it. For example:
docker exec -it container_id bash (assuming bash is available).
What happens when I use the "docker run" command with an image that is not available locally?
Docker will automatically attempt to download the image from a configured registry or repository before creating and running the container. If the image is not found, Docker will display an error message.
How can I remove a container after running it with the "docker run" command?
To remove a container, you can use the
docker rm command followed by the container ID or name. For example:
docker rm container_id.
Docker is the industry standard for application packaging and deployment, as well as a vital component of CI/CD, automation, and DevOps.
docker container run command is used to construct and run Docker containers.
Please leave a comment below if you have any queries.