0

I have created a docker-compose yml file to run a cron-job in a docker container.

The cron-job which will run inside the docker container has to run a python script and connect to mysql an redis through redis-sentinel.

Cron-job need python and required python-dependencies(mysql and redis sentinel) to be installed in the docker container to run the python script successfully.

here is my docker-compose yml file

version: '3'
services:
  cron-job:
    image: alpine:3.10
    command: crond -f -d 8
    volumes:
      - /Users/anshul/cron:/etc/crontabs/root:z
    restart: unless-stopped
networks:
  default:
    external:
      name: cnet

What changes I need to do here to have python and required dependencies installed on the docker container, which will come up after running this compose file.

Anshul Sharma
  • 1,018
  • 3
  • 17
  • 39
  • 1
    You probably would be better off directly using a python image instead of alpine. Is alpine a requirement to you? – fpietka Sep 02 '20 at 12:44
  • No, I do not need alpine, but what about the mysql and redis dependencies if i use python image. – Anshul Sharma Sep 02 '20 at 12:47
  • 1
    if you also want to run them in docker, mysql and redis will have to be separate services – fpietka Sep 02 '20 at 12:49
  • Yes, I have those running in my different docker containers, I just need to install mysql.connector and redis.sentinel dependencies in this container, so that cron can run python script. – Anshul Sharma Sep 02 '20 at 12:52
  • 1
    They all should be in the same docker-compose file to share the same network. Then you probably have all the dependancies needed in your requirements.txt (as stated in the answer from @mikhail-pashkov) – fpietka Sep 02 '20 at 12:54
  • The network part also I have taken care, all the docker-compose yml file(mysql and redis) has the same network as mentioned above cnet. – Anshul Sharma Sep 02 '20 at 12:56
  • 1
    Ok, then just install your requirements as stated in the aswer. – fpietka Sep 02 '20 at 13:03

1 Answers1

2

You can use python image directly (if alpine is required, there is versions based on it) https://hub.docker.com/_/python I think that best way to solve your request is build your own docker image:

create docker file

Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

In this example you store your requirements in requirements.txt file.

Requirements file your can build with pip freeze > requirements.txt command (useful if you use virtual env's)

or

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir {mysql package name} {redis package name}

then you can push it in your docker hub registry, or build and use it directly from your docker-compose

Build your image and use with docker compose

1st way

You can build and push it to docker hub (here you can find more info: https://docs.docker.com/get-started/part3/)

Then your docker compose will be like:

docker-compose.yaml

version: '3'
services:
  cron-job:
    image: dockerhub-login/your-image-name:tag
    ...
2nd way

You can use Dockerfile in docker-compose:

docker-compose.yaml

version: '3'
services:
  cron-job:
    build: path/to/Dockerfile
    ...

if Dockerfile and docker-compose.yaml in the same directory, then you can use build: .