Dockerfile制作运行python的镜像

Dockerfile制作运行python的镜像

文件结构

在这里插入图片描述

#!/bin/bash
FROM ubuntu:18.04
SHELL ["/bin/bash", "-c"]

ENV version 2.0.1
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak

RUN echo 'deb http://mirrors.aliyun.com/ubuntu bionic main multiverse restricted universe' >> /etc/apt/sources.list

RUN echo 'deb http://mirrors.aliyun.com/ubuntu bionic-updates main multiverse restricted universe' >> /etc/apt/sources.list

RUN echo 'deb http://mirrors.aliyun.com/ubuntu bionic-security main multiverse restricted universe' >> /etc/apt/sources.list

RUN echo 'deb http://mirrors.aliyun.com/ubuntu bionic-proposed main multiverse restricted universe' >> /etc/apt/sources.list

RUN echo 'deb http://mirrors.aliyun.com/ubuntu bionic-backports main multiverse restricted universe' >> /etc/apt/sources.list


RUN echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
RUN echo 'nameserver 223.5.5.5' >> /etc/resolv.conf
RUN echo 'nameserver 223.6.6.6' >> /etc/resolv.conf
RUN apt-get update

RUN apt-get install wget net-tools telnet tree nmap sysstat lrzsz dos2unix  vim less selinux-utils ntpdate unzip zip -y

# Step1: Install python3

RUN cd /root \
    && mkdir -p tools
	
COPY ./Miniconda3-py37_4.11.0-Linux-x86_64.sh /root/tools/

RUN /bin/bash /root/tools/Miniconda3-py37_4.11.0-Linux-x86_64.sh -b -p /root/Miniconda3/ \
    && export PATH=$PATH:/root/Miniconda3/bin \
    && rm /root/tools/Miniconda3-py37_4.11.0-Linux-x86_64.sh

RUN /root/Miniconda3/bin/conda create -n py36 python=3.6 -y

RUN rm -rf /root/ibox-server_backend-grpc

RUN cd /root \
    && mkdir -p ibox-server_backend-grpc

COPY ./*  /root/ibox-server_backend-grpc/

RUN ls -l /root/ibox-server_backend-grpc/

RUN cd /root/ibox-server_backend-grpc

# RUN export PATH=/Miniconda3/bin:$PATH \
#     && source activate py36 \
#     && pip3 install numpy -i https://mirrors.aliyun.com/pypi/simple/ \
#     && pip3 install sklearn -i https://mirrors.aliyun.com/pypi/simple/ 

WORKDIR /root/ibox-server_backend-grpc/

ENTRYPOINT ["/root/Miniconda3/envs/py36/bin/python", "-u", "main.py"]

python逻辑

import sys
import time
import os
import sched
import datetime
import logging

# 此处必须,声明之后才可以通过docker logs 看到日志打印
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# 初始化sched模块的scheduler类  
# 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。  
schedule = sched.scheduler(time.time, time.sleep)

# 被周期性调度触发的函数  
def execute_command(cmd, inc):
    logging.info(datetime.datetime.now())
    schedule.enter(inc, 0, execute_command, (cmd, inc))


def main(cmd, inc=10):
    # enter四个参数分别为:间隔时间、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数、给该触发函数的参数(tuple形式)  
    schedule.enter(0, 0, execute_command, (cmd, inc))
    schedule.run()

if __name__ == '__main__':
    main("ping www.baidu.com", 2) #2秒打印一次log

docker build

docker build -t ibox-server_backend-grpc .

docker run

docker run -d --name=ibox-server_backend-grpc ibox-server_backend-grpc

查看日记

docker logs <contain-id> -f

参考

Dockerfile制作运行python的镜像

猜你喜欢

转载自blog.csdn.net/u010006102/article/details/123326676