How To Use Docker Run Command.
In this article, we’ll use the official Nginx image to show various ways to run a Docker container.
The docker run command creates a container from a given image and starts the container using a given command. It is one of the first commands you should become familiar with when starting to work with Docker.
Docker Run Command
The docker run command takes the following form:
The name of the image from which the container should be created is the only required argument for the docker run command. If the image is not present on the local system, it is pulled from the registry.
Since the run command interacts with containers, it is a subcommand of docker container. The syntax of the new command is as follows:
Run the Container in the Foreground
By default, when no option is provided to the docker run command, the root process is started in the foreground. This means that the standard input, output, and error from the root process are attached to the terminal session.
The output of the nginx process will be displayed on your terminal. Since there are no connections to the webserver, the terminal is empty.
To stop the container, terminate the running Nginx process by pressing CTRL+C.
Run the Container in Detached Mode
To keep the container running when you exit the terminal session, start it in a detached mode. This is similar to running a Linux process in the background .
Use the -d option to start a detached container:
The detached container will stop when the root process is terminated.
You can list the running containers using the docker container ls command.
To attach your terminal to the detached container root process, use the docker container attach command.
Remove the Container After Exit
By default, when the container exits, its file system persists on the host system.
The –rm options tells docker run command to remove the container when it exits automatically:
The Nginx image may not be the best example to clean up the container’s file system after the container exits. This option is usually used on foreground containers that perform short-term tasks such as tests or database backups.
Set the Container Name
In Docker, each container is identified by its UUID and name. By default, if not explicitly set, the container’s name is automatically generated by the Docker daemon.
Use the –name option to assign a custom name to the container:
The container name must be unique. If you try to start another container with the same name, you’ll get an error similar to this:
Run docker container ls -a to list all containers, and see their names:
The meaningful names are useful to reference the container within a Docker network or when running docker CLI commands.
Publishing Container Ports
By default, if no ports are published, the process running in the container is accessible only from inside the container.
Publishing ports means mapping container ports to the host machine ports so that the ports are available to services outside of Docker.
To publish a port use the -p options as follows:
If no host_ip is specified, it defaults to 0.0.0.0.
If no protocol is specified, it defaults to TCP.
To publish multiple ports, use multiple -p options.
To map the TCP port 80 (nginx) in the container to port 8080 on the host localhost interface, you would run:
You can verify that the port is published by opening http://localhost:8080 in your browser or running the following curl command on the Docker host:
The output will look something like this:
Sharing Data (Mounting Volumes)
When a container is stopped, all data generated by the container is removed. Docker Volumes are the preferred way to make the data persist and share it across multiple containers.
To create and manage volumes, use the -p options as follows:
The host_src can be an absolute path to a file or directory on the host or a named volume.
The container_dest is an absolute path to a file or directory on the container.
Options can be rw (read-write) and ro (read-only). If no option is specified, it defaults to rw.
To explain how this works, let’s create a directory on the host and put an index.html file in it:
Next, mount the public_html directory into /usr/share/nginx/html in the container:
Instead of specifying the absolute path to the public_html directory, we’re using the $(pwd) command, which prints the current working directory .
Now, if you type http://localhost:8080 in your browser, you should see the contents of the index.html file. You can also use curl:
Run the Container Interactively
When dealing with the interactive processes like bash, use the -i and -t options to start the container.
The -it options tells Docker to keep the standard input attached to the terminal and allocate a pseudo-tty:
The container’s Bash shell will be attached to the terminal, and the command prompt will change:
Now, you can interact with the container’s shell and run any command inside of it.
In this example, we provided a command (/bin/bash) as an argument to the docker run command that was executed instead of the one specified in the Dockerfile.
Docker is the standard for packaging and deploying applications and an essential component of CI/CD, automation, and DevOps.