Erik
Erik

Reputation: 14750

Why does container does't execute scripts inside /etc/my_init.d/ on startup?

I have the following Dockerfile:

FROM phusion/baseimage:0.9.16

RUN mv /build/conf/ssh-setup.sh /etc/my_init.d/ssh-setup.sh

EXPOSE 80 22

CMD ["node", "server.js"]

My /build/conf/ssh-setup.sh looks like the following:

#!/bin/sh

set -e

echo "${SSH_PUBKEY}" >> /var/www/.ssh/authorized_keys

chown www-data:www-data -R /var/www/.ssh
chmod go-rwx -R /var/www/.ssh

It just adds SSH_PUBKEY env to /var/www/.ssh/authorized_keys to enable ssh access.

I run my container just like the following:

docker run -d -p 192.168.99.100:80:80 -p 192.168.99.100:2222:22 \
   -e SSH_PUBKEY="$(cat ~/.ssh/id_rsa.pub)" \ 
   --name dev hub.core.test/dev

My container starts fine but unfortunately /etc/my_init.d/ssh-setup.sh script does't get executed and I'm unable to ssh my container.

Could you help me what is the reason why /var/www/.ssh/authorized_keys doesn't get executed on starting of my container?

Upvotes: 1

Views: 2416

Answers (3)

Azeven
Azeven

Reputation: 138

I had a pretty similar issue, also using phusion/baseimage. It turned out that my start script needed to be executable, e.g.

RUN chmod +x /etc/my_init.d/ssh-setup.sh

Note:

I noticed you're not using baseimage's init system ( maybe on purpose? ). But, from my understanding of their manifesto, doing that forgoes their whole "a better init system" approach.

My understanding is that they want you to, in your case, move your start command of node server.js to a script within my_init.d, e.g. /etc/my_init.d/start.sh and in your dockerfile use their init system instead as the start command, e.g.

FROM phusion/baseimage:0.9.16

RUN mv /build/conf/start.sh /etc/my_init.d/start.sh
RUN mv /build/conf/ssh-setup.sh /etc/my_init.d/ssh-setup.sh

RUN chmod +x /etc/my_init.d/start.sh
RUN chmod +x /etc/my_init.d/ssh-setup.sh

EXPOSE 80 22

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

That'll start baseimage's init system, which will then go and look in your /etc/my_init.d/ and execute all the scripts in there in alphabetical order. And, of course, they should all be executable.

My references for this are: Running start scripts and Getting Started.

Upvotes: 2

James Moser
James Moser

Reputation: 506

As the previous answer states you did not execute ssh-setup.sh. You can only have one process in a Docker container (that is a lie, but it will do for now). Why not run ssh-setup.sh as your CMD/ENTRYPOINT process and have ssh-setup.sh exec into your final command, i.e.

exec node server.js

Or cleaner, have a script, like boot.sh, which runs any init scripts, like ssh-setup.sh, then execs to node.

Upvotes: 0

luanbuingoc
luanbuingoc

Reputation: 178

Because you didn't invoke /etc/my_init.d/ssh-setup.sh when you started your container. you should call it in CMD or ENTRYPOINT, read more here

RUN executes command(s) in a new layer and creates a new image. E.g., it is often used for installing software packages.
CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs.
ENTRYPOINT configures a container that will run as an executable.

Upvotes: -1

Related Questions