Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境

Dockerfile 拉取ubuntu:20.04镜像并配置python3.9的环境

1.构建镜像
requirements.txt里是Python要安装的包,文件和Dockerfile放在同一个目录。
代码结构
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第1张图片

2.一次性构建

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG C.UTF-8
RUN sed -i 's@http://archive.ubuntu.com/ubuntu/@http://mirrors.aliyun.com/ubuntu/@g' /etc/apt/sources.list

RUN apt-get update -qq
RUN mkdir -p /gemdale
WORKDIR /gemdale
COPY TBQA/requirements.txt requirements.txt
ADD TBQA /gemdale
RUN cp /gemdale/app.py /gemdale/scripts

RUN  apt-get clean && \
     apt-get update && \
     apt-get install -y libmysqlclient-dev tzdata  \
                        python3 python3-dev python3-pip libpcre3 libpcre3-dev  uwsgi-plugin-python3\
    && apt-get clean \
    && apt-get autoclean \
   && ln -sf /usr/bin/pip3 /usr/bin/pip && ln -sf /usr/bin/python3 /usr/bin/python \
   && pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple



RUN apt-get update -q \
    && apt-get install -y wget curl vim

CMD bash -c 'sh /gemdale/scripts/build.sh;sh /gemdale/scripts/run_sync.sh'

3.分开构建,先做个基础镜像,这样不用每次全部构建,很耽误时间

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG C.UTF-8
RUN sed -i 's@http://archive.ubuntu.com/ubuntu/@http://mirrors.aliyun.com/ubuntu/@g' /etc/apt/sources.list

RUN apt-get update -qq
COPY TBQA/requirements.txt requirements.txt

RUN  apt-get clean && \
     apt-get update && \
     apt-get install -y libmysqlclient-dev tzdata  \
                        python3 python3-dev python3-pip libpcre3 libpcre3-dev  uwsgi-plugin-python3\
    && apt-get clean \
    && apt-get autoclean \
   && ln -sf /usr/bin/pip3 /usr/bin/pip && ln -sf /usr/bin/python3 /usr/bin/python \
   && pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple



RUN apt-get update -q \
    && apt-get install -y wget curl vim

4.流水线里面运用基础镜像增量构建

FROM gem-acr-p-a01-registry.cn-shenzhen.cr.aliyuncs.com/devops/ubuntu20-python3.9:v0.1
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG C.UTF-8
###
# timezone setting
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone

RUN mkdir -p /gemdale
WORKDIR /gemdale
COPY TBQA/requirements.txt requirements.txt
ADD TBQA /gemdale
RUN cp /gemdale/app.py /gemdale/scripts
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

CMD bash -c 'sh /gemdale/scripts/build.sh;sh /gemdale/scripts/run_sync.sh'

5.云效流水线发布
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第2张图片
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第3张图片
docker 镜像启动环境变量生效
将 source /etc/profile 命令写入 ~/.bashrc中

   vim ~/.bashrc
   RUN echo source /etc/profile >> ~/.bashrc

这样自己制作比较麻烦,还有更简单的就是去到docker官网获取python3.9版本的基础镜像构建
Dockerfile

######## readme -- start ########
#build:
## docker build -t linice/python39:v1.0.0.1 .

# test:
## docker run --rm -it --entrypoint=bash --hostname=python39-test --name=python39-test -v /mnt/hgfs/:/mnt/hgfs/ linice/python39:v1.0.0.1

# access:
## docker exec -it python39-test bash
######## readme -- end ########
FROM python:3.9.10-buster

# locale
ENV LANG=C.UTF-8

# config timezone
RUN ln -snf /usr/share/zoneinfo/PRC /etc/localtime

# python config
RUN  mkdir /root/.pip/
COPY pip.conf /root/.pip/
RUN chmod 0644 /root/.pip/pip.conf

# python ext
RUN pip3 install Flask==1.1.2
RUN pip3 install joblib==1.1.0
RUN pip3 install onnxruntime==1.10.0
RUN pip3 install psutil==5.8.0
RUN pip3 install requests==2.26.0
RUN pip3 install torch==1.10.1
RUN pip3 install transformers==4.15.0
RUN pip3 install scikit-learn==0.24.2

pip.conf

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

7.云效发布docker容器
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第4张图片
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第5张图片
Dockerfile制作ubuntu:20.04镜像并配置python3.9的环境以及基于python镜像构建环境_第6张图片

部署脚本

#!/bin/bash
if [[ `docker inspect -f  '{{ .Name }}' ${appname}` == "/${appname}" ]]; then
  echo "停止${appname}"
  docker stop ${appname} && docker rm ${appname};
else
  echo "${appname}服务未启动"
fi

docker run -d --name=${appname} --restart=always --network=host -p ${hostport}:${containerport} -v /gemdale/logs:/gemdale/logs ${IMAGE}

IMAGE
gem-acr-p-a01-registry.cn-shenzhen.cr.aliyuncs.com/osale/osale-prediction-service:prod-2023-03-31-17-43-27

这个是镜像地址由自己定义的osale(空间)osale-prediction-service(仓库)prod-2023-03-31-17-43-27(标签)构成,如果定义了,这些变量缺一不可
M A V E N P R O F I L E − {MAVEN_PROFILE}- MAVENPROFILE{DATETIME}等于prod-2023-03-31-17-43-27

docker启动容器之后马上又自动关闭解决办法
问题描述:
centos 启动一个容器添加了-d 参数,但是docker ps 或者docker ps -a查看却已经退出了
退出原因
1、docker容器运行必须有一个前台进程, 如果没有前台进程执行,容器认为空闲,就会自行退出
2、容器运行的命令如果不是那些一直挂起的命令( 运行top,tail、循环等),就是会自动退出
3、这个是 docker 的机制问题
解决代码
CMD bash -c ‘pm2 start ecosystem.config.js --env test;tail -f /dev/null’

你可能感兴趣的:(ubuntu,linux,python)