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.