Mastering Docker, Chapter 2: Up and Running

Dockerfile

The goal of this section will be
  • structure dcokerfile in most practical and efficient manner
  • best practises
  • allow us to read dockerfiles

A short review of dockerfile

  • from: image to be used
  • maintainer: the person who maintains the file
  • run: fetch and install packages and other commands
  • add: add files or folders to your docker image
  • expose: exposes ports to the outside world
  • cmd: executes the command and keeps the container alive

Reviewing dockerfile in depth

Label: used to add information to the file
Add or Copy: add will download a file from an url and unpack/untar it.  Copy will add files, but it   won't download from an url or unpack/untar it
Entrypoint: is similar to the CMD instruction, but doesn't keep the command alive
User: allows you to specify the user to run the command
Workdir: sets the working directory
Onbuild: lets you stash commands for when the image is used as a base image

Dockerfile - Best Practices

  • use dockerignore file
  • minimize the number of packages to install
  • keep the file as small as possible
  • use a new container for each file
  • sort commands alphabetically when possible so they are easier to find and change later

Docker build

In this section we learn how to use the docker build command.

The docker build command

Use
docker build --help
 to see all the flags you can use with the build command
The important ones are
  • -f name of the dockerfile to build
  • -t repo name to use
The author also makes note of limiting cpu and memory when building a docker you can use
  • -cpu-period or cpu-quote
  • -m memory limiting swith

.dockerignore

The ignore file in docker is used to exclude any files that you don't want to include in the build

Building images using Dockerfile

The author goes over the command to create an image using docker.  He notes to make sure there is a period at the end, so the container will be built in the current directory.  I looked and looked, but could not find the period in the commands.  This could just be a problem with my copy of the book

Building a base image using an existing image

There are two ways to build a base image.  The easiest is to use one of the official builds from the Docker Hub.  You can also use an image from another provider.

I have very few criticisms of this book, but this is one place where I feel the author really let us down.  He doesn't do a good job explaining the difference between images from Docker Hub and other places.  This section needs some more work

Building your own containers

  •  use tar
  • use a scratch image

Using tar

You can convert an existing machine (bare metal or virtual machine) into a docker container.  You can do this by installing debootstrap.  Get the release name of the distro you are using, and the using docker import to create a new image.

Using scratch

Docker has a blank image called scratch.  It allows you to build exactly what you need to get up and running.  It will allow you to build the smallest image possible

Docker Hub

You can store your images in various places
  • public or private cloud
  • Docker Subscription
  • inside your git project

The Docker Hub location

To store your images on Docker Hub run,
docker login
docker push <repo:tag>

Public repositories

These repos are open to anyone and can be searched via
docker search 
or via the Docker Hub website

Private Repositories

Private repos are as they sound, only you or others you specify that can use them

Docker Hub Enterprise

This will allow you to run your own local or cloud repo of docker images.  You can either run your own image or pay Docker where they will make sure you get the updates you need.

Environmental Variables

Docker allows the use of Environment Variables.  These can be used for creating database users, passwords, and databases, plus many other uses you might possibly need.

Using environmental variables in your Dockerfile

There are two ways to use environment variables
env key value
This allows one variable to be used
env key=value
By using the equal (=) sign, you can use multiple variables per line.  inspect allows you to see the variables.  You can change them at run time by using 
run -e key=value

Creating a MySQL username, database, settings permissions

This section shows how to set up a MySQL database using env variables.

Adding a file to the system

This section shows how to use a file to set up memcached.  This can be used to set up an type of thing you would need to use files to set parameters at run time.

Docker Volumes

One thing of note is that docker containers do not persist data.  To do that we need to use data volumes or data volume containers.

Data Volumes

When using data volumes the data lives on you local machine.  Use the -v switch.  I don't feel this section was well explained.  It needs a lot more information to tell you what exactly is going on.  I think that the Data Volumes are stored on your local machine under some specific file structure, but the author does not explain this well.  Here is a blog post that I think does a better job explaining it.

Data Volume Containers

Allows us to share our data volumes to share data between containers.  Using the
--volumes-from  
You can remove these data volumes and containers using the following command
docker rm -v <dir>
Be careful, this will delete all data from the volume, and it will be gone.

Docker volume backups

Containers are immutable, the data in them is not.  Therefore always make sure you have good backups.




Comments

Popular posts from this blog

Go Programming Blueprints, Chapter 2, Adding User Accounts

Successful Big Game Hunting Chapter 10