The Docker Quickstart Guide for Developers

This Docker Quickstart Guide for Developers aims to get developers familiar with Docker as quickly as possible.

Providing you with the knowledge to be able to use Docker in your personal and professional projects.

What is Docker?

Docker is a tool written in Golang that provides the ability to run applications within things called containers.

It removes the age-old “works on my machine” problems that plagued many software developers and testers lives.

Understanding the Terminology

Dockerfile: A file that lists what will be installed and available to use for later.

Image: A built Dockerfile located on a specific machine.

Container: A running instance of a built and deployable image. It is a standardized unit of software.

Registry: A location to store and share Docker configurations, images and containers.

Getting a Docker Hub Account

Docker Hub is the default place for where all Docker Registry URLs point.

It is free to get an account, additional features come with paid plans.

Register for an account at https://hub.docker.com/signup .

Installing Docker

To install Docker on your local machine or a server, go to the downloads section of the Docker website.

The <a href="https://github.com/docker/cli" target="_blank" rel="noreferrer noopener nofollow">Docker CLI</a> should now be available in the command-line by typing docker and pressing Enter.

$ docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/Users/ao/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/ao/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/ao/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/ao/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  deploy      Deploy a new stack or update an existing stack
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

Getting Started with Common Docker Commands

Running a Docker Container

Download, install and run the hello-world image to see how it all works.

docker run hello-world
:: if this is the first time you should be able to see the message
:: Unable to find image 'hello-world:latest' locally
:: latest: Pulling from library/hello-world
:: 1b930d010525: Pull complete                                                                                           
::   Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
:: Status: Downloaded newer image for hello-world:latest
::
:: 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/

Running in Interactive Mode / Bash

docker run -it ubuntu bash

See all running images

You can use docker ps to view all currently running images.

docker ps
:: CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS            NAMES

You can also see all the images that have run previously.

docker ps -a
:: CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS     
::          NAMES
:: 4a76281f9c53        hello-world         "/hello"            2 minutes ago       Exited (0) 2 minutes ago             
::          happy_poincare
:: the name part is generated automatically so it probably will be different for you

Remove images

Remove our previously generated image

docker rm happy_poincare

Test if it was really deleted.

docker ps -a
:: CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS            NAMES

Run with custom names

specify a custom name for the container

docker run --name test_container 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/

See it in action

docker ps -a
:: CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS          PORTS          NAMES
:: d345fe1a4f41        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago   test_container

as you can see the name is now what we have specified

Retrieve logs

Retrieve logs from a named container

docker logs test_container
:: 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/

Mount and work with Volumes

Docker provides the ability to share the local filesystem with the filesystem in the running container.

docker run -d --name image-name -v /path/to/app/directory/on/host:/var/www/on/container ubuntu:latest

Now all files changes made locally will be available directly within the running container.

Alternatively, you can specify the mount volume from your actual Dockerfile:

FROM alpine
VOLUME ["/data"]
ENTRYPOINT ["/bin/sh"]

Create an actual volume with docker volume create volume_name.

See what volumes exist with docker volume ls.

Remove a volume with docker volume rm volume_name.

If you don’t want the container to be able to write to the volume, you can mount it in read-only mode by appending a :ro to the container mount location:

docker run -d --name image-name -v /path/to/app/directory/on/host:/var/www/on/container:ro ubuntu:latest

Commit changes from a Throwaway Container

Run your normal docker build process:

docker build -t image-name .

Now run a command in a throwaway container that uses volumes and make any changes:

docker run -v /some:/volume --name temp-container image-name /some/post-configure/command

Replace the original image with the result of the changed container:

(reverting CMD to whatever it was, otherwise it will be set to /some/post-configure/command)

docker commit --change="CMD bash" temp-container image-name 

Finally, delete the temporary container:

docker rm temp-container

Run an Ubuntu container

docker run ubuntu
::  Unable to find image 'ubuntu:latest' locally
::  latest: Pulling from library/ubuntu
::  2746a4a261c9: Pull complete                                                                                         
::                                                        4c1d20cdee96: Pull complete                                                                                                                                                 0d3160e1d0de: Pull complete                                                                                                                                                 c8e37668deea: Pull complete                                                                                                                                                 Digest: sha256:250cc6f3f3ffc5cdaa9d8f4946ac79821aafb4d3afc93928f0de9336eba21aa4
::  Status: Downloaded newer image for ubuntu:latest

Run our new Ubuntu container in interactive mode through Bash.

docker run -it ubuntu
::  root@e2cac48323d2:/# uname
::  Linux

How to write a simple Dockerfile

Create a file called index.html and insert the following code:

<h1>Hello world!</h1>

Create a file called Dockerfile and insert the following code:

FROM busybox
ADD app/index.html /www/index.html
EXPOSE 8005
CMD httpd -p 8005 -h /www; tail -f /dev/null

Now in the command-line, run docker build -t hello-world-demo ..

This tells Docker to build an image which we will call hello-world-demo and to use the Dockerfile located in the current directory to do so (because of the dot, which means ‘local directory’).

Now we can run our newly created image by saying docker run -p 80:8005 hello-world-demo.

This looks for a locally found Docker image called hello-world-demo and runs it. It also port maps our local port 80 to the container’s port 8005, which we exposed in our Dockerfile to show the index.html file through the httpd utility.

Push our Docker image to Docker Hub

It’s great to be able to create images and use them locally, but more often than not, you will want to share them with your colleagues or the greater community. Or perhaps you will store them to run on servers somewhere.

To do this, you will first need to login to Docker from the command-line. This is easy and can be done by running docker login.

Once this is done, we will tag our image to a particular repository on our Docker Hub account.

To do this, type docker tag hello-world-demo {your_dockerhub_user}/hello-world-demo.

Then finally, push it to a specific tag, or the latest tag.

This can be done by typing docker push {your_dockerhub_user}/hello-world-demo:latest.

Final Thoughts

Docker is a fantastic invention and very useful tool that every developer should make use of wherever needed.

With the above few lessons learned, it should now be possible to start using Docker in your day to day development work.

For more on using the Docker CLI, check out Use the Docker Command-Line on the Docker Documentation site.