docker compose
This article introduces a Python Web application running on Docker Compose. This application uses the Flask framework to maintain a counter in Redis.
First make sure you have installed Docker Engine and Docker Compose. You don’t need to install Python or Redis because they are provided by Docker Images.
Install
Define the dependence of this application
- Create a directory for this project
$ mkdir composeTest
$ CD ComposeTest
- Create a file named
app.py
under your project, and then paste the code below:
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
- Create another file name is
reques.txt
under your project, and then paste the code below:
Flask
redis
create dockerfile
Create a file in your project directory called dockerfile
, and then paste the following content:
# syntax = docker/dockerfile: 1
From Python: 3.7-Alpine
Workdir /code
ENV FLASK_APP = App.py
ENV FLASK_RUN_HOST = 0.0.0.0
Run apk add-no-cache gcc musl-dev linux-headers
Copy Requirements.txt Requirements.txt
Run PIP Install -R Requirements.txt
Expose 5000
Copy..
CMD ["Flask", "Run"]
definition service in a compose file
Create a file called docker-compose.yml
in your project directory, and then paste the following content:
version: "3.9"
service:
web:
build:..
Ports:
- "8000: 5000"
redis:
Image: "Redis: Alpine"
This compose file defines two services: web
andredis
web service
The mirror used in the web` service is to build the dockerfile file from the current directory, and then bind the port of the container and the host. Flask Web Server’s port is 5000, and the port of the host is 8000.
Redis Service
The redis
service uses publicredis
mirrors from Docker Hub Registry.
Use Compose to build and run your application
- From your project directory, start your application by running the
docker compose up
.
$ docker compose up
- Enter
http: // localhost: 8000/
to observe the operation of the program, you will see the following information in the browser:
Hello World! I have ben seen 1 times.
- Refresh the page
This number will increase
Hello World! I have ben seen 2 times.
- Close the application
- You can run `docker compose double in the project directory
- Or the terminal of the original running program presses CTRL+ C
Edit compose file, add a binding and mount
Edit docker-compose.yml
file, add bind mount to theweb
service:
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
volumes:
- .:/code
environment:
FLASK_DEBUG: True
redis:
image: "redis:alpine"
Volumes keywords mount the current directory to the container/code directory, allowing you to change the code directly without re -constructing the image.
Re-construct and run the program with Compose
In your project directory, enter the docker compose up
with the updated compose file to build a program and running program.
$ docker compose up
update application
Because the code of the application has been mounted to the CODE directory of the container through Volume, you can directly modify the code and see the change immediately. No need to re -build mirror.
Modify the app.py
file, modify theHello World!
To` Hello from Docker!
return 'hello from docker! I have ben seen {} times. \ n'.Format (count)
`
Refresh the application in the browser, the returned information should be updated, and the number of the counter is still growing.
Other commands
If you want your service to run in the background, you can add -d
flag
$ docker compose up -d
[+] Running 3/3
⠿ Network composetest_default Created 0.1s
⠿ Container composetest-redis-1 Start... 0.6s
⠿ Container composetest-web-1 Started 0.6s
$ docker compose ps
NAME COMMAND SERVICE STATUS PORTS
composetest-redis-1 "docker-entrypoint.s…" redis running 6379/tcp
composetest-web-1 "flask run" web running 0.0.0.0:8000->5000/tcp
If you start Compose, use the docker compose up -d
to stop your service use
$ docker compose stop
You can delete all containers with the down
command, plus--volumes
will delete the data volume used by Redis containers at the same time.
$ docker compose down --volumes