0

I have installed mysql in centOS and now, want to start the mysql-server. However, I get that error:

# systemctl start mysqld
Failed to get D-Bus connection: Operation not permitted

To fix it, I created a Dockerfile as shown

FROM centos:7
MAINTAINER theodosiostziomakas <mymail@gmail.com>
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"]

And then run it to create an image.

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

But I am still getting the same error.

I also looked at this proposed solution

Any ideas?

Thanks, Theo.

Theo
  • 3,099
  • 12
  • 53
  • 94
  • 2
    Docker images don't typically have systemctl, so that method of starting up applications usually changes when they run in docker. The official mysql image uses a startup script to bootstrap mysql in lieu of an init script. You'll probably want to do this too ( or just use the official mysql image which has been working wonderfully for me). – erik258 Dec 30 '18 at 16:21
  • Hello Dan. I think it's much more than that... – Theo Dec 30 '18 at 16:22
  • @DanFarrell You mean that you start the mysql-server from centOS with the mysql image right? – Theo Dec 30 '18 at 16:26
  • @DanFarrell This one causes the problem: systemctl – Theo Dec 30 '18 at 16:35
  • 3
    You should assume systemd and commands like `systemctl`, `initctl`, and `service` just don't work in Docker. If you goal is to have a MySQL container, the [stock mysql image](https://hub.docker.com/_/mysql) is a fine starting point. Typically you directly run the service, as a foreground command, without any sort of init system. – David Maze Dec 30 '18 at 19:46
  • @DanFarrell. Yes. I run mysql from Docker in my machine as $ docker run --name theo-mysql5 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v /Users/theodosiostziomakas/MySql:/var/lib/mysql -p 3307:3307 -d mysql – Theo Dec 31 '18 at 04:24
  • @DanFarrell Should I worry that mysql-server doesn't start in centOS though? – Theo Dec 31 '18 at 04:25
  • @Theo did you tried my proposed answer. you can the image detail as well – Adiii Dec 31 '18 at 06:01
  • @Adiii Yes it works good. but just for educational purposes i want to make it work with "systemctl start mysqld" command too. I am new to this. – Theo Dec 31 '18 at 06:52
  • @Adiii Also your solution doesn't run in the background. – Theo Dec 31 '18 at 06:54
  • @Theo just add this line to docker file RUN systemctl enable mysqld – Adiii Dec 31 '18 at 07:46
  • 1
    @Theo _I_ wouldn't spend my time getting init systems to run in docker. There's no particularly compelling reason to do it - I'd restart the _container_ rather than logging in and doing traditional service management. I would try to keep it completely decoupled from the host OS and avoid privileged mode. I'd keep my containers ephemeral too, and focus on making sure I understood why when the container went away my data would be retained. I don't really see how init does any more than enable traditional management techniques that don't apply to the docker paradigm. – erik258 Dec 31 '18 at 14:46

2 Answers2

2

I believe the issue with the Dockerfile or with the run command

It seems the issue in you Dockerfile is in this line

RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \

Here is MySQL centos Dockerfile

# Starting from base CentOS image
FROM centos:7

# Enabling SystemD
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" ]

# Enabling EPEL & Remi repo
#RUN yum install -y epel-release && \
#yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm 

# Mysql repo & installion
RUN yum install -y https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm && \
yum install -y mysql mysql-server

RUN chkconfig --level 345 mysqld on
RUN systemctl enable  mysqld


VOLUME [ "/var/lib/mysql" ]

# Port Expose
EXPOSE 3306
CMD ["/usr/sbin/init"]

Now, Next step is to run

--privileged is not enough, you also need to mount cgroup

Here is the command

docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro -it adilm7177/centos-mysql

You can build your own or you can pull the above image from docker registry that i build and push during testing.

docker push adilm7177/centos-mysql:latest

enter image description here

Update:

RUN systemctl enable  mysqld

After adding this I am able to start-stop using systemctl

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • 2
    Clever. But at the end of the day, I wonder what value it adds. As a note to other readers, I'd encourage you to pursue the "one process per container" paradigm rather than trying to make traditional management systems work in docker. – erik258 Dec 31 '18 at 14:49
  • @DanFarrell It is working for me so I will leave it as it is for now. – Theo Jan 01 '19 at 07:24
  • @DanFarrell Also happy new year with lots of successes and happiness. – Theo Jan 01 '19 at 07:47
  • 2
    happy new year to all @Theo – Adiii Jan 01 '19 at 07:50
2

I am able to run mysql just fine with the docker-systemctl-replacement script which emulates "systemctl" commands without an active systemd daemon. You can look at that at the docker-systemctl-images examples.

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19