[fly-iot Feifan IoT] (15): Open source Python IOT project, solves the MySQL database problem, packages and releases the image to the docker-hub warehouse, reduces the size, and releases the 2024-01 version

Preface


fly-iot Feifan IoT Column:
https://blog.csdn.net/freewebsys/category_12219758.html

1. Video demonstration address


https://www.bilibili.com/video/BV1Nc411t7RU/?vd_source=4b290247452adda4e56d84b659b0c8a2

[fly-iot] (4): Release the 2024 version of the Internet of Things IOT project image, packaged using docker’s builder method to reduce the size of the python image

Insert image description here

2. Python performs docker packaging and uses the builder method to reduce the image size


Mirrored 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"]

3. Publish to docker hub

Publish to docker hub:

docker build --tag flyiotadm/fly-iot-backend-python:v2024.01 .

After docker login logs in, you can publish:
Publish

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 

4. Then you can start using the new image

The entire docker-compose code:
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

Guess you like

Origin blog.csdn.net/freewebsys/article/details/135425745