Open In App

Docker Run Command | Complete Tutorial

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker launches the containers in seconds, and the heart of running containerized applications lies in the powerful command known as ‘docker run‘. Docker is the Container Platform tool. It helps in packaging an application with all its dependencies into a single entity, and it supports running the applications as containerized images on any platform that supports Docker. It acts as a gateway for deploying and managing the Docker containers.

What Is Docker Run Command?

The `docker run` is used to create a running container from using a docker image. It is used with options, docker images, commands, and arguments. It is used for developing, shipping, and running applications in containers. With “docker run,” users can specify various options and parameters to customize the container’s behavior, such as port mappings, volume mounts, environment variables, and more.

Checking Docker Service Status

Before trying the Docker commands, ensure that the Docker software is set and the service is active. Check the status of the docker service as shown in the below the figure:

Checking status of Docker

If the status of the Docker service isn’t in active mode then try running the given below commands, it will enable the docker and keep it active whenever the OS is restarted.

systemctl enable docker --now

Docker Container Run

If you come to deal with the core part of the docker i.e., `docker run` is the command that is used for starting the containers with the help of assigning docker images. The minimum parameter needed with the docker run command is the docker image but the docker run command offers more options for customizing container behavior compared to other commands.

How To Use Docker Container Run Command?

Docker launches the containers by docker run command. While launching a container you can provide the extra functionalities to the container on using docker run options. The basic syntax of the docker run command is as follows:

Syntax

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

Description

The execution of docker run command makes a api call to backend that pull the specified container image if needed for the container and then starts creating a container.

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Command

Usage

OPTIONS

Optional flags are used for configuring the settings in container

IMAGE

A bundle of software, packages, libraries used to create docker container

COMMAND

To run/perform operations inside the container we use commands.

ARG

It is known as arguments .These are used for passing to commands for performing operations inside the container.

  • As shown in the figure below you can use the help option to know other command with the docker run.

Docker-run-help

Key Options For Docker Run Command

Options

Usage

-d or
–detach

It is used for running container in the background mode by freeing up the terminal of the specified docker container.

–name

It is used for assigning name to the container for easy identification of particular docker container.

-p or

–publish

It is helpful in mapping the ports between the containers and the host system. This concept also called as port binding.

-v or
–volume

It is used for providing persistent volume by mounting the host directory to container file system .This concept known as volume binding.

–env

To set up environment variables inside the container. These optional flags are known as environmental variables used for providing information to inside the container.

–env-file

It options helps in reading the file of environmental variables

–cpus

used for specifying number of cpus

–help

prints docker run usage

Examples Of Docker Run Command

The docker run command can be used with many options making the container customizable with robust features. On continution of this article, we will dive on discussing the creation of container with docker run command effectively with cover all the possible options.

Docker Run To Run Docker Containers: Discover How To Do It In A Simple Way

Docker run command launches the containers with light weighted software and packages containing component known as Docker Image.On providing the optional parameters you addon the functionalities. Firstly lets see running a container in docker without using any options. The command looks as follows with using hello-world as sample image.

docker run hello-world

docker-hello_world

Run A Container Under A Specific Name

When we create a container with docker by default it provides a unique name to the creating container and it also provide the specify the name of the container by ourself with the option –name.

docker run  --name [ container_name ]  [docker _image]
  • For example considering the container_name and docker images as mycontainer, ubuntu the command will look like this:
docker run --name mycontainer centos:latest

Container-Under-specific-name

Running A Container In Detached Mode (-d)

The `-d` option is particularly useful for running the containers in the background. This is essentials for the long-running process or services that should be continued running independently of the terminal.

  • The command for running a container With Detached Mode in the background is:
docker container run -d [docker_image]

Example:

docker container run -d --name myserver httpd:latest
  • The following screenshot clearly make you understand successful running of container in background without any interaction display created container.

Detach-Mode

Run A Container Interactively (-it)

When you are looking to perform operations, tasks after creating a container then you enable interactive mode while running a container. The docker provides this with options (-i) for interactive mode and (-t) for accessing the terminal. The command looks as follows:

docker container run -it [docker_image]

mydocker interactive container

Run A Container And Publish Container Ports (-p)

Docker provides an isolated network environments to the containers because of this accessing the containers from outside is not possible by default. They can only accessible from inside the containers. To provide the access from the external environment docker provides port mapping concept with option (-p). On using this option you can map the docker port with the host port. The following command shows the mapping of ports while running a container.

docker container run -p [host_port]:[container_port] [docker_image]
  • Specifying with example the command looks as follows:
docker container run -p 8080:80 [docker_image]

docker-port

Run A Container And Mount Host Volumes (-V)

In Docker, The containers containing is temporary the data will available till the container is alive (not terminated). Once the container get terminated the data inside the container will be lost and can’t be retrivable. To prevent this docker facilitates volume mounting with option (-v).

It mounts the container directory with host directory and maintainces the data over. It helps in preventing data loss and making the data permanent. The command looks as follows:

docker run -v [host_directory]:[container_directory] [docker_image]

Example For Docker Run Volume Mount

  • After specify the directories as an example the command looks as follows:
docker run -dit  -v /mydata:/tmp ubuntu:latest

docker run - volume mount

Run A Docker Container And Remove It Once the Process Is Complete

When a container executes its tasks, after completion of those it stops but the file system of the container remains on the host system. If you want to remove the container after once the container process is completed, you can use the option `–rm` provided by docker. The command looks as follows:

docker run --rm [docker_image]
  • After specify this command as an example, it looks as follows:
docker run --rm ubuntu:latest

run-and-remove-the-container

Example For Docker Run Command With Parameters

  • On specifying the values as an example to the Above docker run command with parameters the command looks as follows:
docker run -it -v /mydata:/tmp -p 8080:80 -e myuser=rajini ubuntu:latest

run-and-remove-the-container-(1)

The docker ps Command

After running the containers with docker run command, you view the launched containers with docker ps command. It list out all the running containers. If you want to see all the containers either it is running or terminated you can use (-a) option. The command looks as follows:

docker ps -a

Docker run Parameters

Till here we have understand and learn how to launch containers and to run containers with more functionality with docker run options/parameters. Now lets look on a few docker run parameters in detail. Running the containers with usage of options, facilitates better functionalities to the containers.

Foreground/Interactive (-it)

The option will be help to provide an interactive mode to the container after launching the docker container. This combinational options provides an interactive terminal for the launched container with docker run. The example looks as follows:

docker run -it alpine:latest

Clean up (–rm)

The clean up functionality helps in removing the container after once its process is done, It helps in removing the unnecessary containers and making resources available to other containers. The example of this looks as follows:

docker run --rm apline echo "hello World"

EXPOSE (Incoming Ports)

Docker Network makes the containers as isolated environments it restrict the incoming traffic from outside. But it is able to excess the outside network. For having access to incoming traffic you have to docker providing Expose option (-p) to expose particular service on that port number. The example looks as follows:

docker run -p 8080:80 ubuntu:latest

Volume Shared Filesystems (-v)

The `-v` option is used for managing the data through enabling the volume mounting. It allows the container to access the specific directories on the host system. It ensures the container data being persistent beyond the container’s lifecycle. Through mounting even if the container restarted the data will be available to access. The option looks as follows:

docker run -v [Host_directory]:[Container_directory]  [docker_image]

ENV Environment Variables (-e)

Using the `–env` or `-e` option, you can pass environment variables to the container, influencing its behavior and configuration. For a container database images such as MySQL , maridb etc.. you need to provide this environmental variables to make the containers to running state. The command looks as follows:

docker run -e MYSQL_PASSWORD=geeksforgeeks -e MYSQL_DATABASE=mydb mysql:latest 

Assigning Name( –name) , Port Number (-p) And Volume Mount (-v) Options

In this Example, I created a container with docker run with options such as detach mode (-d ) , Interactive mode ( -i ) , Terminal mode ( -t ) , ( -v ) Volume binding with host path /mydata and /var/www/html path , ( -p ) port binding with 80 port of container with 80 port of the host system and launched with image httpd on latest version. you can see the status of the container from the command `docker ps` . i.e., on looking the created, status, port columns in the result of `docker ps` command . Now you can know the age of the container , its current status duration and port that where apache is running.

docker run -dit -p 80:80 -v /mydata:/var/www/html --name myapache:latest

docker-run-apache-example

Mount Volume Read Only (–read-only)

On using volumes in combination with the –read-only flag helps in controlling the writing permissions of a file. The following command illustrates it clearly as the read-only flag mounted to container’s root filesystem allowing only to read and prohibiting the write permissions to location other other than the specified volumes for the container.

docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh

Publish All Exposed Ports (-P, –publish-all)

It lets to publish all the exposed ports of a container to the host. Docker binds each exposed port to a random port number available in the host system.

docker run -P nginx:alpine

An Implementation Of Docker Run: A Step-By-Step Guide

In this implementation we will guide you how to create images and create containers in simple and effectively. You can simple think that Docker run execution is a combination of docker container create and container start. Lets discuss the implementation about the execution of this example and how it’s workflow will be.

Example:

docker run -it --name mycontainer1 ubuntu:latest bash 

Step 1: Check If The Image Exists Locally

Firstly it checks the images that are available in the local docker system using the following command in behind:

docker images

If the required ubuntu:latest looking is exists then it go for creating the container as next step or else, the docker images has to be pulled into local system from the public registry or have to provide own custom Image.

Step 2: Create A New Container From The Image

Once the docker image availability verfied it will starts to create the container with that image, for this it uses docker create command in behind for creating the container and names with mycontainer by taking through option –name.

Through this creation phase of container some resources such as hostname, CPU, memory and storage. The container is created with some namespace. It provides the isolated network environment to the container.

Step 3: Start The Container And Run The Container

Once the docker container is started, then as a next phase docker start the container, for this It uses docker start command to start the container. After the docker container is started it uses the enterpoint or command to run a specified program inside it.

Step 4: Attach To The Containe And Print Out The Output To The Console

Now, As final phase of the docker run command it will run the bash program that is specified in the command using the entrypoint, Here you will be landed to the terminal with interactive mode, and can see the bash program in running state.

You can check all these stages will executing the initial specfied example command

docker run -it --name mycontainer1 ubuntu:latest bash 

mycontainer1

People Also Read

Article Name

Article Link

Docker Installation on Linux distributions

Read

Docker Installation on Windows

Read

Docker Installation on Linux MacOS

Read

Conclusion

In conclusion, mastering the `docker run` command is crucial for effective deployment and managing Docker containers. Whether you are launching applications, services, or development environments, understanding the multitude of options available with `docker run`. It will devlops you to support to your containers to specific requirements. Experimentation with the various flags and options will make better understanding on the container workflow in launching the applications or deployments . To know more about other docker commands try on referring Docker Cheat Sheet.

Docker Run Command – FAQ’s

In Docker, What Is The Primary Purpose Of `docker run` ?

`docker run` is used to create and start a docker container with the specified image facilitating the execution of containerized applications.

How To Run A Docker Container In Background ?

Add the ‘-d’ flag to the ‘docker run’ command to run the docker container in background, executing independent of the terminal .

What Are ‘-p’ And ‘-v’ Options In The Docker ?

These are known as port binding and volume binding options that are used with docker run , to map the port numbers and path directories of docker container with the host system.

Example of Port Binding:

docker run -p 80:80  -v /mydata:/var/www/html --name myserver httpd:latest

Can I Change The Values Of Environmental Variables Inside The Docker Container?

Yes , you can set the evironmental variables from the ‘docker run‘ command and those can be changable inside the container from the bash shell .

Can We Create A Container Without Docker Run Command ?

Yes , we can create by using following commands , Firstly run

`docker create --name container_name    image_name`

(or)

`docker container create --name container_name    image_name`

and then run the

`docker start  container_name`

What Is Docker Run Image?

Running a container with docker run command using docker image (A light weight Software) is known as Docker Run Image. The command looks as follows:

docker run [docker_image]

What Is Docker Port Mapping?

Mapping of docker port with its host ports to provide the access to the containers with docker environments is known as Docker Port Mapping.

What Are Docker Run Name?

Providing the name to the container at time of launching is known docker run name. The command looks as follows:

docker run --name mycontainer -dit ubuntu:latest



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads