1.docker-compose 安装

sudo su
curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   612  100   612    0     0     18      0  0:00:34  0:00:32  0:00:02   133
100  1400  100  1400    0     0     41      0  0:00:34  0:00:33  0:00:01  4516

 指定版本比如1.25.1-rc1版本

curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

root@yys:/home/yys/my# docker-compose --version
Unable to find image 'docker/compose:1.8.0' locally
1.8.0: Pulling from docker/compose
e110a4a17941: Pull complete 
92120570534d: Pull complete 
47d26c525b40: Pull complete 
40a1d6f501ac: Pull complete 
643031e197d8: Pull complete 
0841ec069338: Pull complete 
Digest: sha256:9bb1d2f141b4511b52dac37e5ea0aecadaf7786bc47184c133c566a4f678061d
Status: Downloaded newer image for docker/compose:1.8.0
docker-compose version 1.8.0, build f3628c7
 

2.创建工程

2.1 设置

教程:https://docs.docker.com/compose/gettingstarted/ 

mkdir composetest
cd composetest

 

vim composetest
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)
requirements.txt

写入 

flask
redis

2.2 创建一个 Dockerfile

vim Dockerfile

自动下载python3.8

FROM python:3.8-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"]

2.3 定义一个服务

docker-compose.yml

 

乌班图版本改为2.0,不然报错 

yys@yys:~/桌面/composetest$ docker-compose up
ERROR: Version in "./docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a version of "2" (or "2.0") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

version: "2.0"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

2.4 建立并运行compose

docker-compose up

成功后 

his 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 11 Apr 2021 06:05:58.643 * Loading RDB produced by version 6.2.1
redis_1  | 1:M 11 Apr 2021 06:05:58.643 * RDB age 71 seconds
redis_1  | 1:M 11 Apr 2021 06:05:58.643 * RDB memory usage when created 0.77 Mb
redis_1  | 1:M 11 Apr 2021 06:05:58.643 * DB loaded from disk: 0.001 seconds
redis_1  | 1:M 11 Apr 2021 06:05:58.644 * 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)

docker ps

 

yys@yys:~/桌面/composetest$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                    NAMES
dc2ac9c73e9e   docker/compose:1.8.0   "/usr/bin/docker-com…"   4 minutes ago   Up 4 minutes                            ecstatic_blackburn
33723b6b6e64   composetest_web        "flask run"              5 minutes ago   Up 4 minutes   0.0.0.0:5000->5000/tcp   composetest_web_1
c27f1ad7beae   redis:alpine           "docker-entrypoint.s…"   5 minutes ago   Up 4 minutes   6379/tcp   

可以访问一下ip

curl localhost:5000

yys@yys:~/桌面/composetest$ curl localhost:5000
Hello World! I have been seen 1 times.

docker images

yys@yys:~/桌面/composetest$ docker images
REPOSITORY        TAG          IMAGE ID       CREATED          SIZE
composetest_web   latest       ad7ba88635ef   13 minutes ago   183MB
python            3.8-alpine   164bb3a47e39   8 days ago       42.9MB
redis             alpine       f7cdff500577   9 days ago       32.3MB
docker/compose    1.8.0        89188432ef03   4 years ago      59.1MB