Working with Docker Compose
- Installing Docker-compose
- 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 environment variable file
vim .env
COMPOSE_PROJECT_NAME=web2 PYTHONBUFFERED=true FLASK_APP=app.py FLASK_DEBUG=1
save the file ESC + wq!
- Create Docker Compose file vim
docker-compose.yml
- Create python app file
- Rest is magic now! Done by do docker-compose.yml file
docker-compose up --build -d