Shamoon
Shamoon

Reputation: 43559

How can I run a Docker command after building?

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?

Upvotes: 0

Views: 3159

Answers (2)

Sergio Santiago
Sergio Santiago

Reputation: 1514

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

Upvotes: 1

David Maze
David Maze

Reputation: 159351

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

Upvotes: 2

Related Questions