Docker Dockerfile

例1:COPY 复制文件

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html/


[root@k8s-master01 Dockerfile]# vim index.html

Test


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.1 .

Sending build context to Docker daemon  3.072kB

Step 1/3 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/3 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Running in 15f0ac9d5a7a

Removing intermediate container 15f0ac9d5a7a

 ---> 9a49a2859a7e

Step 3/3 : COPY index.html /data/web/html/

 ---> b054857b475e

Successfully built b054857b475e

Successfully tagged weizshttpd:v1.0.1


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm -it weizshttpd:v1.0.1 cat /data/web/html

Test


例2:COPY 复制目录

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html

COPY yum.repos.d /etc/yum.repos.d/


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.2 .

Sending build context to Docker daemon  35.33kB

Step 1/4 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/4 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Using cache

 ---> 9a49a2859a7e

Step 3/4 : COPY index.html /data/web/html/

 ---> Using cache

 ---> b054857b475e

Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/

 ---> d7bc58f8a5b0

Successfully built d7bc58f8a5b0

Successfully tagged weizshttpd:v1.0.2


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.2 ls /etc/yum.repos.d/

CentOS-Base.repo

CentOS-Base.repo.backup

CentOS-CR.repo

CentOS-Debuginfo.repo

CentOS-Media.repo

CentOS-Sources.repo

CentOS-Vault.repo

CentOS-fasttrack.repo

CentOS-x86_64-kernel.repo

docker-ce.repo

elrepo.repo

kubernetes.repo


例3: ADD 下载URL压缩文件,不会自动解压

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html

COPY yum.repos.d /etc/yum.repos.d/

ADD http://nginx.org/download/nginx-1.19.3.tar.gz /usr/local/src/


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.3 .

Sending build context to Docker daemon  35.33kB

Step 1/5 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/5 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Using cache

 ---> 9a49a2859a7e

Step 3/5 : COPY index.html /data/web/html/

 ---> Using cache

 ---> b054857b475e

Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/

 ---> Using cache

 ---> d7bc58f8a5b0

Step 5/5 : ADD http://nginx.org/download/nginx-1.19.3.tar.gz /usr/local/src/

Downloading [==================================================>]  1.053MB/1.053MB

 ---> b9c9a6ecd0d9

Successfully built b9c9a6ecd0d9

Successfully tagged weizshttpd:v1.0.3


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.3 ls /usr/local/src

nginx-1.19.3.tar.gz


例4: ADD 复制压缩文件,自动解压

[root@k8s-master01 Dockerfile]# wget http://nginx.org/download/nginx-1.19.3.tar.gz


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.4 .

Sending build context to Docker daemon  1.089MB

Step 1/5 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/5 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Running in 703a688b9816

Removing intermediate container 703a688b9816

 ---> 0d722c905035

Step 3/5 : COPY index.html /data/web/html/

 ---> 89fdd11b11dc

Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/

 ---> 17d921b2e705

Step 5/5 : ADD nginx-1.19.3.tar.gz /usr/local/src

 ---> 30fddd566308

Successfully built 30fddd566308

Successfully tagged weizshttpd:v1.0.4


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.4 ls /usr/local/src

nginx-1.19.3


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.4 ls /usr/local/src/nginx-1.19.3

CHANGES

CHANGES.ru

LICENSE

README

auto

conf

configure

contrib

html

man

src


例5: WORKDIR 设置目录

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html/

COPY yum.repos.d /etc/yum.repos.d/

#ADD http://nginx.org/download/nginx-1.19.3.tar.gz /usr/local/src/

WORKDIR /usr/local/

ADD nginx-1.19.3.tar.gz ./src



例6: VOLUME 挂载目录

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html/

COPY yum.repos.d /etc/yum.repos.d/

#ADD http://nginx.org/download/nginx-1.19.3.tar.gz /usr/local/src/

WORKDIR /usr/local/

ADD nginx-1.19.3.tar.gz ./src


VOLUME /data/mysql


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.5 .

Sending build context to Docker daemon  1.089MB

Step 1/7 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/7 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Using cache

 ---> 0d722c905035

Step 3/7 : COPY index.html /data/web/html/

 ---> Using cache

 ---> 89fdd11b11dc

Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/

 ---> Using cache

 ---> 17d921b2e705

Step 5/7 : WORKDIR /usr/local/

 ---> Running in ac6376f9f922

Removing intermediate container ac6376f9f922

 ---> 1bccbcd7bd74

Step 6/7 : ADD nginx-1.19.3.tar.gz ./src

 ---> 5ab98d6362ba

Step 7/7 : VOLUME /data/mysql

 ---> Running in 148aff086a01

Removing intermediate container 148aff086a01

 ---> 886c037fe5d3

Successfully built 886c037fe5d3

Successfully tagged weizshttpd:v1.0.5


查看方式1:

[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.5 mount | grep mysql

/dev/mapper/centos-root on /data/mysql type xfs (rw,relatime,attr2,inode64,noquota)


查看方式2:

[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.5 sleep 60


[root@k8s-master01 ~]# docker inspect weizshttpd | grep -C 5 mysql

        "Mounts": [

            {

                "Type": "volume",

                "Name": "1d48548d32c6b4533c88f5c48dccc475e4445c54a2e6037ab342d13f11b183a7",

                "Source": "/var/lib/docker/volumes/1d48548d32c6b4533c88f5c48dccc475e4445c54a2e6037ab342d13f11b183a7/_data",

                "Destination": "/data/mysql",

                "Driver": "local",

                "Mode": "",

                "RW": true,

                "Propagation": ""

            }

--

                "sleep",

                "60"

            ],

            "Image": "weizshttpd:v1.0.5",

            "Volumes": {

                "/data/mysql": {}

            },

            "WorkingDir": "/usr/local",

            "Entrypoint": null,

            "OnBuild": null,

            "Labels": {}


例7: EXPOSE 声明暴露端口

[root@k8s-master01 Dockerfile]# vim Dockerfile

FROM busybox:latest

MAINTAINER "weizs <654421772@qq.com>"

COPY index.html /data/web/html/

COPY yum.repos.d /etc/yum.repos.d/

#ADD http://nginx.org/download/nginx-1.19.3.tar.gz /usr/local/src/

WORKDIR /usr/local/

ADD nginx-1.19.3.tar.gz ./src


VOLUME /data/mysql


EXPOSE 80/tcp


[root@k8s-master01 Dockerfile]# docker build -t weizshttpd:v1.0.6 .

Sending build context to Docker daemon  1.089MB

Step 1/8 : FROM busybox:latest

 ---> f0b02e9d092d

Step 2/8 : MAINTAINER "weizs <654421772@qq.com>"

 ---> Using cache

 ---> 0d722c905035

Step 3/8 : COPY index.html /data/web/html/

 ---> Using cache

 ---> 89fdd11b11dc

Step 4/8 : COPY yum.repos.d /etc/yum.repos.d/

 ---> Using cache

 ---> 17d921b2e705

Step 5/8 : WORKDIR /usr/local/

 ---> Using cache

 ---> 1bccbcd7bd74

Step 6/8 : ADD nginx-1.19.3.tar.gz ./src

 ---> Using cache

 ---> 5ab98d6362ba

Step 7/8 : VOLUME /data/mysql

 ---> Using cache

 ---> 886c037fe5d3

Step 8/8 : EXPOSE 80/tcp

 ---> Running in 059fb7c75af0

Removing intermediate container 059fb7c75af0

 ---> fbe8311f33f9

Successfully built fbe8311f33f9

Successfully tagged weizshttpd:v1.0.6



[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm weizshttpd:v1.0.6 /bin/httpd -f -h /data/web/html


另开窗口

[root@k8s-master01 ~]# docker inspect weizshttpd


[root@k8s-master01 ~]# docker inspect weizshttpd | grep "IPAddress"

            "SecondaryIPAddresses": null,

            "IPAddress": "172.17.0.2",

                    "IPAddress": "172.17.0.2",


[root@k8s-master01 ~]# curl 172.17.0.2

Test


杀死容器

[root@k8s-master01 ~]# docker kill weizshttpd 

weizshttpd


[root@k8s-master01 Dockerfile]# docker run --name weizshttpd --rm -P weizshttpd:v1.0.6 /bin/httpd -f -h /data/web/html


另开窗口

[root@k8s-master01 ~]# docker port weizshttpd 

80/tcp -> 0.0.0.0:32768


Web浏览器访问  http://192.168.56.10:32768/

Docker Dockerfile


[root@k8s-master01 ~]# docker kill weizshttpd 

weizshttpd



上一篇:spark学习


下一篇:k8s官方案例练习-使用 ConfigMap 来配置 Redis