Table of Contents
docker run command
Docker Container is a running instance of an image. You can create, run, start, stop or delete the docker containers. The
docker run command creates runnable containers from the images specified and it can also run the commands inside the docker container.
Now, in this article let us understand how to run the docker container using the Docker CLI. The basic syntax of the docker run command is as follows
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Let us run a few docker containers and understand how docker run works. Let us get started.
$ docker run alpine
Upon running the command the output is as shown below -
Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine 213ec9aee27d: Pull complete Digest: sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad Status: Downloaded newer image for alpine:latest
The docker pulls the alpine image and downloads it on the host machine.
Example-1: Using docker run -i
The -i argument is used to keep STDIN open even if not attached. So you have an option to run some commands inside the container but non-interactively. For example:
~]# echo test | docker run -i alpine:3.6 cat test ~]# echo test; echo hello | docker run -i alpine:3.6 cat test hello
Example-2: Using docker run command interactively
Previously, we saw that upon running the alpine container it immediately shut down the container. What if you want to keep the container running so that you could enter the container and run a few more commands?
To do this, docker provides an option that can be used with the docker run command. Let us take a look with ane example.
Let us run the ubuntu docker container with the below command.
$ docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 301a8b74f71f: Pull complete Digest: sha256:7cfe75438fc77c9d7235ae502bf229b15ca86647ac01c844b272b56326d56184 Status: Downloaded newer image for ubuntu:latest root@97541b3ec2b5:/#
The docker run command will pull the docker image from the public docker registry in case the image is not present on the host. It creates a writable layer on top of the image specified and starts the container and executes /bin/bash.
The docker lets us run your container in an interactive mode. The option
-i -t refers to running the docker container in an interactive mode and attaching it to the terminal by which you can execute any command and the output is shown on your terminal.
In the above command output, you can see that you have entered the container and to come out of the container you can type exit by which the container stops running. In order to access it, you can start again using
$ docker start <container_name>
When the user runs the docker container using
$ docker run the docker container process will have its own filesystem, and the network host fully isolated from the host. The user can specify a specific network or use the default one.
Example-3: Attach STDIN and.or STDERR with docker run -i
Additionally we can use
-a argument to attach either STDIN or STDERR to our container or you can attach BOTH. If you do not specify
-a then Docker will attach to both stdout and stderr . You can specify to which of the three standard streams (STDIN, STDOUT, STDERR) you’d like to connect instead, as in:
~]# docker run -a stdin -a stdout -i -t simuonalpine sh / #
Docker has been one of the best containerization tools and I hope this article helped you get a better overview of the docker components and understand how to run the docker container.
In case you have any doubts, please feel free to add your questions in the comment section below and I shall try to respond at the earliest.