Docker Containers | Docker Images

Docker | Install and Use

Docker is a revolutionary and disruptive technology that saves money, enhances security and encourages flexible and responsive IAAS (infrastructure as a service) architectures.

Computer scientists would say it implements the immutable infrastructure pattern, from which most of its benefits emanate.

Install Docker CE on Ubuntu 16.04 and 17.04

The simplest (most future proofed) Docker Community Edition install is to bundle the below into a script.

  • sudo apt-get update
  • curl -fsSL | sudo apt-key add -
  • sudo apt-key fingerprint 0EBFCD88
  • sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
  • sudo apt-get update
  • sudo apt-get --assume-yes install docker-ce
  • docker --version

Let’s install a Jenkins docker container to test-drive key docker features.

Provision a Jenkins Docker Container

  • pull jenkins » sudo docker pull jenkins
  • run jenkins » sudo docker run -d -p 49001:8080 -v $PWD/docker.jenkins:/var/lib/jenkins -t jenkins
  • now login using » http://jenkins-host:49001

Docker | Images vs Containers

A human being is a classyou are an object (a member) of the class. That goldfish over there is an object belonging to the fish class.

Classes are templates that hold attributes and behaviour covering all instantiated members of that class. And don’t forget the inheritance hierarchy.

Class | Docker Image

A docker image is a class – a template – that defines the properties (attributes) and behaviour of instantiated objects (containers) of the image class. Docker image classes are stored in a repository.

sudo docker images » will list the images (classes) that you have in your local repository cache.

REPOSITORY          TAG         IMAGE ID         CREATED         SIZE
docker.image-repo   latest      0a06b95c53fb     2 hours ago     1.02GB
none                none        1807b681c279     2 hours ago     681MB
jenkins             latest      0b4d4d677a26     2 weeks ago     681MB

Use sudo docker history --human e292276df6f2 to dig into the history of an image.

Object | Docker Container

A docker container is an instantiated member (object) of the image class. The container is duty bound to imitate the properties and behaviour defined by the image class it belongs to.

sudo docker ps -a » will list the docker containers that have been instantiated locally with an IMAGE ID denoting the parental image class.

CONTAINER ID   IMAGE          COMMAND                  CREATED           STATUS                      
58a43e95fd68   0a06b95c53fb   "/bin/tini -- /usr..."   40 minutes ago    Exited (1) 40 minutes ago   
d87d09ebe1bd   0a06b95c53fb   "/bin/tini -- /usr..."   About an hour ago Exited (1) About an hour ago
bda9f0bd822a   0a06b95c53fb   "/bin/tini -- /usr..."   2 hours ago       Exited (1) 2 hours ago      
fd48fd51f32d   d57a197f3224   "/bin/sh -c 'apt-g..."   2 hours ago       Exited (100) 2 hours ago    
d53828b0f3d5   1807b681c279   "/bin/sh -c 'sudo ..."   2 hours ago       Exited (127) 2 hours ago    
6b45a9c17083   1807b681c279   "/bin/sh -c 'apt-g..."   2 hours ago       Exited (100) 2 hours ago    

This sudo docker ps -a --no-trunc is great when you need to see the full run command or container ids.

Dockerfile | The Perfect Girl

Finding the perfect image is like finding the perfect girl – it’s rare. Take for example the official Jenkins image – it’s great but Maven is not installed in it.

Dockerfile exists to smooth out the small imperfections. If your Dockerfile is too big – you’ve probably not got the right image. Try and keep your Dockerfile to ten commands or less.

Dockerfile | Creating the Perfect Image

You use a Dockerfile to tweak (extend) imperfect middleware service docker images. In JAVA (OO) you extend a class to tweak behaviour and Dockerfile follows the same object oriented principle.

Use the FROM image directive to declare the image you wish to extend.

A Dockerfile allows you to install software, copy files and folders in and out of the container, bind a container volume to the host, and set environment variables.

When your Dockerfile is ready – you use docker build to create the image, docker push to store it in a public or private docker registry, docker pull to bring it back down (say on another host) and docker run to fire up the container.

# --- -------------------------------------------- --- #
# --- Dockerfile to Provision Jenkins CI Container --- #
# --- -------------------------------------------- --- #

FROM jenkins

USER root
RUN apt-get update && apt-get install --assume-yes maven

WORKDIR /var/jenkins_home
USER jenkins

COPY ~/docker.jenkins.plugins/plugins/* plugins/
COPY ~/jenkins_user/* users/

ENTRYPOINT ["/usr/local/bin/"]

The above Dockerfile extends the official jenkins image and installs Maven as root. Then as the jenkins user it copies necessary plugins and installs a new admin user.

  • extend official image » FROM jenkins
  • assume the root user » USER root
  • install maven » RUN apt-get update && apt-get install –assume-yes maven
  • set pwd (for COPY cmd) » WORKDIR /var/jenkins_home
  • assume the jenkins user » USER jenkins
  • copy config into image » COPY ~/jenkins_user/* users/
  • kick start service » ENTRYPOINT ["/usr/local/bin/"]

The advantage of Dockerfiles is

  • you can version control them
  • no need for DockerHub registry
  • they are lightweight and can be passed around
  • you can get the latest images with security patches
  • you can configure an exact image version
  • no need to upload images to foreign clouds

For our purposes, keeping security patches up to date outweighs the risk of tiny changes between images.

Dockerfile | Adding a User With Sudoer Permissions

To create a user with sudoer permissions from a Dockerfile requires you to install sudo before creating the user. This Dockerfile illustrates the concept.

# --- ----------------------------------------------------- --- #
# --- Dockerfile to add User apollo with Sudoer Permissions --- #
# --- ----------------------------------------------------- --- #
FROM ubuntu:16.04

RUN apt-get update && \
      apt-get -y install sudo

RUN useradd -m apollo && echo "apollo:apollo" | chpasswd && adduser apollo sudo

USER apollo
CMD /bin/bash

Commands | Docker Images

Docker commands that operate on images are

  • list all images » sudo docker images
  • delete image » sudo docker rmi <<- image ->>
  • remove all images » sudo docker rmi $(sudo docker images -q)
  • image roadmap » sudo docker history --human <<- image ->>

Commands | Docker Containers

The most popular container-centric Docker commands are

  • list running containers » sudo docker ps
  • list all containers » sudo docker ps -a
  • list container ids » sudo docker ps -aq
  • remove all stopped containers » sudo docker rm -vf $(sudo docker ps -aq)
  • container logs » sudo docker logs <<- container ->>
  • copy from container » sudo docker cp naughty_leakey:/var/jenkins_home/plugins ./docker.jenkins.plugins
  • execute command » sudo docker exec -t -i <<- container ->> <<- command ->>
  • container ip address » sudo docker exec -t -i <<- container ->> ip addr
  • login to container » sudo docker exec -t -i <<- container ->> /bin/bash
  • stop docker container » sudo docker stop <<- container ->>
  • remove a container » sudo docker rm -vf <<- container ->>

Commands | Docker Volumes

The most popular volume-centric Docker commands are

  • list all volumes » sudo docker volume ls
  • remove unused volumes » sudo docker volume prune
  • volume attributes » sudo docker volume inspect <<- volume ->>

Commands | Docker Host

The most popular docker commands are listed.

  • host resources eg ram » sudo docker info
  • docker host disk space » sudo du -sh /var/lib/docker

Leave a Reply

Your email address will not be published. Required fields are marked *