Working with Container Entrypoint script
- Lets do some changes in application files,
- Create python app file
vim app.py
import os from flask import Flask from flask_redis import FlaskRedis app = Flask(__name__) app.config['REDIS_URL'] = 'redis://redis:6379/0' redis = FlaskRedis(app) @app.route('/') def counter(): return '{0} {1}'.format(str(redis.incr('web2_counter')), os.getenv('WEB2_COUNTER_MSG', ''))
save the file
ESC + wq!
- Create python app dependency definition file
vim requirements.txt
Flask==0.12 flask-redis==0.3
save the file
ESC + wq!
- Create a bash script to be executed inside container
vim docker-entrypoint.sh
#!/bin/sh set -e echo "The Dockerfile ENTRYPOINT has been executed!" export WEB2_COUNTER_MSG="${WEB2_COUNTER_MSG:-carbon based life forms have sensed this website}" exec "$@"
save the file
ESC + wq!
- Now lets create a Dockerfile for the python application
vim Dockerfile
FROM python:2.7-alpine RUN mkdir /app WORKDIR /app COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . LABEL maintainer="WebMagic Informatica <info@13.233.215.83>" \ version="1.0" VOLUME ["/app/public"] COPY docker-entrypoint.sh / RUN chmod +x /docker-entrypoint.sh ENTRYPOINT ["/docker-entrypoint.sh"] CMD flask run --host=0.0.0.0 --port=5000
save the file
ESC + wq!
- Create python app file
- Navigate to “08 Running Scripts When a Container Starts” and run redis container
docker container run --rm -itd -p 6379:6379 --name redis --net firstnetwork redis:3.2-alpine
- Build a docker image tagged as webentrypoint, this docker file has statement for entrypoint
docker image build -t webentrypoint .
- Run your webentrypoint container
docker container run --rm -it -p 5000:5000 -e FLASK_APP=app.py -e FLASK_DEBUG=1 --name webentrypoint --net firstnetwork -v $PWD:/app webentrypoint
CTRL + C - Run your webentrypoint container
docker container run --rm -it -p 5000:5000 -e FLASK_APP=app.py -e FLASK_DEBUG=1 -e WEB2_COUNTER_MSG="Docker fans have visited this page" --name webentrypoint --net firstnetwork webentrypoint