Skip to content

Instantly share code, notes, and snippets.

@reedsa
Last active January 13, 2017 20:43
Show Gist options
  • Save reedsa/597762df463e550c78a088f45a33f0c7 to your computer and use it in GitHub Desktop.
Save reedsa/597762df463e550c78a088f45a33f0c7 to your computer and use it in GitHub Desktop.
Introduction to Docker

Introduction to Docker

What is Docker?

Simplifies how applications are run by isolating the processes in separate containers. Typically run one application process per container. Each container includes all application dependencies and libraries. Different flavors: Docker for Mac, Docker for Windows, Docker on Linux, Docker Toolbox

What is an image?

There are common public images available from a registry like DockerHub. These include things like nginx, MongoDB, and Redis. Images are made up of layers and all images have a base layer (Which might be the OS). Images enable a simple method of distribution and allow users to save each layer so they won’t need to be downloaded the next time the container is run. An image can be tagged with versions.

The Docker Registry

DockerHub is a registry that maintains Docker images. Images can be created and pushed to the registry with a version tag.

What is a Container?

A container can run an application on any hardware or platform. Each container is an instance of a Docker image.

How is a container different from a VM?

Containers are not lightweight VMs. The fundamental difference is the architecture.

VMs provide a fully self contained environment and operating system. Provisioning a VM with the OS and software is often slow and error prone.

Docker containers are based on a shared infrastructure across the Docker host and each uses the exact amount of space necessary.

Containers are very disposable since they are usually designed to be immutable and stateless. Any stateful data is usually stored in a shared volume.

Containers can be started up very quickly and so the cost of stopping any running container and starting new ones is very low.

A container is much smaller and portable. Containers are easy to configure and install.

Running a Docker container

$ docker run ubuntu /bin/echo ‘Hello world’

‘Docker run’ runs a container. This command uses a ubuntu image to run a container and executes the echo command inside the container. If the ubuntu image is not available from the local Docker host it will be pulled from the public DockerHub registry.

$ docker run -it ubuntu /bin/bash

Like before, this runs a container from a ubuntu image. The ‘-i’ makes an interactive connection using the standard input of the container. The ‘-t’ assigns a terminal inside the new container. ‘/bin/bash’ launches bash inside the container.

Exiting the container will stop it. Run the container in the background as a daemon to keep it running:

$ docker run -d ubuntu /bin/sh -c “while true; do echo hello world; sleep 1; done”

Looking at running containers:

$ docker ps

Check the Docker logs:

$ docker logs <container_name>

Stop the container:

$ docker stop <container_name>

Looking at all containers:

$ docker ps -a

Looking at all images:

$ docker images

Pull an existing image:

$ docker pull training/sinatra

Updating and committing images:

$ docker run -t -i training/sinatra /bin/bash

Run a container, install stuff, then commit the changes.

root@<containerId>$ apt-get install -y ruby2.0-dev ruby2.0
root@<containerId>$ gem2.0 install json
root@<containerId>$ exit
$ docker commit -m "Added json gem" -a "Stuart Reed" <containerId> sareed/sinatra:v2
$ docker run -t -i sareed/sinatra:v2 /bin/bash

This is painful. Make it easier with Dockerfiles!

The Dockerfile

A set of instructions that build up a Docker image.

Dockerfile
# This is a comment
FROM ubuntu:14.04
MAINTAINER Stuart Reed <[email protected]>
RUN apt-get update && apt-get install -y ruby ruby-dev
RUN gem install sinatra

Build the image with docker build:

$ docker build -t sareed/sinatra:v2 .

Run the container for the new image:

$ docker run -t -i sareed/sinatra:v2 /bin/bash

Tag the image:

$ docker tag <imageId> sareed/sinatra:devel

Push to DockerHub:

$ docker push sareed/sinatra

Remove an image:

$ docker rmi sareed/sinatra

Data Volumes

Data that is saved during a container's execution is destroyed when the container exits. Volumes are special directories in containers that are separate from the Union File System that can persist or share data. Also supports mounting shared storage such as NFS.

Create a data volume inside a container

$ docker run -d -P --name web -v /webapp training/webapp python app.py

Find the volume on the host by listing the 'source' path

$ docker inspect webapp

Mount a host directory as a data volume

$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py

Also have the ability to create Data Volume Containers to persist data and share between containers, such as database data that can be backed up or restored.

Docker Compose

Run multiple Docker containers simultaneously. Can link containers together and run dependencies in a specific order.

Great for creating multiple isolated environments with different project names. Container caching so that only services that have changed will rebuild and restart. Supports using variables in the config for injecting environment variables and tags. For production, typically use a separate config (like docker-compose-production.yml) When used with Docker Swarm, a distributed cluster can be implemented to make a pool of Docker hosts act like a single Docker host.

On the bleeding edge of docker-compose is the idea of bundles, which is basically a distributable application bundle for all configured services.

More Information

Most of this content is covered in more detail at https://docs.docker.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment