记一次dockerfile打包运行python程序
*以注册程序login为例
一、基于centos 7 打包python 3.9 images
官方镜像(需注意操作系统)
查看镜像的构建历史,从中找出操作系统痕迹。
[root@i-9d1eq6no ~]# docker images | grep python
python 3.7 2dfc79879562 8 days ago 907MB
[root@i-9d1eq6no ~]# docker history 2dfc79879562
IMAGE CREATED CREATED BY SIZE COMMENT
2dfc79879562 8 days ago /bin/sh -c #(nop) CMD ["python3"] 0B
<missing> 8 days ago /bin/sh -c set -eux; wget -O get-pip.py "$… 10.2MB
<missing> 8 days ago /bin/sh -c #(nop) ENV PYTHON_GET_PIP_SHA256… 0B
<missing> 8 days ago /bin/sh -c #(nop) ENV PYTHON_GET_PIP_URL=ht… 0B
<missing> 8 days ago /bin/sh -c #(nop) ENV PYTHON_SETUPTOOLS_VER… 0B
<missing> 8 days ago /bin/sh -c #(nop) ENV PYTHON_PIP_VERSION=22… 0B
<missing> 8 days ago /bin/sh -c set -eux; for src in idle3 pydoc… 32B
<missing> 8 days ago /bin/sh -c set -eux; wget -O python.tar.xz… 43.3MB
<missing> 8 days ago /bin/sh -c #(nop) ENV PYTHON_VERSION=3.7.16 0B
<missing> 8 days ago /bin/sh -c #(nop) ENV GPG_KEY=0D96DF4D4110E… 0B
<missing> 8 days ago /bin/sh -c set -eux; apt-get update; apt-g… 18.5MB
<missing> 8 days ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0B
<missing> 8 days ago /bin/sh -c #(nop) ENV PATH=/usr/local/bin:/… 0B
<missing> 8 days ago /bin/sh -c set -ex; apt-get update; apt-ge… 529MB
<missing> 8 days ago /bin/sh -c apt-get update && apt-get install… 152MB
<missing> 8 days ago /bin/sh -c set -ex; if ! command -v gpg > /… 19MB
<missing> 8 days ago /bin/sh -c set -eux; apt-get update; apt-g… 10.7MB
<missing> 8 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 8 days ago /bin/sh -c #(nop) ADD file:513c5d5e501279c21… 124MB
看到apt-get就知道是Ubuntu操作系统,这里介绍Centos操作系统,所以自己构建一个。
拉取基础镜像centos,编写dockerfile也可以拉取alpine。
[root@i-9d1eq6no ~]# docker pull centos:7
[root@i-9d1eq6no ~]# docker images | grep centos
centos 7 eeb6ee3f44bd 17 months ago 204MB
基础镜像200+M,编写dockerfile
[root@i-9d1eq6no opt]# pwd
/opt
[root@i-9d1eq6no opt]# cat > Dockerfile << EOF
FROM centos:7
WORKDIR /usr/local/python3
RUN yum -y install gcc.x86_64 gcc-c++.x86_64zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel && yum -y install vim wget && wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz && tar -xf Python-3.9.0.tgz && yum clean all
WORKDIR /usr/local/python3/Python-3.9.0
RUN yum -y install gcc automake autoconf libtool make && ./configure --prefix=/usr/local/python3 && make && make install &&make clean && ln -s /usr/local/python3/bin/python3 /usr/bin/python3 && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 && yum clean all
CMD ['/usr/bin/python3']
EOF
docker执行打包centos-py3
[root@i-9d1eq6no opt]# docker build -t centos-py3:v1 .
[+] Building 0.0s (9/9) FINISHED
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 722B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [1/5] FROM docker.io/library/centos:7 0.0s
=> CACHED [2/5] WORKDIR /usr/local/python3 0.0s
=> CACHED [3/5] RUN yum -y install gcc.x86_64 gcc-c++.x86_64zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-d 0.0s
=> CACHED [4/5] WORKDIR /usr/local/python3/Python-3.9.0