fly-iot飞凡物联专栏:
https://blog.csdn.net/freewebsys/category_12219758.html
https://www.bilibili.com/video/BV1Nc411t7RU/?vd_source=4b290247452adda4e56d84b659b0c8a2
【fly-iot】(4):发布物联网IOT项目镜像2024版, 使用docker的builder方式打包 ,减少python镜像大小
镜像的dockerfile文件:
# 构建后端代码:
# docker build --tag flyiotadm/fly-iot-backend-python:v2024.01 .
# FROM python:3-alpine 使用最新的slim 版本。
#
FROM python:3.6-slim as builder
# 设置debian的镜像源
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list
# 设置python3的镜像源
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple && \
pip3 config set install.trusted-host mirrors.aliyun.com
# 安装需要的lib库
RUN apt update && apt-get install -y locales libpq-dev python3-dev gcc g++ make
ADD requirements.txt /root/requirements.txt
# 最后安装依赖包,然后删除缓存和 pyc 文件,还可以节省40MB 空间。
RUN cd /root && pip3 install -r requirements.txt && rm -rf /root/.cache/ && \
cd /usr/local/lib/python3.6/site-packages && find ./ -name *.pyc | xargs rm -f
# 删除 *.pyc 文件
# python 3.6-slim-server-base ef9e5b350248 689MB
# 大小从 689MB 减少到 652MB
# python 3.6-slim-server-base 7134559c9496 652MB
# 拷贝本地文件到目录
COPY . /data
# service
FROM python:3.6-slim
# 直接使用基础镜像然后拷贝 site-packages 安装包即可。
COPY --from=builder /data /data
COPY --from=builder /usr/local/lib/python3.6/site-packages /usr/local/lib/python3.6/site-packages
COPY --from=builder /usr/local/bin/flask /usr/local/bin/flask
#COPY --from=builder /data/docker-entrypoint.sh /usr/bin/
WORKDIR /data
# 设置python 的环境变量和 fask app文件。
ENV LC_ALL="C.UTF-8" LANG="C.UTF-8"
ENV PYTHONPATH="/data"
ENV FLASK_APP="/data/manage.py"
EXPOSE 7000 7001
ENTRYPOINT ["/data/docker-entrypoint.sh"]
CMD ["/bin/sh"]
发布到docker hub :
docker build --tag flyiotadm/fly-iot-backend-python:v2024.01 .
docker login 登陆之后就可以发布了:
发布
docker push flyiotadm/fly-iot-backend-python:v2024.01
The push refers to repository [docker.io/flyiotadm/fly-iot-backend-python]
5f70bf18a086: Layer already exists
71b22c8679b5: Pushed
744c6e4e2737: Pushed
42d08c675c51: Pushing [==================================================>] 189.1MB/189.1MB
c03fac33ac29: Pushed
61e79b15f086: Layer already exists
1d8d050c9cb8: Layer already exists
2119d4d70feb: Layer already exists
d0fa20bfdce7: Layer already exists
2edcec3590a4: Layer already exists
整个的docker-compose代码:
https://gitee.com/fly-iot/docker-compose
...
############### 使用 iot-backend-python 版本 ###############
iot-backend-python:
image: flyiotadm/fly-iot-backend-python:v2024.01
# build:
# context: .
# dockerfile: docker/DockerfileMysql
container_name: fly-iot-backend-python
ports:
- "7000:7000"
volumes:
- "./python-conf/config-mysql.yml:/data/config/config.yml"
- "../fly-iot-backend-python:/data"
links:
- mysql-iot:mysql-iot
restart: always
entrypoint: /data/docker-entrypoint.sh
depends_on:
- mysql-iot
- tdengine