docker_script

Install

curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-1.9.1 && chmod +x docker-1.9.1 && sudo mv docker-1.9.1 /usr/local/bin/docker

Run daemon

sudo /usr/local/bin/docker daemon

Run client

docker info

Docker Images vs Docker Containers

Images

docker images
docker search linux
docker pull ubuntu
docker images

This does not cover how to create your own images

Containers

docker run ubuntu echo Hello World
docker ps
docker ps -a
docker logs $IMAGE_NAME

Running a container with a custom name

docker run --help | less
docker run --name HelloWorld ubuntu echo Hello World
docker ps -a
docker inspect HelloWorld | less
docker rm HelloWorld

Running an interactive container

docker run --name ubuntu_shell -t -i  ubuntu bash
exit
docker start ubuntu_shell
docker exec ubuntu_shell ifconfig
docker exec -t -i ubuntu_shell bash
exit
docker ps
docker attach ubuntu_shell
exit
docker ps
docker rm ubuntu_shell

Running a web application with docker

docker run --name web -d -P training/webapp python app.py
# See https://github.com/docker-training/webapp
docker ps # Note randomly assigned port mapping
docker port web 5000
docker rm -f web
docker run --name web -d -p 5000:5000 training/webapp python app.py
docker port web 5000
curl --noproxy localhost http://localhost:5000/
docker logs web
docker top web
docker rm -f web

Network containers

Check existing docker networks

docker network ls
docker network inspect bridge

Create new network

docker network create -d bridge privatenet
docker network inspect privatenet

Start DB container in privatenet

docker run -d --net=privatenet --name db training/postgres  # See https://github.com/docker-training/postgres

Start Web container in bridged network

docker run --name web -d -p 5000:5000 training/webapp python app.py
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

Check IP Addresses of each container

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

Check DB container can’t access web container

docker exec -it db bash
ping $WEB_IP_ADDRESS
exit

docker network connect privatenet web
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker exec -it db bash
ping $WEB_PRIVATE_NET_IP_ADDRESS
ping web
exit
docker rm -f web db

Mounting volumes in containers

Volume characteristics

  • Initialized when a container is created
  • Can be shared and reused among containers
  • Changes to a data volume are not included when an image is updated
  • Data volumes persist even if the container is deleted

Sharing data between container and host

mkdir myfiles
echo Hello > myfiles/hello
docker run -it --name ubuntu -v $PWD/myfiles:/myfiles ubuntu bash
ls -l /myfiles
exit
docker inspect ubuntu | less # search for Mounts

Sharing data between containers

TODO

docker_script

Docker Hello World Example

Some quick notes on how to get up and running with Docker.

Installation

On Fedora this turned out to be pretty easy, simply:

dnf install docker

For other systems see the Docker installation manual.

Start docker demon

systemctl status -l docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2015-12-12 22:40:41 GMT; 3min 35s ago
Docs: http://docs.docker.com
Main PID: 5818 (docker)
CGroup: /system.slice/docker.service
└─5818 /usr/bin/docker daemon --selinux-enabled

Dec 12 22:40:01 asterix docker[5818]: time="2015-12-12T22:40:01.803909914Z" level=error msg="WARNING: No --storage-opt dm.thinpooldev specified, using loopback; this configuration is strongly discouraged for production use"
Dec 12 22:40:40 asterix docker[5818]: time="2015-12-12T22:40:39.995085846Z" level=warning msg="Docker could not enable SELinux on the host system"
Dec 12 22:40:40 asterix docker[5818]: time="2015-12-12T22:40:40.059743984Z" level=info msg="Option DefaultDriver: bridge"
Dec 12 22:40:40 asterix docker[5818]: time="2015-12-12T22:40:40.059788786Z" level=info msg="Option DefaultNetwork: bridge"
Dec 12 22:40:40 asterix docker[5818]: time="2015-12-12T22:40:40.388329409Z" level=info msg="Firewalld running: true"
Dec 12 22:40:41 asterix docker[5818]: time="2015-12-12T22:40:41.278276562Z" level=info msg="Loading containers: start."
Dec 12 22:40:41 asterix docker[5818]: time="2015-12-12T22:40:41.278585203Z" level=info msg="Loading containers: done."
Dec 12 22:40:41 asterix docker[5818]: time="2015-12-12T22:40:41.278606222Z" level=info msg="Daemon has completed initialization"
Dec 12 22:40:41 asterix docker[5818]: time="2015-12-12T22:40:41.278627443Z" level=info msg="Docker daemon" commit="cb216be/1.8.2" execdriver=native-0.2 graphdriver=devicemapper version=1.8.2-fc22
Dec 12 22:40:41 asterix systemd[1]: Started Docker Application Container Engine.

An interesting warning about no –storage-opt db.thinpooldev specified. This is related to Docker’s storage driver, which the docs explain in good detail.

Run Hello World

First, some info about the docker installation

docker info

It’s better to follow along the official and pretty good step by step documentation on basic docker usage, but the basics are below:

Run a single command

[root@asterix ~]# docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world

Interactive shell

[root@asterix ~]# docker run -t -i ubuntu:14.04 /bin/bash
root@8aed75e824e2:/# hostname
8aed75e824e2
root@8aed75e824e2:/# exit
exit
[root@asterix ~]#

Restart a container that was previously running

docker ps -a
docker restart $CONTAINER_ID
docker exec -it $CONTAINER_ID /bin/bash

Copy file from container to host

docker cp $CONTAINER_ID:$CONTAINER_PATH $HOST_PATH

Under the covers

Basic information about the newly created image:

[root@asterix ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/ubuntu 14.04 d55e68e6cc9c 4 days ago 187.9 MB

Local docker containers are stored in /var/lib/docker

[root@asterix ~]# ls -l /var/lib/docker/
total 36
drwx------ 5 root root 4096 Dec 12 23:09 containers
drwx------ 5 root root 4096 Dec 12 22:50 devicemapper
drwx------ 7 root root 4096 Dec 12 22:50 graph
-rw-r--r-- 1 root root 5120 Dec 12 23:09 linkgraph.db
-rw------- 1 root root 114 Dec 12 22:50 repositories-devicemapper
drwx------ 2 root root 4096 Dec 12 22:50 tmp
drwx------ 2 root root 4096 Dec 12 22:49 trust
drwx------ 2 root root 4096 Dec 12 22:40 volumes

And we can see what looks like a ext4 filesystem:

[root@asterix ~]# file /var/lib/docker/devicemapper/devicemapper/data
/var/lib/docker/devicemapper/devicemapper/data: Linux rev 1.0 ext4 filesystem data, UUID=6d11e0b5-f063-4a4f-99f4-36253b12b297 (extents) (large files) (huge files)
Docker Hello World Example