Docker

31 March 2016

This post includes simple tutorial about installing and using Docker for running web applications in separated containers at CentOS/RHEL 7. This tutorial also covers using certificates and routing requests to web applications according domain name.

Installation of Docker

Docker is part of CentOS repositories and it can be easily installed using yum:

$ yum -y install docker

When Docker is installed, then it is necessary to enable and start Docker service:

$ systemctl enable docker
$ systemctl start docker

Note: starting of Docker service can take a while.

When Docker service is running, then you can test running simple application in Docker container:

$ docker run hello-world

It will should print following to your console:

3f12c794407e: Pull complete 
975b84d108f1: Pull complete 
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
Status: Downloaded newer image for docker.io/hello-world:latest


Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

Basic documentation about Docker can be found at: https://docs.docker.com/linux/, but keep in mind that this documentation is Ubuntu based. We will use CentOS 7 based Docker images. For this reason it is also good to visit following page: [https://hub.docker.com//centos/](https://hub.docker.com//centos/)

When we launched application in Docker container, then it also created Docker image. We can list Docker images using:

$ docker images

Output of this command could look like this:

REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/hello-world       latest              975b84d108f1        5 months ago        960 B

Creating Docker Image for Web Application

First of all we will create basic image that will be used by own containers:

$ docker pull centos:7

It is possible to run something more ambitious in CentOS 7 container now:

$ docker run -it centos:7 bash

Because we will be using systemd based services it is practical to create following Docker file:

FROM centos:7
MAINTAINER "you" <your@email.here>
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
  rm -f /lib/systemd/system/multi-user.target.wants/*;\
  rm -f /etc/systemd/system/*.wants/*;\
  rm -f /lib/systemd/system/local-fs.target.wants/*; \
  rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
  rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
  rm -f /lib/systemd/system/basic.target.wants/*;\
  rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

Then create new image from this Dockerfile:

$ docker build --rm -t local/c7-systemd .

We have Docker image and we can

comments powered by Disqus