Explaining the Docker run Command

Explaining the Docker run Command
Techiio-author
Written by Nilima PaulFebruary 12, 2022
11 min read
Docker
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's Explaining the Docker run Command.

Introduction to Docker run Command:

The 'docker run' order is utilized to run or begin an order in another holder which implies it makes a writeable layer on top of the referenced picture in the order. That is the reason we call a holder is a writeable picture. This is the principal order that we run while begin learning Docker. There are a lot of choices accessible with this order to arrange the holder according to our necessity like running a compartment in disengaged mode or intelligent mode, determining a name to the compartment, appending network, volume, and so forth, presenting port to get to the compartments' application remotely, and a lot more things.

Syntax:

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

How to run Command Works in Docker:

At the point when we run the 'docker run' order on the terminal, Docker daemon looks for the referenced Docker picture locally, and if it finds the picture locally, it makes a writeable layer over the predefined Docker picture and starts the compartment utilizing determined order and on the off chance that it doesn't observe the picture locally then it firsts pull that picture from the library and naturally, it goes to 'hub.docker.com' assuming there is no neighborhood vault referenced in the daemon.json record.

Examples of Docker run Command:

Here are the following examples mentioned below

Example #1:

Let’s run our first container ‘hello world without specifying any options as shown below:

docker run hello-world

Explanation: In the above snapshot, we can see that the ‘hello-world’ image was not available locally so the daemon first pulled that image locally and ran the container and the ‘hello-world’ container has shown some data on STDOUT and exited.

Example #2:

Let’s give a name to the container using the ‘–name’ option as below:

docker run –name nginx-con Nginx

Explanation: In the above example, created an Nginx container named ‘Nginx-con’. If we don’t use the ‘–name’ option then the daemon randomly picks any name and is assigned to the container. Also, we can see that the cursor is stuck and we did not get the terminal because the container is running in the foreground. We use ‘ctrl+c’ to exit the container, however, it stops the container as well.

Example #3:

Let’s run the container in detached mode to overcome the above issue using the ‘-d’ option as shown below:

docker run -d --name nginx-con2 Nginx

Explanation: In the above example, we only got the container ID because the container is now running in the background and also this time daemon did not pull the image because the Nginx image is already available locally.

Example #4:

Now, set environment variables using the ‘-I option as below:

docker run -d -e "USER=test1" --name nginx-con Nginx

Explanation: In the above example, we have used the ‘-I option to set an environment variable USER to test1. Now, if we want to verify that the USER environment variable created inside the container or not, we can connect to the container using the ‘exec’ command with ‘-i’ and ‘-to options together as shown below: –

docker exec -it nginx-con sh
#echo $USER

Explanation: In the above snapshot, we have connected to the container with its name, however, we can use container ID as well and the container must be running to connect to it.

Example #5:

We can also use the above options with the ‘docker run’ command as shown below:

docker run -it -e "USER=test1" --name nginx-con Nginx sh

Explanation: In the above example, we can see that we have not used the ‘-d’ option otherwise it will run the container in the background and will not be able to connect to the container which means we cannot use the ‘-d’ and ‘-it’ options together.

Example #6:

Let’s assume we have to create an Nginx container named ‘my-Nginx with the below configuration:

  • Attach a volume named my-vol
  • Attach a network named my-net
  • Restrict memory utilization to 256 MB
  • Open port 80 to access Nginx externally
  • Set restart policy to unless-stopped
  • Set the hostname of the container to ‘my-Nginx

Here is the ‘docker run’ command to achieve the above objective: –

$ docker run -it -v my-vol:/root --network=my-net -m 256m -p 80:80 --restart=unless-stopped -h my-Nginx --name my-Nginx Nginx sh

Explanation: In the above example, we have used the ‘-it’ option to connect to the container directly to verify the hostname otherwise we can use the ‘-d’ option. Note: – Network must exist before creating the container. We can create a simple network using the below command:

docker network create my-net

Example #7:

Sometimes we create a container for testing purposes and the container is no longer required after testing so we can use the ‘–rm’ option to delete the container once it is stopped. Here is the command:

docker run --it --rm ubuntu sh

Explanation: In the above snapshot, we can see that there is no container running before and after the creation of the ‘ubuntu’ container because once we exit from the container, it is removed. We can use it for testing the connection between two containers using the ping command directly in the command line.

Advantages:

  • The 'docker run' is a short order to run a holder when contrasted with the 'docker compartment run' order.
  • It has a lot of choices to arrange the holders according to our prerequisites.

Rules and Regulations for using a run Command:

  1. Docker network should exist before appending to any compartment utilizing the 'docker run' order.
  2. Docker volume gets made on the off chance that it doesn't exist as of now while connecting to any holder.
  3. There is inconsequential to utilize the '- d' and '- it' choices together.
  4. We can involve a similar port for the different holder yet not at the host level, for instance, we can make numerous nginx compartments and open port 80 on every holder except we would not uncover these compartments remotely on the similar port, we can have to uncover every holder on an alternate port to get to it remotely.
  5. A few choices just work with a particular variant of Docker so it is prescribed to take a look at the similarity before utilizing that choice.

Conclusion:

The 'docker run' order has a lot of choices yet not every one of the choices is utilized much of the time so we have just examined the most often utilized choices. You can peruse official documentation to know every one of the choices or utilize the '- help' choice to get every single accessible choice.

Docker
Docker run command
DevOps
Docker file
Containers
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance