docker-compose安装

5 篇文章 0 订阅
5 篇文章 1 订阅
  1. 安装docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. 授权
sudo chmod +x /usr/local/bin/docker-compose
  1. 创建软连接
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  1. 测试是否安装成功
$ docker-compose --version
docker-compose version 1.25.5, build 1110ad01
  1. 创建工程目录
mkdir composetest
cd composetest
  1. 创建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)
  1. 创建requirements.txt文件,并写入
flask
redis
  1. 编辑Dockerfile 文件
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
COPY . .
CMD ["flask", "run"]
  1. 定义docker-compose.yml文件
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"
  1. 运行docker-compose容器
[root@localhost composetest]# docker-compose up
Building web
Step 1/9 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
cbdbe7a5bc2a: Pulling fs layer                                                                                          26ebcd19a4e3: Pulling fs layer                                                                                          8341bd19193b: Pulling fs layer                                                                                          ecc595bd65e1: Waiting                                                                                                   4b1c9d8f69d2: Waiting                                                                                                   ERROR: Service 'web' failed to build: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/e8/e854017db51499632825b8eb717bebf6790edd99c8268d2b3654388c1308c478/data?verify=1590578252-SnrTJNgVReKOIXdARUNPaBpFcT8%3D: dial tcp 104.18.124.25:443: i/o timeout
[root@localhost composetest]# docker-compose up
Building web
Step 1/9 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
cbdbe7a5bc2a: Pull complete                                                                                             26ebcd19a4e3: Pull complete                                                                                             8341bd19193b: Pull complete                                                                                             ecc595bd65e1: Pull complete                                                                                             4b1c9d8f69d2: Pull complete                                                                                             Digest: sha256:778802b5b9797279772814fb15a7c6ee494848ced17965bb57092a0b900c0e4f
Status: Downloaded newer image for python:3.7-alpine
 ---> e854017db514
Step 2/9 : WORKDIR /code
 ---> Running in 916ffdbfe0d6
Removing intermediate container 916ffdbfe0d6
 ---> b5641eb49fca
Step 3/9 : ENV FLASK_APP app.py
 ---> Running in 9d93dabde386
Removing intermediate container 9d93dabde386
 ---> 9de8cde96b56
Step 4/9 : ENV FLASK_RUN_HOST 0.0.0.0
 ---> Running in fb11e71cb03f
Removing intermediate container fb11e71cb03f
 ---> a2db799db832
Step 5/9 : RUN apk add --no-cache gcc musl-dev linux-headers
 ---> Running in b90dd2f278cd
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/12) Installing libgcc (9.2.0-r4)
(2/12) Installing libstdc++ (9.2.0-r4)
(3/12) Installing binutils (2.33.1-r0)
(4/12) Installing gmp (6.1.2-r1)
(5/12) Installing isl (0.18-r0)
(6/12) Installing libgomp (9.2.0-r4)
(7/12) Installing libatomic (9.2.0-r4)
(8/12) Installing mpfr4 (4.0.2-r1)
(9/12) Installing mpc1 (1.1.0-r1)
(10/12) Installing gcc (9.2.0-r4)
(11/12) Installing linux-headers (4.19.36-r0)
(12/12) Installing musl-dev (1.1.24-r2)
Executing busybox-1.31.1-r9.trigger
OK: 124 MiB in 46 packages
Removing intermediate container b90dd2f278cd
 ---> 3457b379aa53
Step 6/9 : COPY requirements.txt requirements.txt
 ---> 3c3c6411035f
Step 7/9 : RUN pip install -r requirements.txt
 ---> Running in deb615f9ba4d
Collecting flask
  Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting redis
  Downloading redis-3.5.2-py2.py3-none-any.whl (71 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 Werkzeug>=0.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1.tar.gz (19 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=16913 sha256=2d2682caa72125566b10d1a7eabbf167e3cd76eac7b62959a7f81d3efb312c31
  Stored in directory: /root/.cache/pip/wheels/b9/d9/ae/63bf9056b0a22b13ade9f6b9e08187c1bb71c47ef21a8c9924
Successfully built MarkupSafe
Installing collected packages: click, itsdangerous, Werkzeug, MarkupSafe, Jinja2, flask, redis
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0 redis-3.5.2
Removing intermediate container deb615f9ba4d
 ---> 75ba87456838
Step 8/9 : COPY . .
 ---> 29b486311081
Step 9/9 : CMD ["flask", "run"]
 ---> Running in 7b611e519243
Removing intermediate container 7b611e519243
 ---> 4e0f3c5a9607
Successfully built 4e0f3c5a9607
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
cbdbe7a5bc2a: Already exists                                                                                            dc0373118a0d: Pull complete                                                                                             cfd369fe6256: Pull complete                                                                                             3ab90cca2b5b: Pull complete                                                                                             b54e03d4f573: Pull complete                                                                                             98f28c5b40da: Pull complete                                                                                             Digest: sha256:8be79c511a69ad912f0366d5250968916d1438ff3b1a420c52e3ada98d03c57a
Status: Downloaded newer image for redis:alpine
Creating composetest_web_1   ... done                                                                                   Creating composetest_redis_1 ... done                                                                                   Attaching to composetest_web_1, composetest_redis_1
redis_1  | 1:C 27 May 2020 10:41:26.236 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 27 May 2020 10:41:26.236 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 27 May 2020 10:41:26.236 # 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 27 May 2020 10:41:26.237 * Running mode=standalone, port=6379.
redis_1  | 1:M 27 May 2020 10:41:26.237 # 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 27 May 2020 10:41:26.237 # Server initialized
redis_1  | 1:M 27 May 2020 10:41:26.237 # 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 27 May 2020 10:41:26.238 * 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)
web_1    | 192.168.56.1 - - [27/May/2020 10:41:46] "GET / HTTP/1.1" 200 -
web_1    | 192.168.56.1 - - [27/May/2020 10:41:46] "GET /favicon.ico HTTP/1.1" 404 -
  1. 浏览器中输入
http://192.168.56.10:5000/

在这里插入图片描述
13.刷新
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值