May 13, 2017

Docker Commands: docker run

Docker Commands: docker run


docker run debian ls -ls

What does the above command do?

When you supply the run argument, the Docker daemon finds the image (debian in this case), creates the container and then runs ls -ls command in that container.

In this case, ls -ls is an argument passed on to the container executable (debian), and you see the following:


Note: If the image does not exist locally, an attempt is made to automatically download it from the repository:


docker run -it alpine /bin/sh
When you run this command, Docker daemon does the following:
  1. Runs the alpine image: If the image exists locally, Docker daemon uses it for the new container. Otherwise, Docker Engine pulls it from a registry, e.g. Docker Hub
  2. Creates a new container: Docker allocates a filesystem and mounts a read-write layer on top of the image.
  3. Configures the container: Creates a network bridge interface and attaches an available IP address from a pool
  4. Executes the starting command: Runs the default executable or in this case, /bin/sh from the command line
  5. Manages the data I/O stream: Connects and logs standard input, output and error streams

Running the run command with the -it flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands in the alpine container.


  1. Running docker ps will show if any containers are currently active (running)
  2. docker images lists images available on the local host: nginx, ubuntu, debian, alpine
  3. With docker run, Docker Engine starts the local alpine image running as a container, in interactive mode (-i) and attaches a TTY device (-t) for I/O. After the container starts, it runs the application, in this case the Linux shell, /bin/sh.
    Note: in some cases, you can combine multiple options (e.g. -i -t) into one (e.g. -it).
  4. A couple of things happening behind the scenes before the prompt:
    • Filesystem allocated and mounted as read-write layer
    • A network is attached. Since no network was specified, the default, bridge network driver interface is created. This allows the container to communicate with the local host
    • Allocates an available IP address from a pool
    • Executes the default executable or the override command, in this case, /bin/sh
    • Enables the standard input, output and error streams to allow I/O access with the container

docker run alpine echo "hello from alpine"

In this case, the Docker daemon starts the alpine container, which runs the echo command with the "hello from alpine" argument. The container then immediately exits.

docker run –name web01 -d -p 8080:80 nginx

Starts the nginx web server, names it web01. This allows it to be identified by this name or the automatically generated 64-character ID. It also maps port 80 of the container to port 8080 of the host machine; exposing port 8080 and allowing access via http://localhost:8080 or http://<ip_address:8080>


Note: Nginx (pronounced "engine-x") is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server). It runs on Linux, BSD variants, macOS X, Solaris, AIX, HP-UX.
The above command create a container environment with its own isolated: process space, network stack and file system, names it web01, starts the nginx application running in detached mode (or as a daemon) and exposes container port 80 as local port 8080.

docker run --rm
Creates a transient container, i.e. the container is removed after it exits. This automatically runs the equivalent of $ docker rm <containerID> after the container is exited.


Creative Commons License Licensed under a Creative Commons Attribution 4.0 International License.

No comments:

Post a Comment