A Gentle Introduction
"It works on my machine..."
They use a lot of resources and tend to be very large.
An image is an immutable bundle of "layers" that define a software system.
Images are defined in Dockerfiles.
A container is an instance of an image that is running on a host machine.
A volume is a storage location that can be shared between docker containers and a host machine.
A registry is a library of public (or private) images that can be pulled down to a local host machine.
Docker is available for Windows, Mach and Linux:
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!)
docker run hello-world
docker run {image-name}
This creates a container from an image.
The 'run' command has a lot of options.
docker run {image-name} {command?}
You can optionally specify a command.
docker run -i {image-name} {command?}
The '-i' flag stands for "interactive". This will keep the container attached to your terminal session.
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 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 run --rm {image-name} {command?}
The '--rm' option will automatically remove the container when the command finishes.
(Note the two dashes.)
docker run -v /host/path:/container/path {image-name} {command?}
The '-v' option will mount a local folder into the container.
// .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!
A Dockerfile is used to describe an image.
Each line of the docker file represents one "layer" of the image.
# ~/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 build .
Here we are creating an image from a Dockerfile.
The "." indicates that we want to use the Dockerfile in the current directory.
docker build -f /path/to/Dockerfile
We can use the '-f' flag to specify a Dockerfile in a different directory.
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.
Docker is a very powerful tool - this is just the tip of the iceberg.
The docker docs are great but they can be a bit overwhelming.
Questions?
ryan at stagerightlabs dot com