0

I can't seem to convince the cron (or any other) service to start with my Ubuntu container. I'm sure I'm missing something simple.

I've tried using both update-rc.d and /etc/rc.local but no joy.

In this example Dockerfile I'm also trying to get rsyslog running in order to debug, with the same result- the service does not start.

# syntax = docker/dockerfile:experimental

FROM ubuntu:18.04


RUN apt-get update && apt-get install --reinstall -y \
    rsyslog

# fix for https://stackoverflow.com/questions/56609182/openthread-environment-docker-rsyslogd-imklog-cannot-open-kernel-log-proc-km
RUN sed -i '/imklog/s/^/#/' /etc/rsyslog.conf

# run at startup
RUN update-rc.d rsyslog defaults

# ...really run at startup
RUN echo "service rsyslog start" >> /etc/rc.local

RUN apt-get update && apt-get install -y \
    cron

# run at startup
RUN update-rc.d cron defaults

# ...really run at startup
RUN echo "service cron start" >> /etc/rc.local

ENTRYPOINT ["/bin/bash"]

When I run the container:

root@e392a9404e0f:/# service --status-all
 [ - ]  cron
 [ ? ]  hwclock.sh
 [ - ]  procps
 [ - ]  rsyslog
root@e392a9404e0f:/# service cron start
 * Starting periodic command scheduler cron                                                                             [ OK ] 
root@e392a9404e0f:/# service --status-all
 [ + ]  cron
 [ ? ]  hwclock.sh
 [ - ]  procps
 [ - ]  rsyslog
Matt Savage
  • 327
  • 4
  • 11
  • Docker containers don't typically run init systems or `/etc/init.d` scripts; running `service` commands is likely to fail in several ways. A container will generally run _one_ process as a foreground process and absolutely nothing else; that process usually isn't an interactive shell. – David Maze May 11 '20 at 11:12

2 Answers2

1

Add CMD to your Dockerfile:

CMD ["/sbin/init"]

As init, the program is responsible for system initialization. Read more about init here

WSMathias9
  • 669
  • 8
  • 15
  • Thanks- I've learned something, but still stuck. The `ubuntu:18.04` base image seems pretty pared down. There's no `/sbin/init` and indeed no `/lib/systemd/systemd` , just a `/lib/systemd/system` directory, which has no obvious executable in it, but does at least contain files for `rsyslog` and `cron`. – Matt Savage May 11 '20 at 09:14
  • By changing the puzzle to "why doesn't `/sbin/init` exist?" this got to a solution (see accepted answer). Much appreciated. – Matt Savage May 11 '20 at 09:41
0

systemd is not in the ubuntu docker images, by design

This is because its role as PID 1 is effectively replaced by docker's entrypoint

For more, see:

https://stackoverflow.com/a/39169889/1892116

So the correct solution is what I thought was a hack. In my entrypoint.sh I have:

service rsyslog start
service cron start
Matt Savage
  • 327
  • 4
  • 11