0

I have a Dockerfile:


FROM ubuntu:18.04
RUN apt-get -y update
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update -y
RUN apt-get install -y python3.7 build-essential python3-pip
RUN pip3 install --upgrade pip
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV FLASK_APP application.py
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
EXPOSE 5000
ENTRYPOINT python3 -m flask run --host=0.0.0.0

But I want to also run python3 download.py before running the ENTRYPOINT. If I put it in here, and then build, then it executes here. I need it to execute only on ElasticBeanstalk.

How would I do that?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • Do you want to run `python3 download.py` every time you start a container based on this image? Then you have to change the entrypoint to something like `sh -c 'python3 download.py; python3 -m flask run --host=0.0.0.0'`. – chepner Jul 08 '20 at 17:14
  • Yes, but I only want it to run when I deploy to Elastic Beanstalk – Shamoon Jul 08 '20 at 17:20
  • 1
    That's not Docker's problem to solve, at least not directly. Your entry point might be a small shell script instead, that takes an argument to determine whether or not to run `download.py` before running `flask`. You supply one argument if running on ElasticBeanstalk, a different argument if not. – chepner Jul 08 '20 at 17:23

2 Answers2

2

There's a pattern of using the Docker ENTRYPOINT to do first-time setup, and then launching the CMD. For example, you could write an entrypoint script like

#!/bin/sh

# Do the first-time setup
python3 download.py

# Run the CMD
exec "$@"

Since this is a shell script, you can include whatever logic or additional setup you need here.

In your Dockerfile, you need to change your ENTRYPOINT line to CMD, COPY in this script, and set it as the image's ENTRYPOINT.

...
COPY . /app
...

# If the script isn't already executable on the host
RUN chmod +x entrypoint.sh

# Must use JSON-array syntax
ENTRYPOINT ["/app/entrypoint.sh"]

# The same command as originally
CMD python3 -m flask run --host=0.0.0.0

If you want to debug this, since this setup honors the "command" part, you can run a one-off container that launches an interactive shell instead of the Flask process. This will still do the first-time setup, but then run the command from the docker run command instead of what was in the CMD line.

docker run --rm -it myimage bash
David Maze
  • 130,717
  • 29
  • 175
  • 215
1

You can control whether you run the python3 download.py using environment variables. And then running locally you do docker run -e....

Sergio Santiago
  • 1,316
  • 1
  • 11
  • 19
  • How can I pass that variable from Elastic Beanstalk? – Shamoon Jul 09 '20 at 01:40
  • You can check for the presence in ElasticBeanstalk, so if it is not present means it is ElasticBeanstalk and you don't need to run the download script. Something like in this link: https://stackoverflow.com/a/39296583/1563297 – Sergio Santiago Jul 09 '20 at 08:25