Skip to content

Commit

Permalink
Add docker-entrypoint.sh to deal with cron installation
Browse files Browse the repository at this point in the history
  • Loading branch information
febrezo committed Dec 31, 2022
1 parent f666876 commit 093742a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
config/pre/*.env
config/pro/*.env
config/pre/crontabs/*
config/pro/crontabs/*
config/pre/cron.d/*
config/pro/cron.d/*
.env
volumes
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
FROM python:3.10-alpine

# Installing a blank crontab for the user 'root' in the service
RUN touch /etc/crontabs/root
# Setting permissions to make the file writable only by the owner
RUN chmod 0644 /etc/crontabs/root
COPY ./docker-entrypoint.sh /

# Fixing Python deps
ADD requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

# Adding the app itself
# Creating folders for the volumes
RUN mkdir /data
RUN mkdir /log

# Adding the app itself
ADD app /app
WORKDIR /app
ENTRYPOINT ["sh", "-c" , "crond -f"]
ENTRYPOINT ["/docker-entrypoint.sh"]
53 changes: 28 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,46 @@ Note that if you make changes and you want to see them you will need the `--buil

```
$ docker compose up --build
$ docker compose up --build
WARNING: The APP_NAME variable is not set. Defaulting to a blank string.
Creating network "tempered-iron_default" with the default driver
Creating volume "dev_data_vol" with local driver
Creating volume "dev_log_vol" with local driver
Building main_service
Sending build context to Docker daemon 208.4kB
Sending build context to Docker daemon 221.2kB
Step 1/9 : FROM python:3.10-alpine
---> 2527f31628e7
Step 2/9 : RUN touch /etc/crontabs/root
Step 2/9 : COPY ./docker-entrypoint.sh /
---> Using cache
---> 929555f6812e
Step 3/9 : RUN chmod 0644 /etc/crontabs/root
---> 19f8ffddb714
Step 3/9 : ADD requirements.txt /tmp/requirements.txt
---> Using cache
---> b1fe1619fc32
Step 4/9 : ADD requirements.txt /tmp/requirements.txt
---> 38eee3cce847
Step 4/9 : RUN pip install -r /tmp/requirements.txt
---> Using cache
---> f13593d70c5d
Step 5/9 : RUN pip install -r /tmp/requirements.txt
---> d872ee4d3ed0
Step 5/9 : RUN mkdir /data
---> Using cache
---> 2a63ab14abb4
Step 6/9 : RUN mkdir /data
---> acf1704e22e1
Step 6/9 : RUN mkdir /log
---> Using cache
---> d2331cd7c967
---> a0e82b187ff7
Step 7/9 : ADD app /app
---> Using cache
---> 394f5317655b
---> 72f63d819973
Step 8/9 : WORKDIR /app
---> Using cache
---> 68e4724b652f
Step 9/9 : ENTRYPOINT ["sh", "-c" , "crond -f"]
---> b0fd5ba999a3
Step 9/9 : ENTRYPOINT ["/docker-entrypoint.sh"]
---> Using cache
---> 677d6091020d
Successfully built 677d6091020d
---> 2453fe7bcf18
Successfully built 2453fe7bcf18
Successfully tagged tempered-iron_main_service:latest
Creating tempered-iron_main_service_1 ... done
Starting tempered-iron_main_service_1 ... done
Attaching to tempered-iron_main_service_1
main_service_1 | [*] Verifying permissions of the service crontab at '/etc/cron.d/service-crontab'…
main_service_1 | /docker-entrypoint.sh: line 4: /etc/cron.d/service-crontab: Permission denied
main_service_1 | [*] Installing crontab from …
main_service_1 | [*] Showing installed cron jobs…
main_service_1 | * * * * * python3 /app/run.py
main_service_1 |
main_service_1 |
main_service_1 | [*] Starting cron daemon…
```

Once started, the deployed container will start a cron service to run schedule tasks.
Expand Down Expand Up @@ -139,7 +142,7 @@ Options:
$ docker compose logs -f
```

Once started, the deployed container will start a cron service to run scheduled tasks as stated in the `/etc/crontabs/root` file within the container.
Once started, the deployed container will start a cron service to run scheduled tasks as stated in the `/etc/cron.d/servuce-crontab` file within the container.
In this context, the scheduled task is as simple as grabbing the public IP address and logging it both, in the terminal and in a specific log file under `/log` in the container and in a volume mounted in the host.

### Basic debugging
Expand All @@ -154,10 +157,10 @@ To do so:
```
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29a49899accb temperediron_main_service "sh -c 'crond -f' sh…" 21 seconds ago Up 20 seconds temperediron_main_service_1
3efb87232dd4 tempered-iron_main_service "/docker-entrypoint.…" 2 minutes ago Up 10 seconds tempered-iron_main_service_1
```

2. Identify the `CONTAINER ID`. In this case, `29a49899accb`.
2. Identify the `CONTAINER ID`. In this case, `3efb87232dd4`.

3. Logging into the container:

Expand Down
3 changes: 3 additions & 0 deletions config/dev/cron.d/service-crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* * * * * python3 /app/run.py


25 changes: 0 additions & 25 deletions config/dev/crontabs/root

This file was deleted.

3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ version: '3.5'
services:
main_service:
build: .
command: sh -c "crond -f"
#restart: unless-stopped
environment:
- APP_NAME=${APP_NAME}
Expand All @@ -12,4 +11,4 @@ services:
volumes:
- ./volumes/data:/data
- ./volumes/log:/log
- ./config/${ENVIRONMENT:-dev}/crontabs:/etc/crontabs
- ./config/${ENVIRONMENT:-dev}/cron.d:/etc/cron.d
10 changes: 10 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

echo "[*] Verifying permissions of the service crontab at '/etc/cron.d/service-crontab'…"
chmod 644 /etc/cron.d/service-crontab
echo "[*] Installing crontab from `/etc/cron.d/service-crontab`"
crontab /etc/cron.d/service-crontab
echo "[*] Showing installed cron jobs…"
crontab -l
echo "[*] Starting cron daemon…"
crond -f
4 changes: 2 additions & 2 deletions docs/PRODUCTION_RECOMMENDATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ If you have already run the container, you can check the running ones using `doc
```
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf5d42a2da7d temperediron_main_service "sh -c 'crond -f' sh…" 2 minutes ago Up 2 minutes temperediron_main_service_1
3efb87232dd4 tempered-iron_main_service "/docker-entrypoint.…" 2 minutes ago Up 10 seconds tempered-iron_main_service_1
```

In this case, entering the container is as simple as entering the container with a shell in interactive mode (`-it`).
Note that the default shell of Alpine containers is `sh`, but use `bash` if it is available in the Docker image you used as a base image.

```
$ docker exec -it cf5d42a2da7d sh
$ docker exec -it 3efb87232dd4 sh
/app # whoami
root
/app # ls
Expand Down
8 changes: 4 additions & 4 deletions docs/VOLUME_MANAGEMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ services
volumes:
- data_vol:/data
- log_vol:/log
- ./config/${ENVIRONMENT:-dev}/crontabs:/etc/crontabs
- ./config/${ENVIRONMENT:-dev}/cron.d:/etc/cron.d
volumes:
data_vol:
Expand All @@ -73,7 +73,7 @@ services
volumes:
- data_vol:/data
- log_vol:/log
- ./config/${ENVIRONMENT:-dev}/crontabs:/etc/crontabs
- ./config/${ENVIRONMENT:-dev}/cron.d:/etc/cron.d
volumes:
data_vol:
name: pro_data_vol
Expand All @@ -86,5 +86,5 @@ volumes:
This is good because different environments can be used safely in a single machine without mixing others.
Of course, additional volumes can be added depending on the needs.

The only exception as shown above is the example of the configuration of how the `/etc/crontabs/` is mapped from the host.
This information is grabbed from the `./config/${ENVIRONMENT}/crontabs` folder meaning that the scheduled tasks can be mapped at deployment time.
The only exception as shown above is the example of the configuration of how the `/etc/cron.d/` is mapped from the host.
This information is grabbed from the `./config/${ENVIRONMENT}/cron.d` folder meaning that the scheduled tasks can be mapped at deployment time.

0 comments on commit 093742a

Please sign in to comment.