4

I'm using an Alpine flavor from iron.io. I want to auto-run a trivial 'blink' script as a service when the Docker image starts. (I want derivative images that use this as a base to not know/care about this service--it'd just be "inherited" and run.) I was using S6 for this, and that works well, but wanted to see if something already built into Alpine would work out-of-the-box.

My Dockerfile:

FROM iron/scala
ADD blinkin /bin/
ADD blink /etc/init.d/
RUN rc-update add blink default

And my service script:

#!/sbin/openrc-run

command="/bin/blinkin"

depend()
{
    need localmount
}

The /bin/blinkin script:

#!/bin/bash

for I in $(seq 1 5);
do
    echo "BLINK!"
    sleep 1
done

So I build the Docker image and run it. I see no output (BLINK!...) My script is in /bin and I can run it, and that works. My blink script is in /etc/init.d and symlinked to /etc/runlevels/default. So everything looks ok, but it doesn't seem as anything has run.

If I try: 'rc-service blink start' I see no "BLINK!" outbut, but do get this:

 * WARNING: blink is already starting

What am I doing wrong?

Greg
  • 10,696
  • 22
  • 68
  • 98
  • How are you booting your container? There is neither a `CMD` or `ENTRYPOINT` in your Dockerfile, so by default nothing will happen without an explicit command. What do you expect to be starting the openrc init system? – larsks Mar 01 '16 at 00:21
  • 1
    I usually just docker run ... /bin/bash. Is there a cmd to start OpenRC? I assumed (falsely?) it starts by itself as pid 1 or something. – Greg Mar 01 '16 at 03:48
  • 2
    Your command (`/bin/bash` in this case) is PID 1 for your container. If you want your container to run an init system, you need to arrange to run `/sbin/init` when the container boots. I don't think that openrc functions well in a containerized environment (it requires privileges), but s6 works just fine. – larsks Mar 01 '16 at 04:17
  • I fear you may be right. I changed to: ENTRYPOINT["/sbin/init","/bin/bash"] and this ran init fine but it quickly choked with lots of permissions-related errors (even when I set USER root). I was hopeful it might work out-of-the-box because OpenRC came installed with the iron.io images, but it would seem not. Back to S6 I go then! – Greg Mar 01 '16 at 04:26
  • @larsks what will be best way to start deamon in dockerized environment then, S6? – andilabs Nov 29 '18 at 14:41
  • 1
    @andilabs the best solution is to not run a process supervisor inside your container. If you have multiple services to run, use multiple containers. If you have more question, probably your best bet is to post a question because the comments here aren't really the place for an extended discussion. – larsks Nov 30 '18 at 03:27
  • @larsks please have a look on https://stackoverflow.com/questions/53557531/the-proper-way-to-run-django-rq-in-docker-microservices-setup – andilabs Nov 30 '18 at 16:20
  • Services log use syslogger look at "journalctl -f" or "sudo dmesg" – 3Qn Aug 17 '21 at 14:35

1 Answers1

1

You may find my dockerfy utility useful starting services, pre-running initialization commands before the primary command starts. See https://github.com/markriggins/dockerfy

For example:

RUN wget https://github.com/markriggins/dockerfy/releases/download/0.2.4/dockerfy-linux-amd64-0.2.4.tar.gz; \
    tar -C /usr/local/bin -xvzf dockerfy-linux-amd64-*tar.gz; \
    rm dockerfy-linux-amd64-*tar.gz;


ENTRYPOINT dockerfy 
COMMAND --start bash -c "while true; do echo BLINK; sleep 1; done" -- \
    --reap -- \
    nginx 

Would run a bash script as a service, echoing BLINK every second, while the primary command nginx runs. If nginx exits, then the BLINK service will automatically be stopped.

As an added benefit, any zombie processes left over by nginx will be automatically cleaned up.

You can also tail log files such as /var/log/nginx/error.log to stderr, edit nginx's configuration prior to startup and much more

Mark Riggins
  • 195
  • 1
  • 3
  • 5