0

I am trying to create container with following docker file. Since it is going to execute last CMD, it ignores CMD ["sh", "-c", "python3 run_scheduler.py --config=${config}"]. Is there a way to run both from single docker file. Also, is it possible to pass ${config} argument to flask api in CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"]

FROM python:3.8

LABEL version="0.1"

WORKDIR /app
COPY . /app

ARG config=dev
RUN  pip install -r requirements.txt
EXPOSE 5000
CMD ["sh", "-c", "python3 run_scheduler.py --config=${config}"]
CMD ["gunicorn",  "--bind", "0.0.0.0:5000",  "run_api:application", "&"]
2
  • 1
    Check out this SO Post: serverfault.com/questions/685697/… Commented Jun 10, 2020 at 13:49
  • You can specify an alternate command when you run the container (after the image name in docker run; as a Docker Compose command:). Run two separate containers with two separate commands. Commented Jun 10, 2020 at 14:14

1 Answer 1

1

As mentioned in comments you can pass 2-line bash script in CMD. This can cause problems however. Docker is not really meant to run multiple processes in single container. I already see "&" argument in your second CMD. I guess you had problems with second process wasn't running so you tried to push it to the background.

The solution recommended by Docker is to run supervisord inside the container: https://docs.docker.com/config/containers/multi-service_container/

From naming concepts I guess you can solve your problem with Flask-APScheduler https://github.com/viniciuschiele/flask-apscheduler This way you can get rid of second sidecar script.

And lastly you asked:

Also, is it possible to pass ${config} argument to flask api

and answer is yes. Declare ENV in your Dockerfile like so:

FROM python:3.8
...
...
ENV config foo
...
CMD ["whatever_command", "--option", "${config}"]

This way "${config}" will be replaced with value foo.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for help. I did create 2 containers. Worked for my use case.using following docker compose. version: '3' services: api: build: context: . dockerfile: Dockerfile command: ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"] ports: - "5000:5000" scheduler: build: context: . dockerfile: Dockerfile command: ["sh", "-c", "python3 run_scheduler.py --config=dev"] ports: - "80:80"
I also need to pass --config=dev to command: ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"] . This variable I need to use to pick config in Flask Api. I know that I can pass this as an arg from command but how can I access in my flask api (run_api: application).
You need to use os module: from os import getenv and use getenv to pick up your env var. Have a look at docs.python.org/3.8/library/os.html#os.getenv
Thanks, "from os import getenv and use getenv" did solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.