![]() |
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. Table of Content
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 StatusBefore 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:
systemctl enable docker --now
Docker Container RunIf 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: Syntaxdocker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
DescriptionThe 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.
Key Options For Docker Run CommandThe following are the key options for docker run command:
Examples Of Docker Run CommandThe 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 WayDocker 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
Run A Container Under A Specific NameWhen 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]
docker run --name mycontainer centos:latest
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.
docker container run -d [docker_image]
Exampledocker container run -d --name myserver httpd:latest
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]
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]
docker container run -p 8080:80 [docker_image]
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
docker run -dit -v /mydata:/tmp ubuntu:latest
Run A Docker Container And Remove It Once the Process Is CompleteWhen 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]
docker run --rm ubuntu:latest
Example For Docker Run Command With Parameters
docker run -it -v /mydata:/tmp -p 8080:80 -e myuser=rajini ubuntu:latest
The docker ps CommandAfter 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 ParametersTill 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=horje -e MYSQL_DATABASE=mydb mysql:latest
Assigning Name( –name) , Port Number (-p) And Volume Mount (-v) OptionsIn 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
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 GuideIn 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 LocallyFirstly 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 ImageOnce 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 ContainerOnce 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 ConsoleNow, 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.
docker run -it --name mycontainer1 ubuntu:latest bash
Running Interactive vs Detached ContainersThe following are the difference between running interactive vs Detached Containers:
Best Practices of Docker Run CommandThe following are the best practices of docker run command:
Common Usecases of Docker RunThe following are the usecases of Docker Run Command:
People Also Read
ConclusionIn 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 – FAQsIn Docker, What Is The Primary Purpose Of `docker run` ?
How To Run A Docker Container In Background ?
What Are ‘-p’ And ‘-v’ Options In The Docker ?
Can I Change The Values Of Environmental Variables Inside The Docker Container?
Can We Create A Container Without Docker Run Command ?
What Is Docker Run Image?
What Is Docker Port Mapping?
What Are Docker Run Name?
|
Reffered: https://www.geeksforgeeks.org
DevOps |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |