[root@sail ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.3/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 1004231004230039400:00:010:00:01--:--:--394 10023.5M 10023.5M 008670k 00:00:020:00:02--:--:--20.3M
[root@sail ~]# cd /usr/local/bin/ [root@sailbin]# ls docker-compose
[root@saildocker-compose]# docker-compose up [+] Running 7/7 ⠿ redis Pulled 14.1s ⠿ 59bf1c3509f3 Pull complete 3.0s ⠿ 719adce26c52 Pull complete 3.1s ⠿ b8f35e378c31 Pull complete 3.3s ⠿ d034517f789c Pull complete 9.0s ⠿ 3772d4d76753 Pull complete 9.0s ⠿ 211a7f52febb Pull complete 9.1s Sending build context to Docker daemon 725B Step 1/10 : FROM python:3.7-alpine 3.7-alpine: Pulling from library/python 59bf1c3509f3: Already exists 8786870f2876: Pull complete 45d4696938d0: Pull complete ef84af58b2c5: Pull complete c3c9b71b9a69: Pull complete Digest: sha256:d64e0124674d64e78cc9d7378a1130499ced66a7a00db0521d0120a2e88ac9e4 Status: Downloaded newer image for python:3.7-alpine ---> a1034fd13493 Step 2/10 : WORKDIR /code ---> Running in e23e4b173abf Removing intermediate container e23e4b173abf ---> 41eb64157cfc Step 3/10 : ENV FLASK_APP=app.py ---> Running in cdefb769398d Removing intermediate container cdefb769398d ---> ab741ac5cb17 Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0 ---> Running in4976c1da428c Removing intermediate container 4976c1da428c ---> 5a5c24d67db6 Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers ---> Running in53043bd38e33 fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz (1/13) Installing libgcc (10.3.1_git20211027-r0) (2/13) Installing libstdc++ (10.3.1_git20211027-r0) (3/13) Installing binutils (2.37-r3) (4/13) Installing libgomp (10.3.1_git20211027-r0) (5/13) Installing libatomic (10.3.1_git20211027-r0) (6/13) Installing libgphobos (10.3.1_git20211027-r0) (7/13) Installing gmp (6.2.1-r0) (8/13) Installing isl22 (0.22-r0) (9/13) Installing mpfr4 (4.1.0-r0) (10/13) Installing mpc1 (1.2.1-r0) (11/13) Installing gcc (10.3.1_git20211027-r0) (12/13) Installing linux-headers (5.10.41-r0) (13/13) Installing musl-dev (1.2.2-r7) Executing busybox-1.34.1-r3.trigger OK: 139 MiB in48 packages Removing intermediate container 53043bd38e33 ---> 73e9550df596 Step 6/10 : COPY requirements.txt requirements.txt ---> c5a73d6f1fe1 Step 7/10 : RUN pip install -r requirements.txt ---> Running in826790d0bfbb Collecting flask Downloading Flask-2.0.2-py3-none-any.whl (95 kB) Collecting redis Downloading redis-4.1.0-py3-none-any.whl (171 kB) Collecting itsdangerous>=2.0 Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB) Collecting click>=7.1.2 Downloading click-8.0.3-py3-none-any.whl (97 kB) Collecting Werkzeug>=2.0 Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB) Collecting Jinja2>=3.0 Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB) Collecting packaging>=21.3 Downloading packaging-21.3-py3-none-any.whl (40 kB) Collecting deprecated>=1.2.3 Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB) Collecting importlib-metadata>=1.0 Downloading importlib_metadata-4.10.0-py3-none-any.whl (17 kB) Collecting wrapt<2,>=1.10 Downloading wrapt-1.13.3-cp37-cp37m-musllinux_1_1_x86_64.whl (78 kB) Collecting zipp>=0.5 Downloading zipp-3.7.0-py3-none-any.whl (5.3 kB) Collecting typing-extensions>=3.6.4 Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB) Collecting MarkupSafe>=2.0 Downloading MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl (30 kB) Collecting pyparsing!=3.0.5,>=2.0.2 Downloading pyparsing-3.0.6-py3-none-any.whl (97 kB) Installing collected packages: zipp, typing-extensions, wrapt, pyparsing, MarkupSafe, importlib-metadata, Werkzeug, packaging, Jinja2, itsdangerous, deprecated, click, redis, flask Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 deprecated-1.2.13 flask-2.0.2 importlib-metadata-4.10.0 itsdangerous-2.0.1 packaging-21.3 pyparsing-3.0.6 redis-4.1.0 typing-extensions-4.0.1 wrapt-1.13.3 zipp-3.7.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available. You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command. Removing intermediate container 826790d0bfbb ---> c1483947fad2 Step 8/10 : EXPOSE 5000 ---> Running in cd59e0408b47 Removing intermediate container cd59e0408b47 ---> 05c632dea80d Step 9/10 : COPY . . ---> 8c89b910e366 Step 10/10 : CMD ["flask", "run"] ---> Running in1d9d071fee96 Removing intermediate container 1d9d071fee96 ---> de4639486b50 Successfully built de4639486b50 Successfully tagged docker-compose_web:latest Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them [+] Running 3/3 ⠿ Network docker-compose_default Created 0.3s ⠿ Container docker-compose-redis-1 Created 0.0s ⠿ Container docker-compose-web-1 Created 0.0s Attaching to docker-compose-redis-1, docker-compose-web-1 docker-compose-redis-1 | 1:C 07 Jan 202208:36:26.687# oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo docker-compose-redis-1 | 1:C 07 Jan 202208:36:26.687# Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started docker-compose-redis-1 | 1:C 07 Jan 202208:36:26.687# Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.687 * monotonic clock: POSIX clock_gettime docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.688 * Running mode=standalone, port=6379. docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.688# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.688# Server initialized docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.688# 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. docker-compose-redis-1 | 1:M 07 Jan 202208:36:26.688 * Ready to accept connections docker-compose-web-1 | * Serving Flask app 'app.py' (lazy loading) docker-compose-web-1 | * Environment: production docker-compose-web-1 | WARNING: This is a development server. Do not use it in a production deployment. docker-compose-web-1 | Use a production WSGI server instead. docker-compose-web-1 | * Debug mode: off docker-compose-web-1 | * Running on all addresses. docker-compose-web-1 | WARNING: This is a development server. Do not use it in a production deployment. docker-compose-web-1 | * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)
查看镜像
1 2 3 4 5
[root@saildocker-compose]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-compose_web latest 6df8b78250a1 44 seconds ago 185MB redis alpine 3900abf41552 5 weeks ago 32.4MB python 3.7-alpine a1034fd13493 5 weeks ago 41.8MB
启动 Docker Compose 时,会自动拉取需要的镜像。
查看容器
1 2 3 4
[root@saildocker-compose]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 78a6f8b03a49 docker-compose_web"flask run"8 minutes ago Up 8 minutes 0.0.0.0:5000->5000/tcp docker-compose-web-1 b4da6da4364f redis:alpine "docker-entrypoint.s…"8 minutes ago Up 8 minutes 6379/tcp docker-compose-redis-1
可以看到容器命名都带有数字,是因为需要集群管理,数字代表副本序号。
查看网络
1 2 3 4 5 6 7
[root@saildocker-compose]# docker network ls NETWORK ID NAME DRIVER SCOPE b89f719e94e0 bridge bridge local 619a5845a105 docker-compose_default bridge local 28d77e958643 host host local 801fbbe1b38c mynet bridge local c3ff850e96f0 none null local
项目中的内容都在同个网络下。
访问测试
1 2 3 4 5 6
[root@saildocker-compose]# curl localhost:5000 Hello World! I have been seen 1 times. [root@saildocker-compose]# curl localhost:5000 Hello World! I have been seen 2 times. [root@saildocker-compose]# curl localhost:5000 Hello World! I have been seen 3 times.
[root@saildocker-compose]# docker-compose down [+] Running 3/3 ⠿ Container docker-compose-web-1 Removed 10.2s ⠿ Container docker-compose-redis-1 Removed 0.2s ⠿ Network docker-compose_default Removed [root@saildocker-compose]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@saildocker-compose]# docker network ls NETWORK ID NAME DRIVER SCOPE b89f719e94e0 bridge bridge local 28d77e958643 host host local 801fbbe1b38c mynet bridge local c3ff850e96f0 none null local