In this article we will learn the basics of the Docker run command.
Docker is a platform for developers and sysadmins to build, run, and share applications with containers. The use of containers to deploy applications is called containerization. Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own private filesystem; this filesystem is provided by a Docker image.
An image includes everything needed to run an application – the code or binary, runtimes, dependencies, and any other filesystem objects required.
2. Run Command
Docker runs processes in isolated containers. A container is a process which runs on a host. The host may be local or remote. When an operator executes
docker run, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.
The basic docker run command takes this form:
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
The docker run command must specify an IMAGE to derive the container from. With the
docker run [OPTIONS] an operator can add to or override the image defaults set by a developer. And, additionally, operators can override nearly all the defaults set by the Docker runtime itself. The operator’s ability to override image and Docker runtime defaults is why run has more options than any other
docker command. To learn how to interpret the types of
[OPTIONS], see Option types.
Depending on your Docker system configuration, you may be required to preface the
docker run command with
sudo. To avoid having to use
sudo with the
docker command, your system administrator can create a Unix group called
docker and add users to it.
Now let’s see an example. Let us download the Hello World example from Docker Hub. Make sure you have downloaded the Docker and it is running. Run the below command to download the hello-world image:
docker pull hello-world
You will see similar output as below:
Using default tag: latest latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323 Status: Downloaded newer image for hello-world:latest docker.io/library/hello-world:latest
Run docker image ls to list the hello-world image that you downloaded to your machine.
~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB
Now execute the
run command to run the image:
~$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
2.1 Detached mode
By default the container is run in foreground. If you want to run the container in background you can use the detached mode. To start a container in detached mode, you use
-d=true or just
-d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the
--rm option. If you use -d with –rm, the container is removed when it exits or when the daemon exits, whichever happens first.
~$ docker run -d hello-world 050d37d9a6a8d53ff58374cf2457db776f70276cb3e9d994eae8c23e86ba7495
To do input/output with a detached container use network connections or shared volumes. These are required because the container is no longer listening to the command line where
docker run was run. To reattach to a detached container, use
docker attach command.
In foreground mode (the default when
-d is not specified),
docker run can start the process in the container and attach the console to the process’s standard input, output, and standard error. It can even pretend to be a TTY (this is what most command line executables expect) and pass along signals.
While not strictly a means of identifying a container, you can specify a version of an image you’d like to run the container with by adding
image[:tag] to the command. For example,
docker run ubuntu:14.04.
In this article we learned about Docker run command. We looked at the different options available for the run command and how to use them. Then we looked at a very simple example of downloading an image from the Docker Hub and running it.