0


Dockerfile介绍及创建流程

什么是Dockerfile

Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),用于构建镜像。每一条指令构建一层镜像,因此每一条指令的内容,就是描述该层镜像应当如何构建。

Dockerfile 用于指示 docker image build 命令自动构建Image的源代码是纯文本文件

为什么要使用Dockerfile

日后用户可以将自己应用打包成镜像,这样就可以让我们应用进行容器运行.还可以对官方镜像做扩展,以打包成我们生产应用的镜像。

docker build工作原理

docker build  -t ImageName:TagName dir
  • -t − 给镜像加一个Tag
  • ImageName − 给镜像起的名称
  • TagName − 给镜像的Tag名
  • Dir − Dockerfile所在目录

Docker 在运行时分为 Docker 引擎(也就是服务端守护进程)和客户端工具。Docker 的引擎提供了一组 REST API,被称为 Docker Remote API ,而如 docker 命令这样的客户端工具,则是通过这组 API 与 Docker 引擎交互,从而完成各种功能。因此,虽然表面上我们好像是在本机执行各种 docker 功能,但实际上,一切都是使用的远程调用形式在服务端(Docker 引擎)完成。也因为这种 C/S 设计,让我们操作远程服务器的 Docker 引擎变得轻而易举。

Dockerfile常用指令

示例

创建Dockerfile文件

FROM centos:7
​
#作者信息

MAINTAINER "shizh"

#工作目录

WORKDIR /usr/local/src/

#定义环境变量

ENV NG_VERSION nginx-1.21.0

#安装epel仓库

RUN yum -y install epel-release

#安装wget

RUN yum -y install wget

#下载nginx文件并解压

RUN wget http://nginx.org/download/$NG_VERSION.tar.gz && tar xzvf $NG_VERSION.tar.gz

#安装编译依赖包

RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel && yum install -y pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data

#清理仓库

RUN yum clean all

#创建nginx用户

RUN useradd -M -s /sbin/nologin nginx

#切换工作目录

WORKDIR /usr/local/src/$NG_VERSION

#编译安装nginx

RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install

#复制测试页面到容器中

ADD index.html /usr/local/nginx/html

#设置容器中要挂在到宿主机的目录

VOLUME /usr/local/nginx/html

#设置sbin环境变量

ENV PATH /usr/local/nginx/sbin:$PATH

#暴露80端口

EXPOSE 80/tcp

ENTRYPOINT ["nginx"]

CMD ["-g","daemon off;"]

#当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)

#创建一个index.html文件,用于显示页面

<html>

<head></head>

<body>

<h1>Hello World</h1>

</body>

</html>

#执行构建镜像语句

> docker build -t nginx:0.0.1 .​​​​​​​

#查看构建的镜像

> docker inspect nginx:0.0.1

#结果JSON

[
    {
        "Id": "sha256:97075f3916b57bc7057dc619623ccee5fa8fe0b8268caedc171710a9289e1ceb",
        "RepoTags": [
            "nginx:0.0.1"
        ],
        "RepoDigests": [],
        "Parent": "sha256:0af4cc893f3413c96550aeeb70a4dbacf80a131becc7038b100ff190476edd0a",
        "Comment": "",
        "Created": "2022-08-24T06:20:00.75041395Z",
        "Container": "50888b1331987bc12d367a039e10f70904fe49d98a9e67602e35f478606c34b4",
        "ContainerConfig": {
            "Hostname": "50888b133198",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NG_VERSION=nginx-1.21.0"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"-g\" \"daemon off;\"]"
            ],
            "Image": "sha256:0af4cc893f3413c96550aeeb70a4dbacf80a131becc7038b100ff190476edd0a",
            "Volumes": {
                "/usr/local/nginx/html": {}
            },
            "WorkingDir": "/usr/local/src/nginx-1.21.0",
            "Entrypoint": [
                "nginx"
            ],
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201113",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.17",
        "Author": "\"zhbr_zyn\"",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NG_VERSION=nginx-1.21.0"
            ],
            "Cmd": [
                "-g",
                "daemon off;"
            ],
            "Image": "sha256:0af4cc893f3413c96550aeeb70a4dbacf80a131becc7038b100ff190476edd0a",
            "Volumes": {
                "/usr/local/nginx/html": {}
            },
            "WorkingDir": "/usr/local/src/nginx-1.21.0",
            "Entrypoint": [
                "nginx"
            ],
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201113",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1127213093,
        "VirtualSize": 1127213093,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/docker_data/overlay2/efb8993f117e32e0994e907a99b89a724a2739b0fcb1e4138281d4bd05c0c7b7/diff:/docker_data/overlay2/78a1f799e7f74d2d8c922821912f83208ebe70bd450455fe217a2e97613c2e2a/diff:/docker_data/overlay2/5d5137e53eff53a800bb1e815f5cfcc1f68b8413e57cdb2a39015842dea0941c/diff:/docker_data/overlay2/d1df391b231a4595bc0ac2c854a36cc697e65f033084c210a702a51ee3ec627d/diff:/docker_data/overlay2/ef1f50c4a5e8fcecaad18db0540a2618fa3bbce6081ca2e488b7013799e5efea/diff:/docker_data/overlay2/e8ac2e0d9a4f1055db1382eb9d40a60cd112e2f9efeff44417767a71c83fd2dc/diff:/docker_data/overlay2/479006f3fcdef88500c53833edcd235c7c4b9bb19219258639d7ae79937b8daf/diff:/docker_data/overlay2/d23636ff691c3c062f5f2625337b06d4e095177d3aa9b58f03a5806cc6f7f036/diff",
                "MergedDir": "/docker_data/overlay2/37a0b97cbed1ac629e279689ed0a70ebd362cbda2a61295ada513a4aca977a16/merged",
                "UpperDir": "/docker_data/overlay2/37a0b97cbed1ac629e279689ed0a70ebd362cbda2a61295ada513a4aca977a16/diff",
                "WorkDir": "/docker_data/overlay2/37a0b97cbed1ac629e279689ed0a70ebd362cbda2a61295ada513a4aca977a16/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02",
                "sha256:85a6b441826628c36ccc1fb9ba0a2cc59a2f08d966655bdd9d86b5d2030094f7",
                "sha256:9d7711266a016d104d4e8affd6984f852baca1f64c3045e5c2d53fa47c33dc42",
                "sha256:4436d938df2a9c1e938b07b35645839ede499ef7d14a5cc16811b7b91f587acc",
                "sha256:da2357cf68b4532793baf3b4663053cf241555ecde8b2762746883aa513e443c",
                "sha256:ff505e9fa608e7169a2fa9b3e03e7a7c002c2894f013c4de305f018910dbf8ae",
                "sha256:2ed6f17047f797fb8ba17e6db8f92893e0887bda6253c2b48f3a2f74e4c77e1d",
                "sha256:77ba3416ffe3e1a5db1db88a8954ad54bda039ed4f69b3d17b62fe8102a3cd0b",
                "sha256:dbc575b44a6d99bc06984756d1cdf13357a63addf6b3886234fbc4e0839e3331"
            ]
        },
        "Metadata": {
            "LastTagTime": "2022-08-24T14:20:00.762344685+08:00"
        }
    }
]​​​​​​​

​

运行docker镜像

> docker run -it --name mynginx -p 8123:80 -d nginx:0.0.1

查看容器是否启动起来

> docker ps | grep mynginx

6352fc93060c   nginx:0.0.1             "nginx -g 'daemon of…"   12 minutes ago   Up 12 minutes   0.0.0.0:8123->80/tcp, :::8123->80/tcp                                                           mynginx

验证结果

浏览器查看

标签: 服务器 运维 docker

本文转载自: https://blog.csdn.net/m0_46830547/article/details/136112398
版权归原作者 countrysides 所有, 如有侵权,请联系我们删除。

“Dockerfile介绍及创建流程”的评论:

还没有评论