基于Dockerfile创建镜像

本文最后更新于:2 年前

Dockfile介绍

Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令。Docker程序将这些Dockerfile指令翻译真正的Linux命令。Dockerfile有自己书写格式和支持的命令,Docker程序解决这些命令间的依赖关系,类似于Makefile。Docker程序将读取Dockerfile,根据指令生成定制的image。

pip3 install requirement.txt文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
aiohttp==3.8.3
aiosignal==1.3.1
APScheduler==3.9.1.post1
async-generator==1.10
async-timeout==4.0.2
asynctest==0.13.0
attrdict==2.0.1
attrs==22.1.0
base58==2.1.1
bcrypt==4.0.1
beautifulsoup4==4.11.1
bitarray==2.6.0
bscscan-python==2.0.0
certifi==2022.12.7
cffi==1.15.1
chardet==5.0.0
charset-normalizer==2.1.1
click==8.1.3
cloudscraper==1.2.66
crypto==1.4.1
cryptography==38.0.3
cssselect==1.2.0
cytoolz==0.12.0
Deprecated==1.2.13
ecdsa==0.15
eth-abi==2.2.0
eth-account==0.4.0
eth-hash==0.3.3
eth-keyfile==0.5.1
eth-keys==0.2.4
eth-rlp==0.2.1
eth-typing==2.3.0
eth-utils==1.10.0
exceptiongroup==1.0.4
frozenlist==1.3.3
goose3==3.1.12
greenlet==2.0.1
h11==0.14.0
hexbytes==0.3.0
httpcore==0.12.3
httpx==0.16.1
idna==3.4
importlib-metadata==5.0.0
importlib-resources==5.10.0
ipfshttpclient==0.7.0
jieba==0.42.1
joblib==1.2.0
jsonschema==3.2.0
jwcrypto==1.4.2
kazoo==2.9.0
langdetect==1.0.9
lru-dict==1.1.8
lxml==4.9.1
multiaddr==0.0.9
multidict==6.0.2
mysqlclient==2.1.1
Naked==0.1.32
ndg-httpsclient==0.5.1
netaddr==0.8.0
nltk==3.7
numpy==1.23.5
outcome==1.2.0
packaging==21.3
pandas==1.5.2
paramiko==2.12.0
parsimonious==0.8.1
pika==1.3.1
Pillow==9.3.0
pip==22.3.1
pkgutil_resolve_name==1.3.10
prettytable==3.5.0
protobuf==3.20.3
psycopg2-binary==2.9.5
py-solc-x==1.1.1
pyahocorasick==1.4.4
pyasn1==0.4.8
pycparser==2.21
pycryptodome==3.15.0
pycurl==7.45.1
pyDes==2.0.1
PyMySQL==1.0.2
PyNaCl==1.5.0
pyOpenSSL==22.1.0
pyparsing==3.0.9
pyrsistent==0.19.2
pysha3==1.0.2
PySocks==1.7.1
pysqlite3==0.4.7
python-consul==1.1.0
python-dateutil==2.8.2
python-memcached==1.59
pytz==2022.6
pytz-deprecation-shim==0.1.0.post0
PyYAML==6.0
redis==4.3.5
regex==2022.10.31
requests==2.28.1
requests-toolbelt==0.10.1
rfc3986==1.5.0
rlp==1.2.0
scikit-learn==1.1.3
scipy==1.9.3
selenium==4.6.0
selenium-stealth==1.0.6
semantic-version==2.10.0
setuptools==49.2.1
shellescape==3.8.1
six==1.16.0
sniffio==1.3.0
sockjs==0.11.0
sockjs-tornado==1.0.7
solc==0.0.0a0
sortedcontainers==2.4.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.44
suds-py3==1.4.5.0
threadpoolctl==3.1.0
toolz==0.12.0
tornado==6.2
tqdm==4.64.1
trio==0.22.0
trio-websocket==0.9.2
tronapi==3.1.6
tronpy==0.2.6
trx-utils==1.0.4
typing_extensions==4.4.0
tzdata==2022.6
tzlocal==4.2
ujson==5.5.0
undetected-chromedriver==3.1.7
urllib3==1.26.12
varint==1.0.2
wcwidth==0.2.5
web3==5.8.0
web3-input-decoder==0.1.2
websockets==8.1
Whoosh==2.7.4
wrapt==1.14.1
wsproto==1.2.0
yarl==1.8.1

dockerfile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#基于哪个镜像生成新的镜像
FROM ubuntu:20.04

#作者名
MAINTAINER 500docker

#设置TIMEZONE环境变量
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ UTC

#设置支持中文
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y locales
RUN locale-gen zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8
RUN apt-get clean
RUN rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

#执行命令
#替换为阿里源
RUN sed -i 's#http://archive.ubuntu.com/#http://mirrors.aliyun.com/#' /etc/apt/sources.list \
&& sed -i 's#http://security.ubuntu.com/#http://mirrors.aliyun.com/#' /etc/apt/sources.list

#更新软件源并安装软件
RUN apt-get update -y \
&& apt-get -y install iputils-ping \
&& apt-get -y install wget \
&& apt-get -y install net-tools \
&& apt-get -y install vim \
&& apt-get -y install gcc \
&& apt-get -y install zip \
&& apt-get -y install unzip \
&& apt-get -y install libmysqlclient-dev \
&& apt-get -y install default-libmysqlclient-dev \
&& apt-get -y install python3-pymysql \
&& apt-get -y install python3-dev \
&& apt-get -y install python3.9-dev \
&& apt-get -y install build-essential \
&& apt-get -y install libssl-dev \
&& apt-get -y install libcurl4-openssl-dev \
&& apt-get -y install libjsoncpp-dev \
&& apt-get -y install openssh-server \
&& apt-get -y install python3.9 \
&& apt-get -y install python3-pip python3-dev \
&& cd /usr/local/bin \
&& rm -f python \
&& rm -f python3 \
&& rm -f pip \
&& rm -f pip3 \
&& ln -s /usr/bin/python3.9 python \
&& ln -s /usr/bin/python3.9 python3 \
&& ln -s /usr/bin/pip3 pip \
&& ln -s /usr/bin/pip3 pip3 \
&& python -m pip install --upgrade pip \
&& apt-get clean \
&& rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

# 安装chrome
ADD chromedriver /home/service/chromedriver
ADD google-chrome-stable_current_amd64.deb /home/service/google-chrome-stable_current_amd64.deb
RUN apt-get update && apt-get install -y -f /home/service/google-chrome-stable_current_amd64.deb
RUN ln -s /usr/bin/google-chrome /usr/bin/chrome
RUN mv /home/service/chromedriver /usr/bin/chromedriver
RUN chmod +x /usr/bin/chromedriver

# 创建用户和用户组,并设置权限
RUN mkdir -p /home/service
RUN groupadd -g 551 service && useradd -g service -d /home/service -u 551 service; chown -R service.service /home/service

# 安装requirement
ADD requirement.txt /home/service/requirement.txt
RUN pip3 install -r /home/service/requirement.txt
RUN apt-get clean && rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

#设置密码
RUN echo 'root:yw#500wan.com' | chpasswd

#允许root用户登入
RUN sed -ri 's/^#PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config

# 指定工作目录
WORKDIR /home/service

#创建sshd目录
RUN mkdir /var/run/sshd

# 声明端口
EXPOSE 22

#开始ssh服务
CMD ["/usr/sbin/sshd", "-D"]

构建镜像命令

1
2
3
4
docker build -t 500docker/ubuntu20-python3.9:v1.0.2 ./

# 500docker/ubuntu20-python3.9:v1.0.2 - 镜像名称
# ./ - Dockerfile文件所在目录

提交仓库 - push本地仓库到远程docker.hub

1
docker push 500docker/ubuntu20-python3.9:v1.0.2

生成容器命令

1
2
3
4
5
6
7
8
9
10
docker run -itd --name 'cos_email' \
--restart=always \
--net=host \
--user=$UID:$(id -g $USER) \
--privileged=true \
-v /etc/esun:/etc/esun \
-v /etc/rc.d/init.d:/etc/rc.d/init.d \
-v /home/service/cos_email:/home/service/cos_email \
-d 500docker/ubuntu20-python3.9:v1.0.1 \
/bin/bash