Startup script after init in centos 7

I create a Dockerfile that build image based on centos7. In order to get systemd working, following doc is applied

Dockerfile looks like content below

FROM centos:centos7

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" ]
...
ADD startup-script /startup-script
RUN chmod +x /startup-script
...
CMD ["/usr/sbin/init"]

With this, docker build -t docker-image works fine. However where to place the startup script after /usr/sbin/init?

CMD ["/usr/sbin/init", “/startup-script”] won’t execute /startup-script. Changing CMD to[…] ENTRYPOINT ["/startup-script"] with /startup-script containing

#!/bin/bash
/usr/sbin/init
...
commands or operations (which are not something like (re)start service but some pre- post- operations) to be executed 
...
exec "#@"

doesn’t work either.

How can I execute custom startup script after executing /usr/sbin/init in docker?

Thanks

1 Like

@clouderafn did you find out a way to do that ? I am a newbie and stuck with same issue
Would be great help if you could share as to what you did .
Thanks

or you cam email me on shrenikchhajed1@gmail.com

Hi.

I’m sorry for the translation, I speak Spanish and I’m using google translate.

Hope this can help you.

what I do is launch a script in CMD or ENTRYPOINT, inside of which I have put a secondary script in the background after the instruction /usr/sbin/init, hopefully sytemd ends before and is ready to be used.

FROM centos:centos7

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" ]
COPY ./start /opt/start.sh
CMD["/opt/start.sh"]

start.sh

#!/bin/bash

# script set in background
setsid ./install.sh > output.txt &

# run systemd
exec /usr/sbin/init
1 Like

I also speak Spanish, I use google translator.
Could you use systemctl in centos 7 container?
When I try to user, I get the error

systemctl restart httpd
Failed to get D-Bus connection: Operation not permitted

Thanks @daners, that approach works beautifully.

Thanks @daners, this really works. Finally!!! I was looking for this solution for such long time. Ill go back to the hundreds of forums I went through to recommend your aproach

@daners thank you so much :pray: I spent nearly 3 days to find a solutions for this issue and your solution really helped me :slight_smile: thank you once again.

thank you so much !!!