docker

2016-11-13

Today I learnt about Docker.

Docker is finally available for macos without needing to run linux inside virtualbox.
Docker is a wrapper around LXC.

LXC is a method for running multiple isolated linux systems ona control host using a single Linux kernel.
LXC basically combines cgroups and kernel namespaces to provide an isolated environment for each container.

How to I use it?

Download docker for your operating system here

After completeing the installation, open your terminal and type docker ps, you should see similar output:

1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Now we’re ready to build our first docker image!

Building a docker image

The thing about Docker images is that they are fast. Spinning up a container is only a couple second’s work.
That’s because most of the complexity has been dealt with during the image building phase.
Building the images can take over 20 minutes in some cases, it comes down to how much work you need to do to bring your image to the desired state.

A popular approach to dealing with this is to delegate the image building to a CI server, which builds the image, runs any necessary checks before finally tagging and pushing it to a docker registry.
Once the image has been tagged, it is super simple to distribute and to run.
Create a folder for the image

Writing the Dockerfile

1
2
mkdir first-docker-image
cd first-docker-image

create the Dockerfile

1
touch Dockerfile

The Dockerfile is teh recipe from which docker will build our image.
The easiest way to get started is to base your image off of an existing one.
For this excercise we will base our image off of the official debian image like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM debian:jessie # Specifies the base image, in this case debian linux!
MAINTAINER Thomas Khalil <[email protected]> # If you plan on making your image publicly available
RUN PACKAGES="\
cowsay \
" \
&& apt-get update -y \
&& apt-get install $PACKAGES -y \
&& apt-get autoremove --purge -y # This RUN block will update the debian repo lists, install the list of packages and the cleanup
ENTRYPOINT ["/usr/games/cowsay","-f","tux"] # the comand that runs once you start a container
CMD ["Running Linux like a boss!!!"] # Exceute a command, in thise case, a string that gets passed to the entry point above

Building the Dockerfile into an image

From the folder where the Dockerfile is saved, run the following command:

1
docker build -t khalilt/cowsay .

where khalilt/cowsay signifies your handle khalilt followed by the image name cowsay

Running the image

1
docker run -it --rm khalilt/cowsay
1
2
3
4
5
6
7
8
9
10
11
12
________
< Running Linux like a boss!!! >
--------
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/