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:
- How Does the βdocker runβ Command Work?
- Set the New Container
- Set Password and Volume to Container
- Run an Interactive Shell in a Container
- Run the Container in the Foreground
- Run a Web Server
- Set Environment Variable
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.