Dockerfile创建镜像的方法

2023年 8月 9日 67.0k 0

一、镜像制作的方法

1、本地导入导出镜像

请参考:Docker 架构原理及简单使用

导出:docker save nginx >/tmp/nginx.tar.gz

导入:docker load >/etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]

4、使用 build 命令制作镜像

1)build 命令说明

$ docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build a new image from the source code at PATH
--force-rm=false     Always remove intermediate containers, even after unsuccessful builds # 移除过渡容器,即使构建失败
--no-cache=false     Do not use cache when building the image                              # 不实用 cache
-q, --quiet=false    Suppress the verbose output generated by the containers
--rm=true            Remove intermediate containers after a successful build               # 构建成功后移除过渡层容器
-t, --tag=""         Repository name (and optionally a tag) to be applied to the resulting image in case of success

2)制作镜像

docker build -t zxg/nginx1 . //注意后边有个 点

整个过程还挺长,这里就不详细输出了。

5、验证一下,并用 curl 测试

[root@web1 docker]# docker run -d --name my_nginx1 zxg/nginx1 #后台运行整个容器
751a8f9dda48c034d40b8855b3dd6c7aaf785eeaa9ae404c5eb3c0271e434f50
[root@web1 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
751a8f9dda48        zxg/nginx1          "nginx"             About a minute ago   Up About a minute   80/tcp              my_nginx1
[root@web1 docker]# docker exec -it my_nginx1 bash #进入容器
[root@751a8f9dda48 /]# cat /usr/share/nginx/html/index.html   #查看文件是否添加到容器
this is docker-centos7-nginx1
[root@751a8f9dda48 /]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

daemon off;
[root@751a8f9dda48 /]# exit
exit
[root@web1 docker]#
[root@751a8f9dda48 html]# curl 127.0.0.1 //验证nginx是否生效
this is docker-centos7-nginx1
[root@751a8f9dda48 html]#
[root@web1 ~]# docker inspect my_nginx1 |grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@web1 ~]# curl 172.17.0.2
this is docker-centos7-nginx1
[root@web1 ~]#

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论