Explained – docker run Command With Examples

The “docker run” command starts a new container from a docker image. It allows you to specify various “options” and settings for the container, such as the name of the “IMAGE” to use, the “command” to run when the container starts, and any environment variables or volumes to mount.

This post will provide a detailed usage of the docker run command with the help of suitable examples:

Let’s start with the basics of the docker run command.

How Does the “docker run” Command Work?

The core functionality of any command depends on its syntax. Thus, we will look into the syntax of the “docker run” command before getting into its implementation:

Syntax:

$ docker run [options] IMAGE [command] [arg...]

The above syntax description is given below:

  • docker run: It is utilized to run a Docker container based on a particular image.
  • IMAGE: It specifies the image you want to use for the container.
  • command: It specifies which command you want to run inside the container.

For more information, use the docker run “–help” command to visualize all the options:

$ sudo docker run --help

Note: To install the docker command, follow the link.

Now, start the working of the docker run command in Linux.

Example 1: Set the New Container

In this example, the docker run command starts a new container from the nginx image. Port 80 is used as a communication protocol. It sets the container name to “itslinuxfoss“:

$ sudo docker run --name itslinuxfoss -p 80:80 -d nginx

The output shows the set of the new container by downloading from the “nginx” image.

Example 2: Set Password and Volume to Container

In this example, start a new container using the “name” argument from the “mysql” image. For this, set the root password “mypassword” to the variable “MYSQL_ROOT_PASSWORD” and specify a volume to store the database data using the “-v” option:

$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=mypassword -v mysql-data:/var/lib/mysql -d mysql

The output shows that we have set the new password from the “mysql” image.

Example 3: Run an Interactive Shell in a Container

To run an interactive shell in a new container based on the “nginx” image, the “sudo” privilege is required with the “it” option by specifying the “/bin/bash” directory:

$ sudo docker container run -it nginx /bin/bash

The output returns the bash shell after authentication.

Example 4: Run the Container in the Foreground

To run the container in the foreground, specify the name of the container “nginx” with the “sudo” privilege:

$ sudo docker container run nginx

The output shows that “nginx” is running in the foreground.

Example 5: Run a Web Server

To run a web server in a new container based on the “nginx” image, map the container’s port 80 to the host’s port 8080 in the below script:

$ docker run -p 8080:80 nginx

The output shows that the web server is running in the new container. If users want to run a web server other than nginx, follow the link.

Example 6: Set Environment Variable

To set an environment variable, we consider a PostgreSQL database in a new container based on the “postgres” image and mount the host’s current directory as the “/data” directory in the container:

$ docker run -e POSTGRES_PASSWORD=mypassword -v $(pwd):/data postgres

The output confirms that the “mypassword” is assigned to the “POSTGRES_PASSWORD” environment variable.

Conclusion

The docker “run” command starts a new container from a docker image. The users can set up the new container, the password, and the volume of the container, run an interactive shell in a new container or the foreground, run a web server, and many more. All these usages and the basic information of the docker run command have been explained in this guide.