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.

Docker Commands: docker images

Docker Commands: docker images


$ docker images

Lists all images on the local host


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

Docker Commands: docker help

Docker Commands: docker help


docker run --help

See a list of all flags supported by the run argument.

In general, you can append the --help option to any Docker command for more information, e.g. docker --help or docker <command> --help


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

Docker Commands: docker pull

Docker Commands: docker pull

docker pull <image>

Docker will connect to the Docker Hub and attempt to pull, i.e. download and install, an <image> locally.

E.g. docker pull ubuntu downloads and installs the latest version of the image named ubuntu from Docker Hub.


Note: The above command downloads the most up-to-date version of ubuntu image, or to be technically correct, it pulls (or downloads) from the Docker Hub, the ubuntu image that is either untagged or has the tag latest.

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

Docker Hub: Tags

Docker Hub: tags


To see a list of tags or version identifiers associated with an image, connect to Docker Hub:


and see its Description, e.g.:


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

Installation Requirements

Installation Requirements


Platform:
  • Linux:
    • Any distribution running version 3.10+ of the Linux kernel
    • Specific instructions are available for most Linux distributions, including RHEL, Ubuntu, SuSE, and many others.
  • Microsoft Windows:
    • Windows Server 2016
    • Windows 10
  • Cloud:
    • Amazon EC2
    • Google Compute Engine
    • Microsoft Azure
    • Rackspace
References:

May 08, 2017

Docker Word Cloud

Docker Word Cloud 28 12 24 36 12 16 28 20 18 20 104 112 144 20 60 56 24 156 144 128 36 140 148 40 196 112 64 56 296 232 72

Be sure to comment, follow and/or recommend this blog!

WordClouds.com was used to create this Docker Word Cloud.

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