ealeon
ealeon

Reputation: 12462

Docker: error when running init and systemctl in shell script for CMD

Dockerfile

# Use Centos7 or RHEL7 base image
FROM centos:7

# This steps are needed so that systemd works within container
#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/*;

COPY startup.sh /usr/local/bin/startup.sh
CMD ["/usr/sbin/init"]

startup.sh

#!/bin/bash
systemctl restart autofs

Now, I can run the following without a problem

docker build -t mycentos
docker run -d --privileged --name mycentos mycentos
docker exec -it  mycentos /bin/bash
./usr/local/bin/startup.sh

I would like that startup.sh to run automatically without me having to go into a container and run it manually

So, I just rolled /usr/sbin/init into startup.sh and changed Dockerfile CMD

CMD ["/usr/local/bin/startup.sh"]

and

startup.sh is now

#!/bin/bash
/usr/sbin/init
systemctl restart autofs

I am getting the error

Couldn't find an alternative telinit implementation to spawn.
Failed to get D-Bus connection: Operation not permitted

Any idea how to get startup.sh to work?

PS I remember seeing the same error message when /usr/sbin/init was not running and i treid to run systemctl

EDIT I've changed startup.sh

#!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init

And it looks like autofs is never started from "docker run" CMD

$docker run --rm -itd --privileged --name mycentos mycentos
0c977e677897fc9a42bd3a4efe6742fbb14ed888a010cfee94c604436729db2d
$ docker exec -it mycentos /bin/bash
[root@0c977e677897 /]# systemctl status autofs
\u25cf autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Upvotes: 4

Views: 6505

Answers (1)

Adiii
Adiii

Reputation: 60074

As you did not install autofs and the 2nd thing you did not enable it in docker file. you to enable it in Dockerfile first.

Here is your Dockerfile with a bit modification and the startup script as same as your updated one.

startup.sh

 #!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init

Dockerfile

 FROM centos:7

# This steps are needed so that systemd works within container
#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/*;
RUN yum install -y autofs
COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh
RUN systemctl enable autofs
CMD ["/usr/local/bin/startup.sh"]

NOTE: This also sometimes needs to mount cgroup for systemctl.

-v /sys/fs/cgroup:/sys/fs/cgroup:ro 

Here run the command in a docker container

enter image description here

enter image description here

For further information, you can check this GitHub.

https://github.com/whyistheinternetbroken/docker-centos7-nfs-client-autofs/blob/master/Dockerfile

Upvotes: 3

Related Questions