Technology Security Analyst
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.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
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.
Here are the following examples mentioned below
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.
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.
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.
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
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.
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.
Let’s assume we have to create an Nginx container named ‘my-Nginx with the below configuration:
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
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.
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.
Subscribe to get latest updates