練習7

練習7

使用docker build命令製作容器鏡像:

範例1

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
COPY index.html /data/web/html/  #複製Dockerfile所在目錄下的index.html文件到容器的/data/web/html目錄下,/data/web/html目錄不存在會自動創建
[root@localhost img1]# vim index.html 
<h1>busybox httpd server.</h1>
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Running in 4530e9ed0c8f
Removing intermediate container 4530e9ed0c8f
 ---> 510d8705fb8e
Step 3/3 : COPY index.html /data/web/html/
 ---> 3fd7ce0bf99e
Successfully built 3fd7ce0bf99e
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            3fd7ce0bf99e   25 seconds ago   1.23MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name t1 --rm tinyhttpd:v0.1 cat /data/web/html/index.html
<h1>busybox httpd server.</h1>

範例2

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
COPY yum.repos.d /etc/yum.repos.d/  #複製Dockerfile所在目錄下的yum.repos.d目錄下的所有文件(不包含目錄本身)到容器的/etc/yum.repos.d目錄下,/etc/yum.repos.d目錄不存在會自動創建
[root@localhost img1]# cp -r /etc/yum.repos.d/ ./
[root@localhost img1]# ll
total 4
-rw-r--r--. 1 root root 126 May 16 12:11 Dockerfile
drwxr-xr-x. 2 root root 242 May 16 12:11 yum.repos.d
[root@localhost img1]# docker build -t tinyos:v0.1 ./  
Sending build context to Docker daemon   25.6kB

Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Running in c138d9e1d36e
Removing intermediate container c138d9e1d36e
 ---> 4c2539d47175
Step 3/3 : COPY yum.repos.d /etc/yum.repos.d/
 ---> c44c014c5cb8
Successfully built c44c014c5cb8
Successfully tagged tinyos:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyos        v0.1            c44c014c5cb8   18 seconds ago   1.25MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name t1 --rm tinyos:v0.1 ls /etc/yum.repos.d/
CentOS-Base.repo
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

範例3

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# ll
total 1040
-rw-r--r--. 1 root root 1061062 Apr 13 11:34 nginx-1.19.10.tar.gz  #nginx官網下載的tar壓縮包
[root@localhost img1]# vim Dockerfile
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ADD nginx-1.19.10.tar.gz /usr/local/src/  #nginx-1.19.10.tar.gz會被解tar、解壓縮放到/usr/local/src目錄下,/usr/local/src目錄不存在會自動創建
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  1.064MB
Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> 124b66b113e6
Step 3/3 : ADD nginx-1.19.10.tar.gz /usr/local/src/
 ---> 49bb559f80bc
Successfully built 49bb559f80bc
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            49bb559f80bc   9 seconds ago    7.62MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name t1 --rm tinyhttpd:v0.1 ls -l /usr/local/src/
total 0
drwxr-xr-x    8 1001     1001           158 Apr 13 15:14 nginx-1.19.10

範例4

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ADD https://nginx.org/download/nginx-1.19.10.tar.gz /usr/local/src/  #nginx-1.19.10.tar.gz會被下載放到/usr/local/src目錄下,/usr/local/src目錄不存在會自動創建
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> 124b66b113e6
Step 3/3 : ADD https://nginx.org/download/nginx-1.19.10.tar.gz /usr/local/src/
Downloading [==================================================>]  1.061MB/1.061MB
 ---> c7f011a7cd3c
Successfully built c7f011a7cd3c
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            c7f011a7cd3c   23 seconds ago   2.29MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name t1 --rm tinyhttpd:v0.1 ls -l /usr/local/src/
total 1040
-rw-------    1 root     root       1061062 Apr 13 15:34 nginx-1.19.10.tar.gz

範例5

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
WORKDIR /data/web/html/
COPY index.html ./  #./位置就是/data/web/html/
WORKDIR /usr/local/src/
ADD https://nginx.org/download/nginx-1.19.10.tar.gz ./  #./位置就是/usr/local/src/
[root@localhost img1]# vim index.html
<h1>busybox httpd server.</h1>
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM busybox:latest
 ---> 388056c9a683
Step 2/6 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> 124b66b113e6
Step 3/6 : WORKDIR /data/web/html/
 ---> Running in 67e5acc0cfd0
Removing intermediate container 67e5acc0cfd0
 ---> ba11f6bcca05
Step 4/6 : COPY index.html ./
 ---> 8f3c532fd1c9
Step 5/6 : WORKDIR /usr/local/src/
 ---> Running in 730e6def320a
Removing intermediate container 730e6def320a
 ---> b8e4a8fe5d32
Step 6/6 : ADD https://nginx.org/download/nginx-1.19.10.tar.gz ./
Downloading [==================================================>]  1.061MB/1.061MB
 ---> 281d78bb04c0
Successfully built 281d78bb04c0
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            281d78bb04c0   9 seconds ago    2.29MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name t1 --rm tinyhttpd:v0.1 cat /data/web/html/index.html
<h1>busybox httpd server.</h1>
[root@localhost img1]# docker container run --name t1 --rm tinyhttpd:v0.1 ls -l /usr/local/src/
total 1040
-rw-------    1 root     root       1061062 Apr 13 15:34 nginx-1.19.10.tar.gz

範例6

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
VOLUME /data/web/html/
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./                                       
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> 124b66b113e6
Step 3/3 : VOLUME /data/web/html/
 ---> Running in 5a0d68417702
Removing intermediate container 5a0d68417702
 ---> 6c165542350d
Successfully built 6c165542350d
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            6c165542350d   3 minutes ago    1.23MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name w1 -it --rm tinyhttpd:v0.1
/ # ls -d /data/web/html/
/data/web/html/
[root@localhost img1]# docker container inspect w1
...
        "Mounts": [
            {
                "Type": "volume",
                "Name": "b38d32431f66dc844ef70bb35bebf8c48906ba17d2edd591b1dbcb73b69f70ba",
                "Source": "/var/lib/docker/volumes/b38d32431f66dc844ef70bb35bebf8c48906ba17d2edd591b1dbcb73b69f70ba/_data",
                "Destination": "/data/web/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
...
            "Volumes": {
                "/data/web/html/": {}
            },
...

範例7

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
EXPOSE 123  #當創建容器時加-P選項就會暴露這裡指定的port,如果這裡沒指定則創建容器時加-P選項會暴露容器所有的port
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox:latest
 ---> 388056c9a683
Step 2/3 : LABEL maintainer="author <author@gmail.com>"
 ---> Running in 0bea5a51b72f
Removing intermediate container 0bea5a51b72f
 ---> bc2c832704f9
Step 3/3 : EXPOSE 123
 ---> Running in f20335552e16
Removing intermediate container f20335552e16
 ---> 3b31c7a5d531
Successfully built 3b31c7a5d531
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1 httpd -f -h /tmp
[root@localhost img1]# docker container port w1  #僅暴露Dockerfile裡指定的123 port
123/tcp -> 0.0.0.0:49159
123/tcp -> :::49159

範例8

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ENV DOC_ROOT=/data/web/html/ \
    WEB_SERVER_PACKAGE=nginx-1.19.10
COPY index.html ${DOC_ROOT:-/tmp/}
ADD ${WEB_SERVER_PACKAGE}.tar.gz /usr/local/src/
[root@localhost img1]# ll
total 1044
-rw-r--r--. 1 root root     218 May 16 14:02 Dockerfile
-rw-r--r--. 1 root root       0 May 16 14:03 index.html
-rw-r--r--. 1 root root 1061062 Apr 13 11:34 nginx-1.19.10.tar.gz
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  1.064MB
Step 1/5 : FROM busybox:latest
 ---> 388056c9a683
Step 2/5 : LABEL maintainer="author <author@gmail.com>"
 ---> Running in 16e89174e9a3
Removing intermediate container 16e89174e9a3
 ---> db8e2d719140
Step 3/5 : ENV DOC_ROOT=/data/web/html/     WEB_SERVER_PACKAGE=nginx-1.19.10
 ---> Running in 621e0d91d597
Removing intermediate container 621e0d91d597
 ---> 15ae21e1a36a
Step 4/5 : COPY index.html ${DOC_ROOT:-/tmp/}
 ---> fcc1e806799f
Step 5/5 : ADD ${WEB_SERVER_PACKAGE}.tar.gz /usr/local/src/
 ---> cc69707972d8
Successfully built cc69707972d8
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1 ls /data/web/html/
index.html
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1 ls /usr/local/src/
nginx-1.19.10

範例9

[root@localhost ~]# docker container run --name t1 --rm -e ABC=EFG busybox:latest printenv  #創建容器時加-e選項可以設定容器環境的環境變量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=36131ba8c2dc
ABC=EFG
HOME=/root

範例10

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ENV WEB_DOC_ROOT="/data/web/html/"
RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  1.064MB
Step 1/5 : FROM busybox:latest
 ---> 388056c9a683
Step 2/5 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> db8e2d719140
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 577cffed8421
Step 4/5 : RUN mkdir -p $WEB_DOC_ROOT &&     echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
 ---> Running in ba23599d008a
Removing intermediate container ba23599d008a
 ---> 6551eaafb6b1
Step 5/5 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
 ---> Running in fa9432f5daf4
Removing intermediate container fa9432f5daf4
 ---> 89d33579da37
Successfully built 89d33579da37
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker image ls
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
tinyhttpd     v0.1            89d33579da37   47 seconds ago   1.23MB
busybox       latest          388056c9a683   5 weeks ago      1.23MB
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1
[root@localhost ~]# docker container exec -it w1 /bin/sh
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/httpd -f -h /data/web/html/  #PID號為1運行的是CMD的命令
    7 root      0:00 /bin/sh
   13 root      0:00 ps

範例11

[root@localhost ~]# docker container run --name t1 --rm nginx:stable-alpine ls /  #後面加上運行的命令可以覆蓋鏡像默認要運行的命令
bin
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

範例12

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ENV WEB_DOC_ROOT="/data/web/html/"
RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./
Sending build context to Docker daemon  1.064MB
Step 1/5 : FROM busybox:latest
 ---> 388056c9a683
Step 2/5 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> db8e2d719140
Step 3/5 : ENV WEB_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 577cffed8421
Step 4/5 : RUN mkdir -p $WEB_DOC_ROOT &&     echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
 ---> Running in 969b0f7cd089
Removing intermediate container 969b0f7cd089
 ---> 4fcd9c8f5a48
Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
 ---> Running in 3a72428a3042
Removing intermediate container 3a72428a3042
 ---> cd453ec059f8
Successfully built cd453ec059f8
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1 ls /  #不會執行ls /,因為ls /被當作鏡像中ENTRYPOINT的參數
[root@localhost img1]# docker container run --name w1 --rm --entrypoint ls tinyhttpd:v0.1 /  #加上--entrypoint選項後面接命令,最後再加命令的參數
bin
data
dev
etc
home
proc
root
sys
tmp
usr
var

範例13

[root@localhost ~]# mkdir img1
[root@localhost ~]# cd img1/
[root@localhost img1]# vim Dockerfile 
# Description: test image
FROM busybox:latest
LABEL maintainer="author <author@gmail.com>"
ENV WEB_DOC_ROOT="/data/web/html/"
RUN mkdir -p $WEB_DOC_ROOT && \
    echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
CMD ["/bin/sh","-c","/bin/httpd -f -h /data/web/html"]  #當默認參數傳給ENTRYPOINT
ENTRYPOINT 
[root@localhost img1]# docker build -t tinyhttpd:v0.1 ./                 
Sending build context to Docker daemon  1.064MB
Step 1/6 : FROM busybox:latest
 ---> 388056c9a683
Step 2/6 : LABEL maintainer="author <author@gmail.com>"
 ---> Using cache
 ---> db8e2d719140
Step 3/6 : ENV WEB_DOC_ROOT="/data/web/html/"
 ---> Using cache
 ---> 577cffed8421
Step 4/6 : RUN mkdir -p $WEB_DOC_ROOT &&     echo '<h1>busybox httpd server.</h1>' > ${WEB_DOC_ROOT}/index.html
 ---> Running in 766df7b18b72
Removing intermediate container 766df7b18b72
 ---> 07b8880a5bed
Step 5/6 : CMD ["/bin/sh","-c","/bin/httpd -f -h /data/web/html"]
 ---> Running in 36913c8432ba
Removing intermediate container 36913c8432ba
 ---> fcf39b512689
Step 6/6 : ENTRYPOINT
 ---> Running in 0a22b089a0c6
Removing intermediate container 0a22b089a0c6
 ---> 8090bc89845a
Successfully built 8090bc89845a
Successfully tagged tinyhttpd:v0.1
[root@localhost img1]# docker container run --name w1 --rm tinyhttpd:v0.1 ls /  #ls /當參數傳給ENTRYPOINT,覆蓋了CMD這個默認參數
bin
data
dev
etc
home
proc
root
sys
tmp
usr
var