用容器部署应用 Dockerfile
Setup
- create a directory for the project
mkdir composetest cd composetest
- create flask app
import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): # 重试循环允许我们在redis服务不可用时多次尝试请求 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) # 3. create requirements.txt # 4. create 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"]
5.定义service docker-compose.yml 定义两个service
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
docker-compose的相关指令
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [--profile <name>...] [options] [--] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--profile NAME Specify a profile to enable
-c, --context NAME Specify a context name
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--ansi (never|always|auto) Control when to print ANSI control characters
--no-ansi Do not print ANSI control characters (DEPRECATED)
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent (DEPRECATED)
--env-file PATH Specify an alternate environment file
Commands:
build Build or rebuild services
config Validate and view the Compose file
create Create services
down Stop and remove resources
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show version information and quit
使用用法:Overview of docker-compose CLI | Docker Documentation
Creating network "composetest_default" with the default driver
Building web
[+] Building 48.6s (16/16) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 325B 0.1s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 2.4s
=> docker-image://docker.io/docker/dockerfile:1@sha256:9e2c9eca7367393aecc68795c671f93466818395a2693498debe831fd67f5e89 3.7s
=> => resolve docker.io/docker/dockerfile:1@sha256:9e2c9eca7367393aecc68795c671f93466818395a2693498debe831fd67f5e89 0.0s
=> => sha256:8d9a8cad598f6c5e97bcb90aab70cd2e868d3dc0d514fa9b60468bf72bf32338 9.67MB / 9.67MB 3.2s
=> => sha256:9e2c9eca7367393aecc68795c671f93466818395a2693498debe831fd67f5e89 2.00kB / 2.00kB 0.0s
=> => sha256:cb8d6fa06268f199b41ccc0c2718caca915f74c640f3cd044af8e205e8d616cf 528B / 528B 0.0s
=> => sha256:b1e2fdbfa8cb359e5e566c70344df94749db2e6419e06ac542ed14a348c3ce81 1.21kB / 1.21kB 0.0s
=> => extracting sha256:8d9a8cad598f6c5e97bcb90aab70cd2e868d3dc0d514fa9b60468bf72bf32338 0.4s
=> [internal] load .dockerignore 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> [internal] load metadata for docker.io/library/python:3.7-alpine 8.0s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [1/6] FROM docker.io/library/python:3.7-alpine@sha256:cb74758aca3c66b1213e5d84fb943f425c7d44a07ba4949e4cf5c690cd855cae 4.5s
=> => resolve docker.io/library/python:3.7-alpine@sha256:cb74758aca3c66b1213e5d84fb943f425c7d44a07ba4949e4cf5c690cd855cae 0.0s
=> => sha256:3626a090061d56f9d6eaa946c29f4a7600401e2bca3f7b517369d0da709bf6d3 281.50kB / 281.50kB 0.7s
=> => sha256:dccff05a79aec65c158dbc956419ffd4baa371864b2f1614cc3d5ecdd46d3cb6 10.58MB / 10.58MB 1.6s
=> => sha256:cb74758aca3c66b1213e5d84fb943f425c7d44a07ba4949e4cf5c690cd855cae 1.65kB / 1.65kB 0.0s
=> => sha256:103c1a91cf1a49c3578d69a4921fe464ddafd020fb8b12de2e491e6355744055 1.37kB / 1.37kB 0.0s
=> => sha256:9231c59cd582c09c5ef11d97ac48f5754c6f5189e4f0ade5d8b9edefdcc5f2cf 7.84kB / 7.84kB 0.0s
=> => sha256:29291e31a76a7e560b9b7ad3cada56e8c18d50a96cca8a2573e4f4689d7aca77 2.81MB / 2.81MB 1.7s
=> => sha256:328cb130581154a13f554840c7073a8e17c76d8786b285fc7fe582b4e6ce32e2 231B / 231B 0.9s
=> => sha256:e91eabc5840c43a8311b77ec0134eb1b8aa44ee5dea57268171f6f88608e950b 2.35MB / 2.35MB 3.4s
=> => extracting sha256:29291e31a76a7e560b9b7ad3cada56e8c18d50a96cca8a2573e4f4689d7aca77 0.3s
=> => extracting sha256:3626a090061d56f9d6eaa946c29f4a7600401e2bca3f7b517369d0da709bf6d3 0.2s
=> => extracting sha256:dccff05a79aec65c158dbc956419ffd4baa371864b2f1614cc3d5ecdd46d3cb6 1.2s
=> => extracting sha256:328cb130581154a13f554840c7073a8e17c76d8786b285fc7fe582b4e6ce32e2 0.0s
=> => extracting sha256:e91eabc5840c43a8311b77ec0134eb1b8aa44ee5dea57268171f6f88608e950b 0.6s
=> [internal] load build context 0.0s
=> => transferring context: 8.32kB 0.0s
=> [2/6] WORKDIR /code 0.4s
=> [3/6] RUN apk add --no-cache gcc musl-dev linux-headers 19.2s
=> [4/6] COPY requirements.txt requirements.txt 0.1s
=> [5/6] RUN pip install -r requirements.txt 8.0s
=> [6/6] COPY . . 0.0s
=> exporting to image 1.3s
=> => exporting layers 1.3s
=> => writing image sha256:068295146e83fee102c3b69ca6efe778fdede175c26d10cd1a551a678bfdc393 0.0s
=> => naming to docker.io/library/composetest_web 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
alpine: Pulling from library/redis
29291e31a76a: Already exists
16bbd9479e91: Pull complete
d4bb2b90a23b: Pull complete
011b05d94792: Pull complete
3a615fd1f2fd: Pull complete
bd76b8efa6e9: Pull complete
Digest: sha256:b658aeb0824fa240bd04b5017de2379d9707a433cb7040332328996d63c265f6
Status: Downloaded newer image for redis:alpine
Creating composetest_redis_1 ... done
Creating composetest_web_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 | 1:C 21 Aug 2021 09:33:10.354 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 21 Aug 2021 09:33:10.355 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 21 Aug 2021 09:33:10.356 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 21 Aug 2021 09:33:10.357 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 21 Aug 2021 09:33:10.358 * Running mode=standalone, port=6379.
redis_1 | 1:M 21 Aug 2021 09:33:10.362 # Server initialized
redis_1 | 1:M 21 Aug 2021 09:33:10.367 * Ready to accept connections
web_1 | * Serving Flask app 'app.py' (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: off
web_1 | * Running on all addresses.
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)
增加环境变量与挂载
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
重新构建app docker-compose up
更新应用
vi app.py
不需重新构建镜像、因为已经挂载过了
return 'Hello from Docker! I have been seen {} times.n'.format(count)
停止
docker-compose stop
docker-compose down --volumes 同时移除挂载
网络
会自动维护一个网络,项目中的内容都在一个网络下
Service
Creating composetest_redis_1 ... done
Creating composetest_web_1 ... done
默认服务名:文件名_服务名_num
多个服务器。集群A B _num 副本数量
服务redis服务 => 4个副本,可以理解成多个运行的实例
可以直接通过服务名称访问: