Note

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Package:Docker

From Funtoo
Revision as of 13:29, April 12, 2015 by Pytony (talk | contribs) (Added ebuild-header)
Jump to navigation Jump to search

Docker

   Tip

We welcome improvements to this page. To edit this page, Create a Funtoo account. Then log in and then click here to edit this page. See our editing guidelines to becoming a wiki-editing pro.


Docker Fun

What is Docker?

Docker is a platform built around linux containers to provide the possibility to build, ship and run distributed applications in containers. It allows users to quickly deploy their applications atop of linux system of their choice in a safe environment. For more information see understanding docker.

Installation

   Note

Docker with default use flags needs 'thin' use flag to be set for 'sys-fs/lvm2' ebuild in order to emerge.

(host)# echo 'sys-fs/lvm2 thin' >> /etc/portage/package.use
(host)# emerge app-emulation/docker
Calculating dependencies... done!
[ebuild  N     ] dev-lang/go-1.3.1  USE="-emacs -vim-syntax -zsh-completion"
[ebuild  N     ] dev-libs/libaio-0.3.110  USE="-static-libs {-test}"
[ebuild  N     ] dev-vcs/mercurial-3.1.1-r1000  USE="-bugzilla -emacs -gpg {-test} -tk" PYTHON_ABIS="2.7 -2.6"
[ebuild  N     ] dev-util/boost-build-1.55.0-r1000  USE="python -examples {-test}"
[ebuild  N     ] dev-libs/boost-1.55.0-r1000  USE="icu nls python threads -c++11 -context -debug -doc -mpi -static-libs -tools" PYTHON_ABIS="2.7 3.3 -2.6 -3.1 -3.2 -3.4 (-3.5)"
[ebuild  N     ] sys-block/thin-provisioning-tools-0.4.0  USE="{-test}"
[ebuild  N     ] sys-fs/lvm2-2.02.103  USE="lvm1 readline static static-libs thin -clvm -cman -lvm2create_initrd (-selinux) -udev"
[ebuild  N     ] app-emulation/docker-1.1.0  USE="contrib device-mapper -aufs -btrfs -doc -lxc -vim-syntax -zsh-completion"
...snip...

If everything went well and no errors appeared during the installation, you're almost ready to go. The only thing that is left is to run the docker daemon.

root # to run docker daemon just once
(host)# rc-service docker start
root # to run docker on each boot
(host)# rc-update add docker default

Playing with Docker

   Tip

If you want to run docker as regular user, add yourself to docker group

Getting images

The first thing you need is a base image. Base image is a minimal container with an os. You can browse available images on docker hub or through the commandline.

(host)# docker search busybox
NAME                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
busybox                      Busybox base image.                             60        [OK]
progrium/busybox                                                             8                    [OK]
lgsd/diamond                 Diamond is the smallest lightweight Docker...   5                    [OK]
... snip ...
flynn/busybox                Busybox from Ubuntu 13.10 with libc             0

Let's get the official busybox image.

(host)# docker pull busybox
Pulling repository busybox
37fca75d01ff: Download complete
a9eb17255234: Download complete
d200959a3e91: Download complete
fd5373b3d938: Download complete
511136ea3c5a: Download complete
42eed7f1bf2a: Download complete
1f5049b3536e: Download complete
120e218dd395: Download complete
c120b7cab0b0: Download complete
f06b02872d52: Download complete

Running in container

With the image on your drive, you can give it a spin.

(host)# docker run -t -i busybox
   Note

-t means you want to allocate a tty for the container -i means the container should be interactive

You are now presented a shell running in the container. You can try looking around. We will now create a new file in the container and leave it.

root ##i## echo 'My hovercraft is full of eels.' > /message
root # exit

Listing containers

When the process running in the container ends, the container is stopped. If we were to execute the docker run command again, our message file wouldn't be there, because docker run creates a new container from the base image and runs it. To resume our container, we have to find it's name.

(host)# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                     PORTS               NAMES
33dca35dce2a        busybox:buildroot-2014.02   "/bin/sh"           6 minutes ago       Exited (0) 2 seconds ago                       clever_goldstine
   Note

-a lists all containers, even stopped ones. By default only running are shown

Resuming containers

My busybox container is named clever_goldstine, yours will differ. With this name, we can start the container again.

(host)# docker start clever_goldstine
(host)# docker attach clever_goldstine

You are in the container you ran earlier and your file should still be there.

root # cat /message
My hovercraft is full of eels.

Stopping containers

(host)# docker stop clever_goldstine
clever_goldstine

Commiting changes

If we consider our changes worth keeping, we can commit them and this resulting image can server as base image for another container. We can also specify the repository and tag for the container.

(host)# docker commit clever_goldstine busybox:message
a168a8241324f5c98cc3c5ffdb42a44aa0d0f67bba03a6e27ebbc566108aeebe

(host)# docker images
REPOSITORY           TAG                   IMAGE ID            CREATED             VIRTUAL SIZE
busybox              message               ee7b96d88faf        6 seconds ago       2.433 MB
busybox              buildroot-2013.08.1   d200959a3e91        3 months ago        2.489 MB
busybox              ubuntu-14.04          37fca75d01ff        3 months ago        5.609 MB
busybox              ubuntu-12.04          fd5373b3d938        3 months ago        5.455 MB
busybox              latest                a9eb17255234        3 months ago        2.433 MB

As you can see, our container is now prepared as an image for further usage in repository busybox with tag message.

Using Dockerfile

To create an image identical to our busybox:message, one could use the Dockerfile. Dockerfile is a 'recipe' on how to make an image. It contains a series of instructions which are one by one applied to the FROM image. Using Dockerfile is recommended over the run-change-commit workflow.

(host)# cat > Dockerfile <<-EOF
FROM busybox:latest
RUN echo 'My hovercraft is full of eels.' > /message
EOF
(host)# docker build -t busybox:message .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM busybox:latest
 ---> a9eb17255234
Step 1 : RUN echo 'My hovercraft is full of eels.' > /message
 ---> Running in 4473ad720fa4
 ---> 6c9bb6eeb7ec
Removing intermediate container 4473ad720fa4
Successfully built 6c9bb6eeb7ec

The image should be identical (in contents) to our previous busybox:message one.

Deleting containers

clever_goldstine was a trusty companion but time has come the bid farewell.

(host)# docker rm clever_goldstine
clever_goldstine
   Tip

To remove all containers, issue docker rm $(docker ps -a -q)

Deleting images

The image we created is cool and all, but pretty much useless so we can delete it.

(host)# docker rmi busybox:message
Untagged: busybox:message
Deleted: 6c9bb6eeb7ecc32418fe7f42bd317c9b15e2b1f9f2aa7cf15ac6dd24700225d0
   Tip

To remove all images, issue docker rmi $(docker images -q)

Playing with Docker for the brave

Volumes

Docker can attach a directory from the host machine to the container. We can specify a volume for the docker run command in the form of -v /local/directory:/container/directory.

(host)# mkdir /tmp/docker-volume
(host)# echo 'Some content' > /tmp/docker-volume/message
(host)# docker run -v /tmp/docker-volume:/opt/data -t -i busybox

root # ls /opt/data
message
root # cat /opt/data/message
Some content