Docker and systemd

First of all we need docker up and running in your system.I have already setup docker in my system so we can proceed.

First we need a docker image to launch docker container.You can think that Container is the instance of a docker image and we can launch multiple containers from single image.

Put simply we can say it’s simplest to think of a container as a running image, but this isn’t quite accurate.

An image is really a template that can be turned into a container. To turn an image into a container, the Docker engine takes the image, adds a read-write filesystem on top and initialises various settings including network ports, container name, ID and resource limits.

  • To download an docker image

docker pull

  • To list all the downloaded images

docker images

I am using centos image for this tutorial.

Now I am making a directory to store webpages in my host machine (Redhat).

Then we go on to create a demo html file for our webserver inside host machine

Now we will launch the docker container from centos image.The command used is

docker run -it -v /webpages/:/var/www/html -p 8085:80 –name websrv centos

Now let’s understand what all this means.

  • -i is for interactive mode and -t is to get a terminal of the container.
  • -v option is for volume.It basically mount host system directory to docker container.Volumes are the preferred way to persist data in Docker containers and services. Some use cases for volumes include: Sharing data among multiple running containers. If you don’t explicitly create it, a volume is created the first time it is mounted into a container.
  • By default, when you create or run a container using docker create or docker run, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the –publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.-p 8080:80 Maps TCP port 80 in the container to port 8080 on the Docker host.
  • — name to give container a name

You can see container has been lauched and we get a terminal of the container which is centos.

Now let’s setup the webserver in it.These are basic container with very limited commands.So you will not find even common commands like clear or ifconfig.But we can install them manually

Now that we have that out of the way, we can proceed to main part.We are installing httpd package for webserver using

yum install httpd -y

Now let’s check if our webpage is there in /var/www/html

Now all that’s left is to start the webserver services but here’s a catch.Traditional methods like :

service start httpd


systemctl start httpd

will not work here and we get an error.

There is a great reason why it doesn’t work.

It is because the Docker Model suggests that it is better to run a single service within a container. If you wanted to build an application that required an Apache service and a MariaDB database, you should generate two different containers.

Thus docker doesn’t need systemd which is tool for monitoring multiple services.Thus it is disabled by default and to enhance security and isolation to container and its service.But we can enable it which i will talk about later.

So now to start httpd service we need to know how actually systemd starts the services.

If you see Loaded block , it loads /usr/lib/systemd/system/httpd.service file.Now if we go into that file.

We can see it runs /usr/sbin/httpd file to start the services.Now we know how to start the service

This will start our services.Let’s check if our webserver works.To check we need host machine IP and port number we used to bind docker to host which is 8085.

IP of host machine

You can see it’s working fine.

This way we can install any package and run it.Lets try with python.

Now we create simple python program and run it.

This completes our basic tutorial of docker.

Leave a Comment

Your email address will not be published. Required fields are marked *