Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start service using systemctl inside docker container

In my Dockerfile I am trying to install multiple services and want to have them all start up automatically when I launch the container. One among the services is mysql and when I launch the container I don't see the mysql service starting up. When I try to start manually, I get the error: Failed to get D-Bus connection: Operation not permitted

Dockerfile:

FROM centos:7

RUN yum -y install mariadb mariadb-server

COPY start.sh start.sh

CMD ["/bin/bash", "start.sh"]

My start.sh file:

service mariadb start

Docker build:

docker build --tag="pbellamk/mariadb" .

Docker run:

docker run -it -d --privileged=true pbellamk/mariadb bash

I have checked the centos:systemd image and that doesn't help too. How do I launch the container with the services started using systemctl/service commands.

like image 497
Premchand Avatar asked Oct 17 '17 23:10

Premchand


People also ask

How do I run Systemctl inside docker?

Build the container image: docker build . -t sysd. Start a container: docker run --tmpfs /tmp --tmpfs /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 9090:80 --name sysd --rm sysd. To get a shell inside the container execute the following command in a second shell: docker exec -it sysd bash.

How do I access a service inside a container?

To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. If you have a service running on some other port you can access it via 172.17. 42.1:5432.


4 Answers

When you do docker run with bash as the command, the init system (e.g. SystemD) doesn’t get started (nor does your start script, since the command you pass overrides the CMD in the Dockerfile). Try to change the command you use to /sbin/init, start the container in daemon mode with -d, and then look around in a shell using docker exec -it <container id> sh.

like image 196
Dima Spivak Avatar answered Oct 18 '22 02:10

Dima Spivak


Docker is designed around the idea of a single service/process per container. Although it definitely supports running multiple processes in a container and in no way stops you from doing that, you will run into areas eventually where multiple services in a container doesn't quite map to what Docker or external tools expect. Things like moving to scaling of services, or using Docker swarm across hosts only support the concept of one service per container.

Docker Compose allows you to compose multiple containers into a single definition, which means you can use more of the standard, prebuilt containers (httpd, mariadb) rather than building your own. Compose definitions map to Docker Swarm services fairly easily. Also look at Kubernetes and Marathon/Mesos for managing groups of containers as a service.

Process management in Docker

It's possible to run systemd in a container but it requires --privileged access to the host and the /sys/fs/cgroup volume mounted so may not be the best fit for most use cases.

The s6-overlay project provides a more docker friendly process management system using s6.

It's fairly rare you actually need ssh access into a container, but if that's a hard requirement then you are going to be stuck building your own containers and using a process manager.

like image 44
Matt Avatar answered Oct 18 '22 02:10

Matt


You can avoid running a systemd daemon inside a docker container altogether. You can even avoid to write a special start.sh script - that is another benefit when using the docker-systemctl-replacement script.

The docker systemctl.py can parse the normal *.service files to know how to start and stop services. You can register it as the CMD of an image in which case it will look for all the systemctl-enabled services - those will be started and stopped in the correct order.

The current testsuite includes testcases for the LAMP stack including centos, so it should run fine specifically in your setup.

like image 21
Guido U. Draheim Avatar answered Oct 18 '22 04:10

Guido U. Draheim


I found this project:

https://github.com/defn/docker-systemd

which can be used to create an image based on the stock ubuntu image but with systemd and multiuser mode.

My use case is the first one mentioned in its Readme. I use it to test the installer script of my application that is installed as a systemd service. The installer creates a systemd service then enables and starts it. I need CI tests for the installer. The test should create the installer, install the application on an ubuntu, and connect to the service from outside.

Without systemd the installer would fail, and it would be much more difficult to write the test with vagrant. So, there are valid use cases for systemd in docker.

like image 26
espakm Avatar answered Oct 18 '22 03:10

espakm