Docker Compose Test
Prerequisites
Make sure you have already installed both Docker Engine and Docker Compose. You don’t need to install Python or Redis, as both are provided by Docker images.
Step1. Setup
1. Define the application dependencies.
# mkdir composetest
# cd composetest
2. Create a file called app.py in your project directory and paste this in:
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)
In this example, redis is the hostname of the redis container on the application’s network. We use the default port for Redis, 6379.
3. Create another file called requirements.txt in your project directory and paste this in:
flask
redis
Step2. Create a Dockerfile
# 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"]
This tells Docker to:
- Build an image starting with the Python 3.7 image.
- Set the working directory to /code.
- Set environment variables used by the flask command.
- Install gcc and other dependencies
- Copy requirements.txt and install the Python dependencies.
- Add metadata to the image to describe that the container is listening on port 5000
- Copy the current directory . in the project to the workdir . in the image.
- Set the default command for the container to flask run.
Step 3: Define services in a Compose file
Create a file called docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
Web service
The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 8000. This example service uses the default port for the Flask web server, 5000.
Redis service
The redis service uses a public Redis image pulled from the Docker Hub registry.
Step 4: Build and run your app with Compose
Start up the application by running docker compose up
[root@test composetest]# docker-compose up
Starting composetest_web_1 ... done
Starting composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
redis_1 | 1:C 28 Sep 2022 06:32:07.293 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 28 Sep 2022 06:32:07.294 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 28 Sep 2022 06:32:07.294 # Warning: no config file specified, using the default config. In order to specify a conf ig file use redis-server /path/to/redis.conf
redis_1 | 1:M 28 Sep 2022 06:32:07.294 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * Running mode=standalone, port=6379.
redis_1 | 1:M 28 Sep 2022 06:32:07.295 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/s omaxconn is set to the lower value of 128.
redis_1 | 1:M 28 Sep 2022 06:32:07.295 # Server initialized
redis_1 | 1:M 28 Sep 2022 06:32:07.295 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condit ion. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommi t_memory=1' for this to take effect.
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * Loading RDB produced by version 7.0.5
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * RDB age 60 seconds
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * RDB memory usage when created 0.82 Mb
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * Done loading RDB, keys loaded: 0, keys expired: 0.
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 28 Sep 2022 06:32:07.295 * Ready to accept connections
web_1 | * Serving Flask app 'app.py'
web_1 | * Debug mode: off
web_1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web_1 | * Running on all addresses (0.0.0.0)
web_1 | * Running on http://127.0.0.1:5000
web_1 | * Running on http://172.19.0.2:5000
web_1 | Press CTRL+C to quit
web_1 | 172.19.0.1 - - [28/Sep/2022 06:33:07] "GET / HTTP/1.1" 200 -
标签:Compose,06,Sep,redis,28,2022,32,Test,Docker
From: https://www.cnblogs.com/comecc/p/16738249.html