• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

2022-10-15,通过Dockerfile构建Docker镜像并运行

武飞扬头像
城风蔚然
帮助1

好了,废话不多说,直接上才艺:

一、准备Dockerfile文件

文件目录:

  1.  
    [root@localhost flask]# pwd
  2.  
    /root/flask
  3.  
    [root@localhost flask]# ls -lrth
  4.  
    total 12K
  5.  
    drwxr-xr-x 5 root root 138 Oct 14 18:46 welink
  6.  
    -rw-r--r-- 1 root root 233 Oct 15 00:32 uwsgi.ini
  7.  
    -rw-r--r-- 1 root root 731 Oct 15 21:22 Dockerfile
  8.  
    -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文件夹:

  1.  
    [root@localhost flask]# cd welink/
  2.  
    [root@localhost welink]#
  3.  
    [root@localhost welink]# ls -lrth
  4.  
    total 16K
  5.  
    -rw-r--r-- 1 root root 69 Oct 14 09:44 pyvenv.cfg
  6.  
    lrwxrwxrwx 1 root root 3 Oct 14 09:44 lib64 -> lib
  7.  
    drwxr-xr-x 3 root root 23 Oct 14 09:44 lib
  8.  
    drwxr-xr-x 2 root root 6 Oct 14 09:44 include
  9.  
    -rw-r--r-- 1 root root 61 Oct 14 09:50 pip-selfcheck.json
  10.  
    drwxr-xr-x 2 root root 186 Oct 14 09:55 bin
  11.  
    -rw-r--r-- 1 root root 708 Oct 14 10:53 app.py
  12.  
    -rw-r--r-- 1 root root 174 Oct 14 12:47 requirements.txt

uwsgi.ini文件内容:

  1.  
    [uwsgi]
  2.  
    http = 0.0.0.0:5007
  3.  
    chdir = /flask_app/welink
  4.  
    master = true
  5.  
    # processes = 2
  6.  
    wsgi-file = %(chdir)/app.py
  7.  
    pidfile = %(chdir)/uwsgi/uwsgi.pid
  8.  
    daemonize = %(chdir)/uwsgi/uwsgi.log
  9.  
    callable = app
  10.  
    buffer-size = 8192
  11.  
    vacuum = true

Dockerfile文件内容:

  1.  
    FROM centos:7
  2.  
     
  3.  
    RUN mkdir -p /flask_app/welink/uwsgi
  4.  
    COPY ./welink/ /flask_app/welink
  5.  
    WORKDIR /flask_app/welink
  6.  
     
  7.  
    RUN yum -y install epel-release && \
  8.  
    yum -y install python3 && \
  9.  
    yum -y install python3-pip && \
  10.  
    yum -y install gcc-c && \
  11.  
    yum -y install pcre pcre-devel && \
  12.  
    yum -y install openssl openssl-devel && \
  13.  
    yum -y install python3-devel && \
  14.  
    pip3 install uwsgi -i https://pypi.douban.com/simple && \
  15.  
    yum -y install nginx && \
  16.  
    pip3 install -r requirements.txt -i https://pypi.douban.com/simple
  17.  
     
  18.  
    COPY nginx.conf /etc/nginx/nginx.conf
  19.  
    COPY uwsgi.ini /flask_app/welink/uwsgi
  20.  
     
  21.  
    ENV LANG="en_US.UTF-8"
  22.  
     
  23.  
    EXPOSE 81
  24.  
     
  25.  
    CMD sh -c 'uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"'

nginx.conf文件内容:

  1.  
     
  2.  
    #user nobody;
  3.  
    worker_processes 1;
  4.  
     
  5.  
    #error_log logs/error.log;
  6.  
    #error_log logs/error.log notice;
  7.  
    #error_log logs/error.log info;
  8.  
     
  9.  
    #pid logs/nginx.pid;
  10.  
     
  11.  
     
  12.  
    events {
  13.  
    worker_connections 1024;
  14.  
    }
  15.  
     
  16.  
     
  17.  
    http {
  18.  
    include mime.types;
  19.  
    default_type application/octet-stream;
  20.  
     
  21.  
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  22.  
    # '$status $body_bytes_sent "$http_referer" '
  23.  
    # '"$http_user_agent" "$http_x_forwarded_for"';
  24.  
     
  25.  
    #access_log logs/access.log main;
  26.  
     
  27.  
    sendfile on;
  28.  
    #tcp_nopush on;
  29.  
     
  30.  
    #keepalive_timeout 0;
  31.  
    keepalive_timeout 65;
  32.  
     
  33.  
    #gzip on;
  34.  
     
  35.  
    server {
  36.  
    listen 81;
  37.  
    server_name localhost;
  38.  
     
  39.  
    #charset koi8-r;
  40.  
     
  41.  
    #access_log logs/host.access.log main;
  42.  
     
  43.  
    location / {
  44.  
    root html;
  45.  
    index index.html index.htm;
  46.  
    }
  47.  
     
  48.  
    location /hello {
  49.  
    proxy_pass http://127.0.0.1:5007/welink;
  50.  
    proxy_redirect off;
  51.  
    proxy_set_header Host $host;
  52.  
    proxy_set_header X-Real-IP $remote_addr;
  53.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  54.  
    }
  55.  
     
  56.  
    location /post {
  57.  
    proxy_pass http://127.0.0.1:5007/welink/send_msg;
  58.  
    proxy_redirect off;
  59.  
    proxy_set_header Host $host;
  60.  
    proxy_set_header X-Real-IP $remote_addr;
  61.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  62.  
    }
  63.  
     
  64.  
    #error_page 404 /404.html;
  65.  
     
  66.  
    # redirect server error pages to the static page /50x.html
  67.  
    #
  68.  
    error_page 500 502 503 504 /50x.html;
  69.  
    location = /50x.html {
  70.  
    root html;
  71.  
    }
  72.  
     
  73.  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  74.  
    #
  75.  
    #location ~ \.php$ {
  76.  
    # proxy_pass http://127.0.0.1;
  77.  
    #}
  78.  
     
  79.  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  80.  
    #
  81.  
    #location ~ \.php$ {
  82.  
    # root html;
  83.  
    # fastcgi_pass 127.0.0.1:9000;
  84.  
    # fastcgi_index index.php;
  85.  
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  86.  
    # include fastcgi_params;
  87.  
    #}
  88.  
     
  89.  
    # deny access to .htaccess files, if Apache's document root
  90.  
    # concurs with nginx's one
  91.  
    #
  92.  
    #location ~ /\.ht {
  93.  
    # deny all;
  94.  
    #}
  95.  
    }
  96.  
     
  97.  
     
  98.  
    # another virtual host using mix of IP-, name-, and port-based configuration
  99.  
    #
  100.  
    #server {
  101.  
    # listen 8000;
  102.  
    # listen somename:8080;
  103.  
    # server_name somename alias another.alias;
  104.  
     
  105.  
    # location / {
  106.  
    # root html;
  107.  
    # index index.html index.htm;
  108.  
    # }
  109.  
    #}
  110.  
     
  111.  
     
  112.  
    # HTTPS server
  113.  
    #
  114.  
    #server {
  115.  
    # listen 443 ssl;
  116.  
    # server_name localhost;
  117.  
     
  118.  
    # ssl_certificate cert.pem;
  119.  
    # ssl_certificate_key cert.key;
  120.  
     
  121.  
    # ssl_session_cache shared:SSL:1m;
  122.  
    # ssl_session_timeout 5m;
  123.  
     
  124.  
    # ssl_ciphers HIGH:!aNULL:!MD5;
  125.  
    # ssl_prefer_server_ciphers on;
  126.  
     
  127.  
    # location / {
  128.  
    # root html;
  129.  
    # index index.html index.htm;
  130.  
    # }
  131.  
    #}
  132.  
     
  133.  
    }

二、构建Docker镜像

  1.  
    [root@localhost flask]# docker build . -t welink:v5 --network host
  2.  
    Sending build context to Docker daemon 17.27MB
  3.  
    Step 1/9 : FROM centos:7
  4.  
    ---> eeb6ee3f44bd
  5.  
    Step 2/9 : RUN mkdir -p /flask_app/welink/uwsgi
  6.  
    ---> Using cache
  7.  
    ---> 2287baf70f92
  8.  
    Step 3/9 : COPY ./welink/ /flask_app/welink
  9.  
    ---> Using cache
  10.  
    ---> f2e47565b58d
  11.  
    Step 4/9 : WORKDIR /flask_app/welink
  12.  
    ---> Using cache
  13.  
    ---> e7ef6cbf9fe0
  14.  
    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
  15.  
    ---> Using cache
  16.  
    ---> 8a2ddae00489
  17.  
    Step 6/9 : COPY nginx.conf /etc/nginx/nginx.conf
  18.  
    ---> af8a26adeb18
  19.  
    Step 7/9 : COPY uwsgi.ini /flask_app/welink/uwsgi
  20.  
    ---> b75213ee312b
  21.  
    Step 8/9 : EXPOSE 81
  22.  
    ---> Running in b42cd57d939f
  23.  
    Removing intermediate container b42cd57d939f
  24.  
    ---> 744484ebcc2f
  25.  
    Step 9/9 : CMD sh -c 'uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"'
  26.  
    ---> Running in 2fc336a899f2
  27.  
    Removing intermediate container 2fc336a899f2
  28.  
    ---> 60ac3562ed19
  29.  
    Successfully built 60ac3562ed19
  30.  
    Successfully tagged welink:v5

第一次构建的时候会因为网络、yum源配置等因素比较慢,后续沟通docker会基于cache而变得非常快,这里已经是第5个版本了,所以构建的非常快。

查看构建成功的镜像:

  1.  
    [root@localhost flask]# docker images
  2.  
    REPOSITORY TAG IMAGE ID CREATED SIZE
  3.  
    welink v5 60ac3562ed19 1 second ago 675MB
  4.  
    welink v4 d019f46ee4be About an hour ago 675MB
  5.  
    welink v3 0fd30675b7ac 21 hours ago 675MB

这里就可以看出,通过Dockerfile构建的镜像比docker commit直接打包的会精简,不会有太多冗余数据,后续可以很方便的通过修改Dockerfile来构建新的镜像。

三、生成Docker容器

  1.  
    [root@localhost flask]# docker run -id --name welink01 -p 81:81 welink:v5
  2.  
    614cea779ed61c80a51ed9833186131c928aefa6e9a8f99c0a83ec05250f7e52
  3.  
    [root@localhost flask]#
  4.  
    [root@localhost flask]# docker ps
  5.  
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6.  
    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

四、访问测试

  1.  
    [root@localhost flask]# curl 127.0.0.1:81/hello
  2.  
    welcome to flask

注意:这里可以直接从物理机访问容器里的web服务,说明容器里的web项目已成功运行,并通过端口映射出来了。

五、维护命令

  1.  
    # 进docker
  2.  
    [root@localhost flask]# docker exec -it welink01 /bin/bash
  3.  
    [root@614cea779ed6 welink]#
  4.  
    [root@614cea779ed6 welink]# ps -ef|grep nginx
  5.  
    root 1 0 0 14:06 ? 00:00:00 sh -c uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"
  6.  
    root 11 1 0 14:06 ? 00:00:00 nginx: master process nginx -g daemon off;
  7.  
    nginx 12 11 0 14:06 ? 00:00:00 nginx: worker process
  8.  
    root 33 18 0 14:42 pts/0 00:00:00 grep --color=auto nginx
  9.  
    [root@614cea779ed6 welink]#
  10.  
    [root@614cea779ed6 welink]# ps -ef|grep uwsgi
  11.  
    root 1 0 0 14:06 ? 00:00:00 sh -c uwsgi --ini ./uwsgi/uwsgi.ini && nginx -g "daemon off;"
  12.  
    root 10 1 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
  13.  
    root 16 10 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
  14.  
    root 17 10 0 14:06 ? 00:00:00 uwsgi --ini ./uwsgi/uwsgi.ini
  15.  
    root 35 18 0 14:42 pts/0 00:00:00 grep --color=auto uwsgi
  16.  
     
  17.  
    # 出docker
  18.  
    [root@614cea779ed6 welink]# exit
  19.  
    exit
  20.  
     
  21.  
    # 在docker执行命令
  22.  
    [root@localhost flask]# docker exec -it welink01 ps -ef|grep nginx
  23.  
    root 11 1 0 14:06 ? 00:00:00 nginx: master process nginx
  24.  
    nginx 12 11 0 14:06 ? 00:00:00 nginx: worker process
  25.  
     
  26.  
    # 停docker
  27.  
    [root@localhost flask]# docker stop welink01
  28.  
    welink01
  29.  
     
  30.  
    # 删docker
  31.  
    [root@localhost flask]# docker rm welink01
  32.  
    welink01
  33.  
     
  34.  
    # 删镜像
  35.  
    [root@localhost flask]# docker rmi 3ecb659a87dc
  36.  
    Untagged: welink:v4
  37.  
    Deleted: sha256:3ecb659a87dcd3e940ded320f5fb490392f9e9ead3f01cd68e06113542e06339

六、导出镜像

  1.  
    [root@localhost flask]# docker save 60ac3562ed19 > welink_v5.tar
  2.  
    [root@localhost flask]# ls -hl
  3.  
    total 661M
  4.  
    -rw-r--r-- 1 root root 731 Oct 15 21:22 Dockerfile
  5.  
    -rw-r--r-- 1 root root 3.2K Oct 15 21:30 nginx.conf
  6.  
    -rw-r--r-- 1 root root 233 Oct 15 00:32 uwsgi.ini
  7.  
    drwxr-xr-x 5 root root 138 Oct 14 18:46 welink
  8.  
    -rw-r--r-- 1 root root 661M Oct 16 13:00 welink_v5.tar

这时就可以把镜像传到其它服务器进行导入了。

七、导入镜像

  1.  
    [root@appt tmp]# docker load < welink_v5.tar
  2.  
    07840385d029: Loading layer [==================================================>] 3.072kB/3.072kB
  3.  
    ed93135292b9: Loading layer [==================================================>] 17.26MB/17.26MB
  4.  
    69bc51ea49cd: Loading layer [==================================================>] 463.5MB/463.5MB
  5.  
    b7f0d7d30a92: Loading layer [==================================================>] 6.144kB/6.144kB
  6.  
    e8601482ae40: Loading layer [==================================================>] 3.584kB/3.584kB
  7.  
    Loaded image ID: sha256:60ac3562ed198dc014a5dbe00756c070d7584d57a7834f7a75776f24146d75c5
  8.  
    [root@appt tmp]#
  9.  
    [root@appt tmp]# docker images
  10.  
    REPOSITORY TAG IMAGE ID CREATED SIZE
  11.  
    <none> <none> 60ac3562ed19 15 hours ago 675MB
  12.  
    nginx latest 51086ed63d8c 10 days ago 142MB
  13.  
    weopsrdp latest 2ff3d656855c 5 months ago 148MB
  14.  
    dushixiang/guacd latest b578fc3fd8f3 9 months ago 281MB
  15.  
    python36e 1.0 5171821bb418 15 months ago 760MB
  16.  
    python27e 1.0 974165f81357 15 months ago 721MB
  17.  
    flaskdemo/demo latest 31e995c212d6 4 years ago 210MB
  18.  
    [root@appt tmp]#
  19.  
    [root@appt tmp]# docker tag 60ac3562ed19 welink:v5
  20.  
    [root@appt tmp]#
  21.  
    [root@appt tmp]# docker images
  22.  
    REPOSITORY TAG IMAGE ID CREATED SIZE
  23.  
    welink v5 60ac3562ed19 15 hours ago 675MB
  24.  
    nginx latest 51086ed63d8c 10 days ago 142MB
  25.  
    weopsrdp latest 2ff3d656855c 5 months ago 148MB
  26.  
    dushixiang/guacd latest b578fc3fd8f3 9 months ago 281MB
  27.  
    python36e 1.0 5171821bb418 15 months ago 760MB
  28.  
    python27e 1.0 974165f81357 15 months ago 721MB
  29.  
    flaskdemo/demo latest 31e995c212d6 4 years ago 210MB

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfiebg
系列文章
更多 icon
同类精品
更多 icon
继续加载