美文网首页
Centos下的纯命令记录Docker学习(12)-docker

Centos下的纯命令记录Docker学习(12)-docker

作者: 小钟钟同学 | 来源:发表于2020-11-23 17:13 被阅读0次

flask镜像的精简

因为我们的镜像基本就只是一些代码,但是我们发现之前生产的镜像居然有了900多M,这也有点忒离谱了!所以我们需要精简一下,这地方其实涉及到一个概念,就是镜像的多阶构建。后续我再实践一下这个多阶构建的。

Dockerfile文件构建Flask镜像(旧的镜像构建):

FROM python:3.6

WORKDIR /data/Project/dockerflask

COPY requirements.txt ./

RUN pip install --no-cache-dir --default-timeout=100  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

COPY . .

CMD ["gunicorn", "main:app", "-c", "./gunicorn.conf"]
image.png

Dockerfile文件构建Flask镜像(新的镜像构建):

FROM python:3.7-alpine

WORKDIR /data/Project/web_statistics

COPY requirements.txt ./

RUN pip install --no-cache-dir --default-timeout=100  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

COPY . .

CMD ["gunicorn", "main:app", "-c", "./gunicorn.conf"]

执行进行的构建


[root@localhost dockerflask]#  docker build -t='flaskalpine' .
Sending build context to Docker daemon  44.17MB
Step 1/6 : FROM python:3.7-alpine
 ---> 6b73b71fd64e
Step 2/6 : WORKDIR /data/Project/web_statistics
 ---> Running in f94bdd96571d
Removing intermediate container f94bdd96571d
 ---> 7dda70493a42
Step 3/6 : COPY requirements.txt ./
 ---> af53a760b158
Step 4/6 : RUN pip install --no-cache-dir --default-timeout=100  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
 ---> Running in 0e8f161cab08
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting click==7.1.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting Flask==1.1.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting gunicorn==20.0.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/69/ca/926f7cd3a2014b16870086b2d0fdc84a9e49473c68a8dff8b57f7c156f43/gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
Collecting itsdangerous==1.1.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2==2.11.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
Collecting MarkupSafe==1.1.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz (19 kB)
Collecting psycopg2==2.8.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fd/ae/98cb7a0cbb1d748ee547b058b14604bd0e9bf285a8e0cc5d148f8a8a952e/psycopg2-2.8.6.tar.gz (383 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ju4ar_gj/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ju4ar_gj/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-_pwr2fy_
         cwd: /tmp/pip-install-ju4ar_gj/psycopg2/
    Complete output (23 lines):
    running egg_info
    creating /tmp/pip-pip-egg-info-_pwr2fy_/psycopg2.egg-info
    writing /tmp/pip-pip-egg-info-_pwr2fy_/psycopg2.egg-info/PKG-INFO
    writing dependency_links to /tmp/pip-pip-egg-info-_pwr2fy_/psycopg2.egg-info/dependency_links.txt
    writing top-level names to /tmp/pip-pip-egg-info-_pwr2fy_/psycopg2.egg-info/top_level.txt
    writing manifest file '/tmp/pip-pip-egg-info-_pwr2fy_/psycopg2.egg-info/SOURCES.txt'

    Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.

    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).

    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The command '/bin/sh -c pip install --no-cache-dir --default-timeout=100  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple' returned a non-zero code: 1
[root@localhost dockerflask]#

出现异常。我们的依赖包的安装的PG文件驱动好像找不到!!!:

Error: pg_config executable not found.

修正构建镜像文件,在一次执行构建:

FROM python:3.7-alpine

WORKDIR /data/Project/web_statistics

COPY requirements.txt ./

RUN apk add --no-cache postgresql-dev gcc python3 python3-dev musl-dev && \
  python3 -m ensurepip && \
  rm -r /usr/lib/python*/ensurepip && \
  pip install --upgrade pip setuptools && \
  rm -r /root/.cache && \
  pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple &&\
  rm requirements.txt

COPY . .

CMD ["gunicorn", "main:app", "-c", "./gunicorn.conf"]

好像是小了那么一点点了,!!!!!


image.png

重新调整docker compose文件:

version: "3.3"
services:
  dockerflask_nginxmini:
    container_name: dockerflask_nginx_yamlmini
    restart: always
    image: nginx
    privileged: true
    volumes:
        - /data/dockerflask/nginx_api.conf:/etc/nginx/conf.d/default.conf
    ports:
        - "80:80"
    links:
        - dockerflask_apimini

  dockerflask_pgdbmini:
    container_name: dockerflask_pg_yamlmini
    restart: always
    image: postgres:9.4
    environment:
          POSTGRES_PASSWORD: 123456
    volumes:
        - /data/dockerflask/pgdata:/var/lib/postgresql/data
    ports:
          - "5432:5432"

  dockerflask_redismini:
    container_name:  dockerflask_redis_yamlmini
    restart: always
    image: redis:4.0.14
    volumes:
        - /data/ dockerflask/redis.conf:/etc/redis/redis.conf
        - /data/dockerflask/redisdata:/data
    command: redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
    ports:
        - "6379:6379"

  dockerflask_apimini:
    build: .
    restart: always
    ports:
      - "80"
    depends_on:
      - dockerflask_redismini
      - dockerflask_pgdbmini

image.png

再跑一次!!!

[root@localhost dockerflask]# docker-compose -f docker-compose.yaml up -d
image.png

相关文章

网友评论

      本文标题:Centos下的纯命令记录Docker学习(12)-docker

      本文链接:https://www.haomeiwen.com/subject/aqrviktx.html