Docker Console App Taking Many Arguments
Possible usage:
Use cases:
- app is a console app
- app needs some input (input data could be put inside mapped
in
dir. E.g./home/<username>/app_in
) - app generates some output (output data could be saved inside mapped
out
dir. E.g./home/<username>/app_out
) - app needs to run a few times with different commands
- some operations needed before app starts
Entrypoint script:
#!/bin/sh
# Activate venv
source /arg/venv/bin/activate
# some commands could be put here
# they will be executed before commands given with docker run command and before test_app.py as well
for var in "$@"
do
eval "$var"
done
Sample app Dockerfile:
# Python Debian image as a base
FROM python:3.8-slim-buster
# Package manager update and new packages installation
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y <some_packages>
# Copy application to container
COPY test_app.py /app/test_app.py
COPY requirements.txt /app/requirements.txt
# Change dir, dirs creation, venv install and packages install
WORKDIR /app
RUN mkdir /app/in /app/out
RUN pip install virtualenv
RUN virtualenv --python /usr/local/bin/python3.8 venv
RUN /bin/sh venv/bin/activate && pip install -r requirements.txt
# Env variables set if needed
ENV PYTHONPATH /app/venv/lib/python3.8/site-packages:/app
# Copying entrypoint script and change permissions and set as entrypoint
COPY entrypoint.sh /app/entrypoint.sh
RUN ["chmod", "+x", "/app/entrypoint.sh"]
ENTRYPOINT ["/app/entrypoint.sh"]
Docker image build
Dockerfile’s name is app.Dockerfile
and is located in the same dir with entrypoint.sh
, test_app.py
and requirements.txt
docker build -t docker_repo/app_name . -f app.Dockerfile
Sample usage command
Below command will:
- start your contenerized app in interactive mode
- map
in
andout
dirs as volumes (possible data exchange with container and host) - run
test_app.py
- copy everything to out dir
- stays in container because of
/bin/bash
at the enddocker run -it -v "/home/<username>/app_in:/app/in" -v "/home/<username>/app_out:/app/out" docker_repo/app_name "python test_app.py" "cp -a . /app/out" "/bin/bash"