Skip to content

Commit 28e27b2

Browse files
author
Julien Neuhart
committed
now using supercronic instead of cron
1 parent 7913392 commit 28e27b2

File tree

10 files changed

+143
-24
lines changed

10 files changed

+143
-24
lines changed

Dockerfile.10

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633

Dockerfile.10-apache

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633
# |--------------------------------------------------------------------------

Dockerfile.6

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633

Dockerfile.6-apache

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633
# |--------------------------------------------------------------------------

Dockerfile.8

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633

Dockerfile.8-apache

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1010
# |
1111

1212
RUN apt-get update &&\
13-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
13+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
14+
15+
# |--------------------------------------------------------------------------
16+
# | Supercronic
17+
# |--------------------------------------------------------------------------
18+
# |
19+
# | Supercronic is a drop-in replacement for cron (for containers).
20+
# |
21+
22+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
23+
&& SUPERCRONIC=supercronic-linux-amd64 \
24+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
25+
&& curl -fsSLO "$SUPERCRONIC_URL" \
26+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
27+
&& chmod +x "$SUPERCRONIC" \
28+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
29+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
30+
1431

1532

1633
# |--------------------------------------------------------------------------

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ CRON_SCHEDULE_2=0 3 * * *
162162
CRON_COMMAND_2=yarn run other-stuff
163163
```
164164

165-
**Important**: Cron was never designed with Docker in mind (it is way older than Docker). It will run correctly on
165+
**Important**: The cron runner we use is "Supercronic" and not the orginial "cron" that has a number of issues with containers.
166+
Even with Supercronic, the architecture of cron was never designed with Docker in mind (Cron is way older than Docker). It will run correctly on
166167
your container. If at some point you want to scale and add more containers, it will run on all your containers.
167168
At that point, if you only want to run a Cron task once for your application (and not once per container), you might
168169
want to have a look at alternative solutions like [Tasker](https://github.com/opsxcq/tasker) or one of the many

utils/Dockerfile.blueprint

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@ LABEL authors="Julien Neuhart <j.neuhart@thecodingmachine.com>, David Négrier <
1313
# |
1414

1515
RUN apt-get update &&\
16-
apt-get install -y --no-install-recommends curl cron git nano sudo ca-certificates procps --no-install-recommends
16+
apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps --no-install-recommends
17+
18+
# |--------------------------------------------------------------------------
19+
# | Supercronic
20+
# |--------------------------------------------------------------------------
21+
# |
22+
# | Supercronic is a drop-in replacement for cron (for containers).
23+
# |
24+
25+
RUN SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.5/supercronic-linux-amd64 \
26+
&& SUPERCRONIC=supercronic-linux-amd64 \
27+
&& SUPERCRONIC_SHA1SUM=9aeb41e00cc7b71d30d33c57a2333f2c2581a201 \
28+
&& curl -fsSLO "$SUPERCRONIC_URL" \
29+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
30+
&& chmod +x "$SUPERCRONIC" \
31+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
32+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
33+
1734

1835
{{ if eq $variant "apache" }}
1936
# |--------------------------------------------------------------------------

utils/docker-entrypoint-as-root.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ fi
4747
DOCKER_USER_ID=`id -ur $DOCKER_USER`
4848
#echo "Docker user id: $DOCKER_USER_ID"
4949

50-
# output on the logs can be done by writing on the "tini" PID. Useful for CRONTAB
51-
TINI_PID=`ps -e | grep tini | awk '{print $1;}'`
52-
node /usr/local/bin/generate_cron.js $TINI_PID > /etc/cron.d/generated_crontab
53-
chmod 0644 /etc/cron.d/generated_crontab
54-
5550
if [[ "$IMAGE_VARIANT" == "apache" ]]; then
5651
node /usr/local/bin/enable_apache_mods.js | bash
5752
fi
5853

59-
cron
54+
# output on the logs can be done by writing on the "tini" PID. Useful for CRONTAB
55+
TINI_PID=`ps -e | grep tini | awk '{print $1;}'`
56+
node /usr/local/bin/generate_cron.js $TINI_PID > /tmp/generated_crontab
57+
chmod 0644 /tmp/generated_crontab
58+
59+
# If generated_crontab is not empty, start supercronic
60+
if [[ -s /tmp/generated_crontab ]]; then
61+
supercronic /tmp/generated_crontab &
62+
fi
6063

6164
if [ -e /etc/container/startup.sh ]; then
6265
sudo -E -u "#$DOCKER_USER_ID" source /etc/container/startup.sh

utils/generate_cron.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* The script is run on each start of the container.
44
*/
55

6-
let tiniPid = process.argv[2],
7-
envVars = Object.keys(process.env);
6+
let envVars = Object.keys(process.env);
87

98
envVars.forEach((envVar) => {
109
if (envVar.indexOf('CRON_COMMAND') >= 0) {
@@ -19,13 +18,10 @@ envVars.forEach((envVar) => {
1918
let user = envVars.indexOf('CRON_USER' + suffix) >= 0 ? process.env['CRON_USER' + suffix] : 'root',
2019
schedule = process.env['CRON_SCHEDULE' + suffix];
2120

22-
// Note: this is a bit cryptic so here is what is going on:
23-
// First the command is piped into "sed" and we add [Cron]
24-
// In case there is an error message (on stderr), this will not be handled by sed.
25-
// So we switch output and error streams using "3>&2 2>&1 1>&3"
26-
// And we apply again sed on stdout (which is the past stderr)
27-
// Finally we switch back to stderr and stdout: 4>&2 2>&1 1>&4
28-
// and we put the output in /proc/xxx/fd1|2 which are the processes output for the Docker container.
29-
console.log(schedule + ' ' + user + ' ((((' + command + ") | sed -e 's/^/[Cron] /' ) 3>&2 2>&1 1>&3 | sed -e 's/^/[Cron error] /') 4>&2 2>&1 1>&4) > /proc/" + tiniPid + "/fd/1 2> /proc/" + tiniPid + "/fd/2");
21+
if (user !== 'root') {
22+
command = 'sudo -E -u ' + user + ' ' + command;
23+
}
24+
25+
console.log(schedule + ' ' + command);
3026
}
3127
});

0 commit comments

Comments
 (0)