2022-10-15,通过Dockerfile构建Docker镜像并运行
好了,废话不多说,直接上才艺:
一、准备Dockerfile文件
文件目录:
-
[root@localhost flask]# pwd
-
/root/flask
-
[root@localhost flask]# ls -lrth
-
total 12K
-
drwxr-xr-x 5 root root 138 Oct 14 18:46 welink
-
-rw-r--r-- 1 root root 233 Oct 15 00:32 uwsgi.ini
-
-rw-r--r-- 1 root root 731 Oct 15 21:22 Dockerfile
-
-rw-r--r-- 1 root root 3.2K Oct 15 21:30 nginx.conf
welink:web项目文件夹,这里面是flask工程文件,包括项目入口文件app.py。
uwsgi.ini:uwsgi的配置文件,描述uwsgi的运行参数。
Dockerfile:构建镜像所需的配置文件,记录镜像产生需要做哪些动作,例如:基于什么镜像来构建?需要创建哪些目录?需要安装什么中间件、数据库等等,以及运行容器时默认执行什么命令都可以定义清楚。
nginx.conf:nginx的配置文件,构建镜像时会打包到镜像中。
welink文件夹:
-
[root@localhost flask]# cd welink/
-
[root@localhost welink]#
-
[root@localhost welink]# ls -lrth
-
total 16K
-
-rw-r--r-- 1 root root 69 Oct 14 09:44 pyvenv.cfg
-
lrwxrwxrwx 1 root root 3 Oct 14 09:44 lib64 -> lib
-
drwxr-xr-x 3 root root 23 Oct 14 09:44 lib
-
drwxr-xr-x 2 root root 6 Oct 14 09:44 include
-
-rw-r--r-- 1 root root 61 Oct 14 09:50 pip-selfcheck.json
-
drwxr-xr-x 2 root root 186 Oct 14 09:55 bin
-
-rw-r--r-- 1 root root 708 Oct 14 10:53 app.py
-
-rw-r--r-- 1 root root 174 Oct 14 12:47 requirements.txt
uwsgi.ini文件内容:
-
[uwsgi]
-
http = 0.0.0.0:5007
-
chdir = /flask_app/welink
-
master = true
-
# processes = 2
-
wsgi-file = %(chdir)/app.py
-
pidfile = %(chdir)/uwsgi/uwsgi.pid
-
daemonize = %(chdir)/uwsgi/uwsgi.log
-
callable = app
-
buffer-size = 8192
-
vacuum = true
Dockerfile文件内容:
-
FROM centos:7
-
-
RUN mkdir -p /flask_app/welink/uwsgi
-
COPY ./welink/ /flask_app/welink
-
WORKDIR /flask_app/welink
-
-
RUN yum -y install epel-release && \
-
yum -y install python3 && \
-
yum -y install python3-pip && \
-
yum -y install gcc-c && \
-
yum -y install pcre pcre-devel && \
-
yum -y install openssl openssl-devel && \
-
yum -y install python3-devel && \
-
pip3 install uwsgi -i https://pypi.douban.com/simple && \
-
yum -y install nginx && \
-
pip3 install -r requirements.txt -i https://pypi.douban.com/simple
-
-
COPY nginx.conf /etc/nginx/nginx.conf
-
COPY uwsgi.ini /flask_app/welink/uwsgi
-
-
ENV LANG="en_US.UTF-8"
-
-
EXPOSE 81
-
-
CMD sh -c 'uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"'
nginx.conf文件内容:
-
-
#user nobody;
-
worker_processes 1;
-
-
#error_log logs/error.log;
-
#error_log logs/error.log notice;
-
#error_log logs/error.log info;
-
-
#pid logs/nginx.pid;
-
-
-
events {
-
worker_connections 1024;
-
}
-
-
-
http {
-
include mime.types;
-
default_type application/octet-stream;
-
-
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
-
# '$status $body_bytes_sent "$http_referer" '
-
# '"$http_user_agent" "$http_x_forwarded_for"';
-
-
#access_log logs/access.log main;
-
-
sendfile on;
-
#tcp_nopush on;
-
-
#keepalive_timeout 0;
-
keepalive_timeout 65;
-
-
#gzip on;
-
-
server {
-
listen 81;
-
server_name localhost;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
root html;
-
index index.html index.htm;
-
}
-
-
location /hello {
-
proxy_pass http://127.0.0.1:5007/welink;
-
proxy_redirect off;
-
proxy_set_header Host $host;
-
proxy_set_header X-Real-IP $remote_addr;
-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-
}
-
-
location /post {
-
proxy_pass http://127.0.0.1:5007/welink/send_msg;
-
proxy_redirect off;
-
proxy_set_header Host $host;
-
proxy_set_header X-Real-IP $remote_addr;
-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-
}
-
-
#error_page 404 /404.html;
-
-
# redirect server error pages to the static page /50x.html
-
#
-
error_page 500 502 503 504 /50x.html;
-
location = /50x.html {
-
root html;
-
}
-
-
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
-
#
-
#location ~ \.php$ {
-
# proxy_pass http://127.0.0.1;
-
#}
-
-
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
-
#
-
#location ~ \.php$ {
-
# root html;
-
# fastcgi_pass 127.0.0.1:9000;
-
# fastcgi_index index.php;
-
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
-
# include fastcgi_params;
-
#}
-
-
# deny access to .htaccess files, if Apache's document root
-
# concurs with nginx's one
-
#
-
#location ~ /\.ht {
-
# deny all;
-
#}
-
}
-
-
-
# another virtual host using mix of IP-, name-, and port-based configuration
-
#
-
#server {
-
# listen 8000;
-
# listen somename:8080;
-
# server_name somename alias another.alias;
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
-
# HTTPS server
-
#
-
#server {
-
# listen 443 ssl;
-
# server_name localhost;
-
-
# ssl_certificate cert.pem;
-
# ssl_certificate_key cert.key;
-
-
# ssl_session_cache shared:SSL:1m;
-
# ssl_session_timeout 5m;
-
-
# ssl_ciphers HIGH:!aNULL:!MD5;
-
# ssl_prefer_server_ciphers on;
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
}
二、构建Docker镜像
-
[root@localhost flask]# docker build . -t welink:v5 --network host
-
Sending build context to Docker daemon 17.27MB
-
Step 1/9 : FROM centos:7
-
---> eeb6ee3f44bd
-
Step 2/9 : RUN mkdir -p /flask_app/welink/uwsgi
-
---> Using cache
-
---> 2287baf70f92
-
Step 3/9 : COPY ./welink/ /flask_app/welink
-
---> Using cache
-
---> f2e47565b58d
-
Step 4/9 : WORKDIR /flask_app/welink
-
---> Using cache
-
---> e7ef6cbf9fe0
-
Step 5/9 : RUN yum -y install epel-release && yum -y install python3 && yum -y install python3-pip && yum -y install gcc-c && yum -y install pcre pcre-devel && yum -y install openssl openssl-devel && yum -y install python3-devel && pip3 install uwsgi -i https://pypi.douban.com/simple && yum -y install nginx && pip3 install -r requirements.txt -i https://pypi.douban.com/simple
-
---> Using cache
-
---> 8a2ddae00489
-
Step 6/9 : COPY nginx.conf /etc/nginx/nginx.conf
-
---> af8a26adeb18
-
Step 7/9 : COPY uwsgi.ini /flask_app/welink/uwsgi
-
---> b75213ee312b
-
Step 8/9 : EXPOSE 81
-
---> Running in b42cd57d939f
-
Removing intermediate container b42cd57d939f
-
---> 744484ebcc2f
-
Step 9/9 : CMD sh -c 'uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"'
-
---> Running in 2fc336a899f2
-
Removing intermediate container 2fc336a899f2
-
---> 60ac3562ed19
-
Successfully built 60ac3562ed19
-
Successfully tagged welink:v5
第一次构建的时候会因为网络、yum源配置等因素比较慢,后续沟通docker会基于cache而变得非常快,这里已经是第5个版本了,所以构建的非常快。
查看构建成功的镜像:
-
[root@localhost flask]# docker images
-
REPOSITORY TAG IMAGE ID CREATED SIZE
-
welink v5 60ac3562ed19 1 second ago 675MB
-
welink v4 d019f46ee4be About an hour ago 675MB
-
welink v3 0fd30675b7ac 21 hours ago 675MB
这里就可以看出,通过Dockerfile构建的镜像比docker commit直接打包的会精简,不会有太多冗余数据,后续可以很方便的通过修改Dockerfile来构建新的镜像。
三、生成Docker容器
-
[root@localhost flask]# docker run -id --name welink01 -p 81:81 welink:v5
-
614cea779ed61c80a51ed9833186131c928aefa6e9a8f99c0a83ec05250f7e52
-
[root@localhost flask]#
-
[root@localhost flask]# docker ps
-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-
614cea779ed6 welink:v5 "/bin/sh -c 'sh -c '…" 9 seconds ago Up 7 seconds 0.0.0.0:81->81/tcp, :::81->81/tcp welink01
四、访问测试
-
[root@localhost flask]# curl 127.0.0.1:81/hello
-
welcome to flask
注意:这里可以直接从物理机访问容器里的web服务,说明容器里的web项目已成功运行,并通过端口映射出来了。
五、维护命令
-
# 进docker
-
[root@localhost flask]# docker exec -it welink01 /bin/bash
-
[root@614cea779ed6 welink]#
-
[root@614cea779ed6 welink]# ps -ef|grep nginx
-
root 1 0 0 14:06 ? 00:00:00 sh -c uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"
-
root 11 1 0 14:06 ? 00:00:00 nginx: master process nginx -g daemon off;
-
nginx 12 11 0 14:06 ? 00:00:00 nginx: worker process
-
root 33 18 0 14:42 pts/0 00:00:00 grep --color=auto nginx
-
[root@614cea779ed6 welink]#
-
[root@614cea779ed6 welink]# ps -ef|grep uwsgi
-
root 1 0 0 14:06 ? 00:00:00 sh -c uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"
-
root 10 1 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
-
root 16 10 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
-
root 17 10 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
-
root 35 18 0 14:42 pts/0 00:00:00 grep --color=auto uwsgi
-
-
# 出docker
-
[root@614cea779ed6 welink]# exit
-
exit
-
-
# 在docker执行命令
-
[root@localhost flask]# docker exec -it welink01 ps -ef|grep nginx
-
root 11 1 0 14:06 ? 00:00:00 nginx: master process nginx
-
nginx 12 11 0 14:06 ? 00:00:00 nginx: worker process
-
-
# 停docker
-
[root@localhost flask]# docker stop welink01
-
welink01
-
-
# 删docker
-
[root@localhost flask]# docker rm welink01
-
welink01
-
-
# 删镜像
-
[root@localhost flask]# docker rmi 3ecb659a87dc
-
Untagged: welink:v4
-
Deleted: sha256:3ecb659a87dcd3e940ded320f5fb490392f9e9ead3f01cd68e06113542e06339
六、导出镜像
-
[root@localhost flask]# docker save 60ac3562ed19 > welink_v5.tar
-
[root@localhost flask]# ls -hl
-
total 661M
-
-rw-r--r-- 1 root root 731 Oct 15 21:22 Dockerfile
-
-rw-r--r-- 1 root root 3.2K Oct 15 21:30 nginx.conf
-
-rw-r--r-- 1 root root 233 Oct 15 00:32 uwsgi.ini
-
drwxr-xr-x 5 root root 138 Oct 14 18:46 welink
-
-rw-r--r-- 1 root root 661M Oct 16 13:00 welink_v5.tar
这时就可以把镜像传到其它服务器进行导入了。
七、导入镜像
-
[root@appt tmp]# docker load < welink_v5.tar
-
07840385d029: Loading layer [==================================================>] 3.072kB/3.072kB
-
ed93135292b9: Loading layer [==================================================>] 17.26MB/17.26MB
-
69bc51ea49cd: Loading layer [==================================================>] 463.5MB/463.5MB
-
b7f0d7d30a92: Loading layer [==================================================>] 6.144kB/6.144kB
-
e8601482ae40: Loading layer [==================================================>] 3.584kB/3.584kB
-
Loaded image ID: sha256:60ac3562ed198dc014a5dbe00756c070d7584d57a7834f7a75776f24146d75c5
-
[root@appt tmp]#
-
[root@appt tmp]# docker images
-
REPOSITORY TAG IMAGE ID CREATED SIZE
-
<none> <none> 60ac3562ed19 15 hours ago 675MB
-
nginx latest 51086ed63d8c 10 days ago 142MB
-
weopsrdp latest 2ff3d656855c 5 months ago 148MB
-
dushixiang/guacd latest b578fc3fd8f3 9 months ago 281MB
-
python36e 1.0 5171821bb418 15 months ago 760MB
-
python27e 1.0 974165f81357 15 months ago 721MB
-
flaskdemo/demo latest 31e995c212d6 4 years ago 210MB
-
[root@appt tmp]#
-
[root@appt tmp]# docker tag 60ac3562ed19 welink:v5
-
[root@appt tmp]#
-
[root@appt tmp]# docker images
-
REPOSITORY TAG IMAGE ID CREATED SIZE
-
welink v5 60ac3562ed19 15 hours ago 675MB
-
nginx latest 51086ed63d8c 10 days ago 142MB
-
weopsrdp latest 2ff3d656855c 5 months ago 148MB
-
dushixiang/guacd latest b578fc3fd8f3 9 months ago 281MB
-
python36e 1.0 5171821bb418 15 months ago 760MB
-
python27e 1.0 974165f81357 15 months ago 721MB
-
flaskdemo/demo latest 31e995c212d6 4 years ago 210MB
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfiebg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13