Docker

A Gentle Introduction

A Common Problem:

"It works on my machine..."

Solutions

  • Vagrant: Virtual Machines
  • Docker: Containers

Virtual Machines are Cumbersome

They use a lot of resources and tend to be very large.

Why use containers?

  • They run in isolated kernel processes.
  • They have a small footprint.
  • The stakes are very low.
  • You can easily share them with your friends
https://www.reddit.com/r/ProgrammerHumor/comments/cw58z7/it_works_on_my_machine/

Some Basic Terminology

  • Image
  • Container
  • Volume
  • Registry

What is an image?

An image is an immutable bundle of "layers" that define a software system.

Images are defined in Dockerfiles.

What is a container?

A container is an instance of an image that is running on a host machine.

What is a volume?

A volume is a storage location that can be shared between docker containers and a host machine.

What is a registry?

A registry is a library of public (or private) images that can be pulled down to a local host machine.

Installation

Docker is available for Windows, Mach and Linux:

https://docs.docker.com/install/

Docker Hub

hub.docker.com is the most popular image registry.

You can find many open source projects hosting docker images on Docker Hub.

There are many others: AWS, GitLab, Azure, etc.

You can also host your own private registry
(using docker!)

"Hello World"


docker run hello-world

Docker Command: "run"


docker run {image-name}

This creates a container from an image.

The 'run' command has a lot of options.

https://docs.docker.com/engine/reference/commandline/run/

Docker Command: "run"


docker run {image-name} {command?}

You can optionally specify a command.

Docker Command: "run"


docker run -i {image-name} {command?}

The '-i' flag stands for "interactive". This will keep the container attached to your terminal session.

Docker Command: "run"


docker run -t {image-name} {command?}

The '-t' flag stands for "pseudo TTY". This will tell docker that you are connecting via a terminal session.

Docker Command: "run"


docker run -it {image-name} {command?}

If you want to interact with a container and see the output it generates you will generally want to use these two flags together.

Docker Command: "run"


docker run --rm {image-name} {command?}

The '--rm' option will automatically remove the container when the command finishes.
(Note the two dashes.)

Docker Command: "run"


docker run -v /host/path:/container/path {image-name} {command?}

The '-v' option will mount a local folder into the container.

Example Usage


// .bash-aliases
carbon() {
  docker run -it --rm -v $(pwd):/src node:carbon /bin/sh -c "cd /src; ${*:-sh}"
}

alias carbon=carbon

This is a handy tool, but there is one problem: File Ownership.

Solution: Let's build an image!

Dockerfiles

A Dockerfile is used to describe an image.

Each line of the docker file represents one "layer" of the image.

An Example Dockerfile

# ~/project/Dockerfile
FROM postgres:9.6

LABEL maintainer="Ryan Durham"

# Copy psqlrc config to the postgres home directory
COPY --chown=postgres:postgres .psqlrc /var/lib/postgresql/

# Install VIM
RUN apt update && apt install vim -y

WORKDIR /var/lib/postgresql

ENV PATH "$PATH:/var/lib/postgresql"

Docker Command: "build"


docker build .

Here we are creating an image from a Dockerfile.
The "." indicates that we want to use the Dockerfile in the current directory.

Docker Command: "build"


docker build -f /path/to/Dockerfile

We can use the '-f' flag to specify a Dockerfile in a different directory.

Docker Command: "build"


docker build . -t my-image:latest

The '-t' option lets us tag the image with a name and a version. This is the name we will use when referencing the image with the "run" command.

Demo: Build a Custom Image

Helpful Tips

  • docker container ls
  • docker image ls
  • docker ps
  • docker system prune

Docker is a very powerful tool - this is just the tip of the iceberg.

Documentation Links

The docker docs are great but they can be a bit overwhelming.

Additional Resources

Questions?

Thank you!

ryan at stagerightlabs dot com

stagerightlabs

Stage Right Labs Logo