15

Dockerfile

FROM centos:7 COPY docker-entrypoint.sh /data/ ENTRYPOINT ["/data/docker-entrypoint.sh"] 

docker-entrypoint.sh

exec /usr/sbin/init && systemctl restart autofs %% python /data/myapp.py 

I need to run /usr/sbin/init as the first command then run subsequent commands such as systemctl restart autofs and python myapp.py

I cant seem to get all working inside docker-entrypoint.sh because /usr/sbin/init does not return

if i change docker-entrypoint.sh to

/usr/sbin/init & systemctl restart autofs && python /data/myapp.py 

it fails with error in d-bus

what can i do so the container runs /usr/sbin/init, systemctl restart autofs and python /data/myapp.py in that order when "docker run" is executed?

Is there any other ways to run commands after /usr/sbin/init is executed? i've tried putting systemctl as CMD in Dockerfile

FROM centos:7 ENTRYPOINT ["/usr/sbin/init"] CMD ["systemctl restart autofs"] 

but CMD is never executed

1
  • 3
    The error about d-bus comes from systemctl to have executed. That's because systemctl uses d-bus to talk to the systemd daemon. May be you want to avoid the init/dbus thing with the docker-systemctl-replacement :D Commented Mar 15, 2018 at 23:24

1 Answer 1

23

Declaring

ENTRYPOINT ["/usr/sbin/init"] CMD ["systemctl"] 

Will result in:

/usr/sbin/init systemctl 

In other words, the ENTRYPOINT directive sets the executable which is used to execute the command given from the COMMAND directive.

The default ENTRYPOINT is /bin/sh -c so /bin/sh -c /data/docker-entrypoint.sh should work, if /data/docker-entrypoint.sh contains:

/usr/sbin/init systemctl restart autofs python /data/myapp.py 

That means: You don't have to change the ENTRYPOINT

If you change the the ENTRYPOINT to /data/docker-entrypoint.sh than it should contain something like:

/usr/sbin/init systemctl restart autofs python /data/myapp.py # run the command given as arguments from CMD exec "$@" 

reference

Sign up to request clarification or add additional context in comments.

2 Comments

this cannot work. your shell script will have PID 1 and you need init to be PID 1.
Indeed I've got an error message: "System has not been booted with systemd as init system (PID 1). Can't operate."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.