Docker Run Command with Examples
Introduction
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 CMD
or 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.
The 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 CTRL+C
.
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 -d
option:
docker container run -d nginx
Output
050e72d8567a3ec1e66370350b0069ab5219614f9701f63fcf02e8c8689f04fa
Upon the termination of the root process, the detached container will also stop.
The docker container ls
command can be used to list the active containers.
Use the 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.
The --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 --name
option:
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:
Output
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
Output
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.
Use the -p
options as shown below to publish a port:
-p host_ip:host_port:container_port/protocol
- If
host_ip
is not given,0.0.0.0
is used by default. - TCP is used by default when no
protocol
is specified. - Use multiple
-p
options 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:
Output
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Tue, 26 Nov 2019 22:55:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 19 Nov 2019 12:50:08 GMT
Connection: keep-alive
ETag: "5dd3e500-264"
Accept-Ranges: bytes
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.
Use the -p
options as shown below to create and manage volumes:
-v host_src:container_dest:options
- The
host_src
can be an absolute path to a file, directory, or named volume on the host. - The
container_dest
specifies the exact path to a file or directory on the container. - Options include
rw
(read-write) andro
(read-only). If no option is supplied,rw
is used by default.
To demonstrate how this works, we'll establish a directory on the host and place an index.html
file inside:
mkdir public_html
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 public_html
directory.
Now, when you enter http://localhost:8080
into your browser, the contents of the index.html
file should appear. curl
can also be used:
curl http://localhost:8080
Output
Testing Docker Volumes
Run the Container Interactively
Use the -i
and -t
options to start the container when working with interactive programs like bash
.
The -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:
Output
root@1da70f1937f5:/#
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
.
Conclusion
Docker is the industry standard for application packaging and deployment, as well as a vital component of CI/CD, automation, and DevOps.
The docker container run
command is used to construct and run Docker containers.
Please leave a comment below if you have any queries.