DEV Community

Pratik Pathak
Pratik Pathak

Posted on • Originally published at pratikpathak.com on

Docker Run Command example the ultimate cheatsheet

Docker Run Cheat sheet

Hey Everyone, we will talk about different commands of docker; you can get the list of all the commands at the end of this article.

The First command which we are going to talk about is docker run command.

Docker Run Command in Detail:

The docker run Command is used to run a container using an image file. If the image file is not available locally it will then check it on Docker Hub if it is available then it will download it and then it will create the container.

The Syntax for the Docker run command is:

docker run imageName:version command
Enter fullscreen mode Exit fullscreen mode

Here,

  • imageName: is the name of the image file
  • Version: also known as tag. Generally, this tag is used to specify the version of the image file, eg, MySQL:5.1
  • Command: is used to specify any arguments which you may need to specify when running a container. It can be a command which you want to run inside the container. This can be easily customized using ENTRYPOINT or/and COMMAND value of the docker file of the image file.

If you don’t specify the version, it will assume it is “latest”

Docker Run command has many flags let’s go through them one by one

Flags of Docker Run Command :

Docker run –name Name Flag in Docker –

–name flag is used to specify the name of the container, An Example of the docker run name command will be:

docker run --name myUbuntu ubuntu
Enter fullscreen mode Exit fullscreen mode

Docker run -d Detached Flag in Docker –

-d stands for detached, it is used to specify to run the container in the background.

An example of docker run -d is:

docker run -d imageName
Enter fullscreen mode Exit fullscreen mode

Docker run -it Interactive Flag in Docker –

docker run -it is used to specify to connect in interactive mode, basically, it will attach the container terminal to your terminal so that you can run commands inside the containers

An example of docker run it command is:

docker run -it imageName
Enter fullscreen mode Exit fullscreen mode

Docker run -p Port Flag in Docker –

-p is known as the port flag, this is used to expose the port of the container, we can also say that it is used to specify the port number of a host which needs to be linked with the port number of the container.

The syntax for Docker Run Port Command:

docker run -p hostPort:containerPort imageName
Enter fullscreen mode Exit fullscreen mode

Note: You need to specify the Host Port first and then the container Port

let’s take an example to understand It better. Suppose you have a flask web app image file (named “flask-web-app”) which runs on Port 5000 in the container, and you want to access this application, but you cannot since it’s by default denied, To access the application you need to expose the port, still exposing the port is not enough you need to map it will host port else it will be only accessible with container IP and accessing via container IP is very complex so you map the container port to host port, let’s say we map 5000 port to 8080, The Final Command will look like this

An example of the docker run p command is:

docker run -p 8080:5000 flask-web-app
Enter fullscreen mode Exit fullscreen mode

Docker run –rm Port Flag in Docker –

Docker run –rm is used to specify to auto-remove the container, and its file as soon as it stops. This is done to free up the space consumed by unwanted, unused containers.

An example of the docker run –rm command is:

docker run --rm imageName
Enter fullscreen mode Exit fullscreen mode

Docker run -e Environmental variable Flag in Docker –

We all know for running instances we need some credentials, and these credentials are stored in an environmental variable. The container expects these credentials in the form of environmental variables. You can feed these environmental variables using -e flag

docker run -e variable=value imageName
Enter fullscreen mode Exit fullscreen mode

Example of docker run -e:

docker run -e example

Docker run -v Volume map Flag in Docker –

By default, data in the container doesn’t get saved, data exist as long as the container is alive as soon as the container stops, data gets lost. To tackle this issue, you can specify docker to save data in external storage so that even if the container stops working, your data will still exist, and you can create another container using this data. Also, by doing this you can also make the various container to be in sync.

The syntax for volume mapping is:

docker run -v volumePath:containerDataPath image
Enter fullscreen mode Exit fullscreen mode

Example of docker volume mapping:

docker run -v

Note: if the volume does not exist, then “docker run -v” will create the volume, and then it will map

Top comments (0)