记使用docker部署项目出现问题

我的docker-compose.yml内容如下:

version: "3"
services:
  mysql:
    image: mysql:latest # mysql:oracle
    restart: always
    container_name: mysql
    command: 
      - --lower_case_table_names=1
    ports:
      - 3306:3306
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/setup.sql
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "mysql"]
      timeout: 5s
      retries: 20

  my_server:
    build: .
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
    ports:
      - 9999:9999
    links:
      - mysql
    environment:
      - ADMIN_PASSWD=admin   # 这个会覆盖Dockerfile里面的内容

  

Dockerfile内容如下:


FROM sanicframework/sanic:3.8-latest

WORKDIR /sanic/my_server

COPY . /sanic/my_server

COPY . .

RUN pip install -r requirements.txt

EXPOSE 9999

CMD ["python", "main.py"]

使用docker-compose.yml部署项目时候,提示

RuntimeError:‘cryptography’ package isrequired for sha256_password or caching_sha2_password,

出现的原因是mysql8.0+版本使用了新的密码校验插件,需要安装cryptography这个库,

当你在requirement.txt加上cryptography==39.0.1后,重新运行docker compose up这条命令时候,又出现如下错误

记使用docker部署项目出现问题_第1张图片

也就是说,安装cryptography这个库的时候,下载依赖出现问题,解决这个问题就是安装一些必备的库,在Dockerfile文件添加以下内容

RUN apk update

RUN apk --update add gcc musl-dev libffi-dev openssl-dev 

RUN apk add gcc musl-dev python3-dev libffi-dev openssl-dev cargo

RUN pip install -r requirements.txt

EXPOSE 9999

RUN apk del --purge gcc musl-dev libffi-dev openssl-dev

就可以解决库安装问题,完整的dockerfile如下:


FROM sanicframework/sanic:3.8-latest

WORKDIR /sanic/my_server

COPY . /sanic/my_server

COPY . .

RUN apk update

RUN apk --update add gcc musl-dev libffi-dev openssl-dev 

RUN apk add gcc musl-dev python3-dev libffi-dev openssl-dev cargo

RUN pip install -r requirements.txt

EXPOSE 9999

RUN apk del --purge gcc musl-dev libffi-dev openssl-dev

CMD ["python", "main.py"]

当你再次运行docker compose up时候,又出现问题,提示如下错误

 /bin/sh: ./start.sh: not found

刚开始我怀疑是没有将这个文件复制到容器里面,所以加了如下内容

记使用docker部署项目出现问题_第2张图片

再次运行docker compose up,还是找不到not found start.sh

最终没有运行start.sh文件,而是直接跑main.py,让容器先跑起来,再 exec 到 容器里 vi .start.sh,发现后面多了一个^M,也就是换行符引起的,最后在Dokcerfile引入如下内容完美解决

RUN dos2unix ./start.sh

完整的Dockerfile如下:

ARG ADMIN_PASSWD

FROM sanicframework/sanic:3.8-latest

WORKDIR /sanic/my_server

COPY . /sanic/my_server

COPY . .

ENV ADMIN_PASSWD=admin

RUN apk update

RUN apk --update add gcc musl-dev libffi-dev openssl-dev dos2unix
#
#  The Rust available by default in Alpine < 3.12 is older than the minimum supported version.
#  https://cryptography.io/en/latest/installation.html
# 
RUN apk add gcc musl-dev python3-dev libffi-dev openssl-dev dos2unix

RUN dos2unix ./start.sh

RUN pip install -r requirements.txt

EXPOSE 9999

RUN apk del --purge gcc musl-dev libffi-dev openssl-dev dos2unix

VOLUME ["/sanic/license/config", "/sanic/license/log"]

CMD ./start.sh $ADMIN_PASSWD


你可能感兴趣的:(docker,容器,运维)