Docker-compose 安装及入门体验

一、下载资源包

[root@yang ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 633 100 633 0 0 426 0 0:00:01 0:00:01 --:--:-- 425
100 11.6M 100 11.6M 0 0 2363k 0 0:00:05 0:00:05 --:--:-- 4870k

二、授权、安装docker-compose

[root@yang ~]# cd /usr/local/bin/

[root@yang bin]# chmod +x docker-compose
[root@yang bin]# ll
总用量 11916
-rwxr-xr-x. 1 root root 12199520 2月 2 12:18 docker-compose

测试:

[root@yang bin]# docker-compose version
docker-compose version 1.28.2, build 67630359
docker-py version: 4.4.1
CPython version: 3.7.9
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

如果出现以上信息,说明安装成功!

三、体验(Docker Compose入门)

1.创建目录

[root@yang home]# mkdir composetest
[root@yang home]# cd composetest/
[root@yang composetest]# ll
总用量 0

2.创建 app.py 应用文件

[root@yang composetest]# cat app.py
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)

3.创建 requirements.txt 文件

[root@yang composetest]# cat requirements.txt
flask redis

4.创建 Dockerfile 

[root@yang composetest]# cat Dokcerfile
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.创建 docker-compose.yml 文件

[root@yang composetest]# cat docker-compose.yml
version: "3.9" services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine"

6.查看创建的四个文件

[root@yang composetest]# ll
总用量 16
-rw-r--r--. 1 root root 514 2月   7 15:40 app.py
-rw-r--r--. 1 root root 111 2月   7 15:41 docker-compose.yml
-rw-r--r--. 1 root root 252 2月   7 15:40 Dockerfile
-rw-r--r--. 1 root root  12 2月   7 15:40 requirements.txt

7.使用Compose构建并运行应用程序

启动方式:

前台启动:docker-compose up
后台启动:docker-compose up -d

 

[root@yang composetest]# docker-compose up
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
Creating network "composetest_default" with the default driver
Building web Sending build context to Docker daemon 5.632kB Step 1/10 : FROM python:3.7-alpine 3.7-alpine: Pulling from library/python 801bfaa63ef2: Pulling fs layer 7678dd7631a2: Pulling fs layer 673b2cf6feb8: Pulling fs layer 924474d3e184: Pulling fs layer 3531f307fdb1: Pulling fs layer 924474d3e184: Waiting 3531f307fdb1: Waiting 7678dd7631a2: Verifying Checksum 7678dd7631a2: Download complete 801bfaa63ef2: Verifying Checksum 801bfaa63ef2: Download complete 924474d3e184: Verifying Checksum 924474d3e184: Download complete 801bfaa63ef2: Pull complete 673b2cf6feb8: Verifying Checksum 673b2cf6feb8: Download complete 7678dd7631a2: Pull complete 3531f307fdb1: Verifying Checksum 3531f307fdb1: Download complete 673b2cf6feb8: Pull complete 924474d3e184: Pull complete 3531f307fdb1: Pull complete Digest: sha256:a73d0fdab3235920c0df44d55c420e2c3096f1b1a95e5f913a0287eee4192bdb Status: Downloaded newer image for python:3.7-alpine ---> 72e4ef8abf8e Step 2/10 : WORKDIR /code ---> Running in 151b945d2c13 Removing intermediate container 151b945d2c13 ---> 1b14ab6c18ac Step 3/10 : ENV FLASK_APP=app.py ---> Running in 7a7eab9fb42f Removing intermediate container 7a7eab9fb42f ---> 747b4e122ed3 Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0 ---> Running in 08adb93422cd Removing intermediate container 08adb93422cd ---> 3eb1874d7f16 Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers ---> Running in c37bc1ad7951 fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz (1/13) Installing libgcc (9.3.0-r2) (2/13) Installing libstdc++ (9.3.0-r2) (3/13) Installing binutils (2.34-r1) (4/13) Installing gmp (6.2.0-r0) (5/13) Installing isl (0.18-r0) (6/13) Installing libgomp (9.3.0-r2) (7/13) Installing libatomic (9.3.0-r2) (8/13) Installing libgphobos (9.3.0-r2) (9/13) Installing mpfr4 (4.0.2-r4) (10/13) Installing mpc1 (1.1.0-r1) (11/13) Installing gcc (9.3.0-r2) (12/13) Installing linux-headers (5.4.5-r1) (13/13) Installing musl-dev (1.1.24-r10) Executing busybox-1.31.1-r19.trigger OK: 153 MiB in 48 packages Removing intermediate container c37bc1ad7951 ---> 2481120acaf8 Step 6/10 : COPY requirements.txt requirements.txt ---> eae4008435ae Step 7/10 : RUN pip install -r requirements.txt ---> Running in 748aac6814ef Collecting flask Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB) Collecting click>=5.1 Downloading click-7.1.2-py2.py3-none-any.whl (82 kB) Collecting itsdangerous>=0.24 Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB) Collecting Jinja2>=2.10.1 Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB) Collecting MarkupSafe>=0.23 Downloading MarkupSafe-1.1.1.tar.gz (19 kB) Collecting Werkzeug>=0.15 Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB) Collecting redis Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB) Building wheels for collected packages: MarkupSafe Building wheel for MarkupSafe (setup.py): started Building wheel for MarkupSafe (setup.py): finished with status 'done' Created wheel for MarkupSafe: filename=MarkupSafe-1.1.1-cp37-cp37m-linux_x86_64.whl size=16911 sha256=18abd87c072361d8f963e9c8c76503320ada5c25c367c87abfe0657d41e24ded Stored in directory: /root/.cache/pip/wheels/b9/d9/ae/63bf9056b0a22b13ade9f6b9e08187c1bb71c47ef21a8c9924 Successfully built MarkupSafe Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, redis, flask Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0 redis-3.5.3 WARNING: You are using pip version 20.3.3; however, version 21.0.1 is available. You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command. Removing intermediate container 748aac6814ef ---> 2d46ebf3fdae Step 8/10 : EXPOSE 5000 ---> Running in 599e367d31e7 Removing intermediate container 599e367d31e7 ---> 10d25f300e5b Step 9/10 : COPY . . ---> 47cd580fff15 Step 10/10 : CMD ["flask", "run"] ---> Running in ffdb0da94e85 Removing intermediate container ffdb0da94e85 ---> cb67d85f811e Successfully built cb67d85f811e Successfully tagged composetest_web:latest 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 801bfaa63ef2: Already exists 9a8d0188e481: Pull complete 8a3f5c4e0176: Pull complete 3f7cb00af226: Pull complete e421f2f8acb5: Pull complete f41cc3c7c3e4: Pull complete Digest: sha256:2cd821f730b90a197816252972c2472e3d1fad3c42f052580bc958d3ad641f96 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 07 Feb 2021 07:55:01.635 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo redis_1 | 1:C 07 Feb 2021 07:55:01.635 # Redis version=6.0.10, bits=64, commit=00000000, modified=0, pid=1, just started redis_1 | 1:C 07 Feb 2021 07:55:01.635 # 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 07 Feb 2021 07:55:01.636 * Running mode=standalone, port=6379. redis_1 | 1:M 07 Feb 2021 07:55:01.636 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. redis_1 | 1:M 07 Feb 2021 07:55:01.636 # Server initialized redis_1 | 1:M 07 Feb 2021 07:55:01.636 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. redis_1 | 1:M 07 Feb 2021 07:55:01.636 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never'). redis_1 | 1:M 07 Feb 2021 07:55:01.637 * Ready to accept connections web_1 | * Serving Flask app "app.py" 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 http://0.0.0.0:5000/ (Press CTRL+C to quit)

8.查看启动的容器

[root@yang composetest]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES
6c01e350d40e   composetest_web   "flask run"              14 minutes ago   Up 10 minutes   0.0.0.0:5000->5000/tcp   composetest_web_1
a79c91a73a7e   redis:alpine      "docker-entrypoint.s…"   14 minutes ago   Up 10 minutes   6379/tcp                 composetest_redis_1

思考:为什么是web_1和redis_1呢?

  因为在 app.py 应用程序文件中写了增值,每访问一次就会增加一次,如下所示:

验证测试:

[root@yang composetest]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@yang composetest]# curl localhost:5000
Hello World! I have been seen 2 times.
[root@yang composetest]# curl localhost:5000
Hello World! I have been seen 3 times.
[root@yang composetest]# curl localhost:5000
Hello World! I have been seen 4 times.
[root@yang composetest]# curl localhost:5000
Hello World! I have been seen 5 times.

 

 9.查看镜像:

[root@yang composetest]# docker images
REPOSITORY        TAG          IMAGE ID       CREATED          SIZE
composetest_web   latest       cb67d85f811e   23 minutes ago   196MB
redis             alpine       933c79ea2511   3 weeks ago      31.6MB
python            3.7-alpine   72e4ef8abf8e   7 weeks ago      41.1MB
hello-world       latest       bf756fb1ae65   13 months ago    13.3kB

以上,发现通过docker-compose up 构建运行时,镜像已经全部自动下载!

10.查看网络:

[root@yang composetest]# docker network ls 
NETWORK ID     NAME                  DRIVER    SCOPE
930bc9066f65   bridge                bridge    local
16d740c61af8   composetest_default   bridge    local
aa2d7d096c1f   host                  host      local
0cc6b5946c09   none                  null      local

docker-compose up 时会自动生成一个网络,composetest_default,这是一个维护后的网络,使其启动的容器都在这个网络中,

因为在同一个网络中就可以用域名访问了,比如我们搭建了一个集群,下面分了好多个节点,届时如果时IP固定的情况下,就起不到什么作用,这时如果将所有节点加在同一网络中使用域名访问,就实现了高可用,高并发!

docker-compose,通过编写 docker-compose 文件,可以通过compose 一键启动和停止所有服务!


 

posted @ 2021-02-07 16:48  西瓜君~  阅读(361)  评论(0编辑  收藏  举报