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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值