1. 程式人生 > 實用技巧 >dockerfile-映象製作

dockerfile-映象製作

目錄

1.基於alpine製作nginx映象

1.1 拉取alpine映象

[root@vm1 ~]# docker pull alpine:latest
latest: Pulling from library/alpine
Digest: sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest

1.2 編寫dockerfile

[root@vm1 ~]# mkdir alpine-nginx
[root@vm1 ~]# cd alpine-nginx/
[root@vm1 alpine-nginx]# vim Dockerfile 
FROM alpine:latest
LABEL MAINTAINER="wisan [email protected]"
RUN echo -e "http://mirrors.aliyun.com/alpine/v3.9/main\nhttp://mirrors.aliyun.com/alpine/v3.9/community" > /etc/apk/repositories \
    && apk add nginx \
    && mkdir -p /html/nginx \
    && echo "hello world !" > /html/nginx/index.html \
    && mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
EXPOSE 80
COPY ./nginx.conf /etc/nginx/nginx.conf
CMD ["nginx","-g","daemon off;"]

1.3 nginx配置檔案

[root@vm1 alpine-nginx]# vim nginx.conf 
user                            nginx;
worker_processes                auto;
error_log                       /var/log/nginx/error.log;
pid                             /run/nginx.pid;

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    on;
    access_log                  /var/log/nginx/access.log;
    keepalive_timeout           3000;
    server {
        listen                  80;
        server_name             localhost;
        client_max_body_size    32m;
        location / {
            root                    /html/nginx;
            index                   index.html;
        }
    }
}
[root@vm1 alpine-nginx]# ls
Dockerfile  nginx.conf

1.4 build生成映象

[root@vm1 alpine-nginx]# docker build -t xpengzong/nginx:v1 .
Sending build context to Docker daemon  3.584kB
Step 1/6 : FROM alpine:latest
 ---> 389fef711851
Step 2/6 : LABEL MAINTAINER="wisan [email protected]"
 ---> Running in ec6e24471846
Removing intermediate container ec6e24471846
 ---> 2ec5c5f00b8b
Step 3/6 : RUN echo -e "http://mirrors.aliyun.com/alpine/v3.9/main\nhttp://mirrors.aliyun.com/alpine/v3.9/community" > /etc/apk/repositories     && apk add nginx     && mkdir -p /html/nginx     && echo "hello world !" > /html/nginx/index.html     && mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
 ---> Running in b54d45f257c1
fetch http://mirrors.aliyun.com/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
(1/2) Installing pcre (8.42-r2)
(2/2) Installing nginx (1.14.2-r5)
Executing nginx-1.14.2-r5.pre-install
Executing busybox-1.31.1-r19.trigger
OK: 7 MiB in 16 packages
Removing intermediate container b54d45f257c1
 ---> 664451d60ff1
Step 4/6 : EXPOSE 80
 ---> Running in e81893e9d503
Removing intermediate container e81893e9d503
 ---> 571e21d92794
Step 5/6 : COPY ./nginx.conf /etc/nginx/nginx.conf
 ---> 4b14be0a860d
Step 6/6 : CMD ["nginx","-g","daemon off;"]
 ---> Running in 417d2859958f
Removing intermediate container 417d2859958f
 ---> 5b9d6971af83
Successfully built 5b9d6971af83
Successfully tagged xpengzong/nginx:v1

1.5 上傳製作的映象

[root@vm1 alpine-nginx]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
xpengzong/nginx     v1        5b9d6971af83   15 seconds ago   8.32MB
alpine              latest    389fef711851   2 weeks ago      5.58MB
nginx               latest    ae2feff98a0c   3 weeks ago      133MB
httpd               latest    dd85cdbb9987   3 weeks ago      138MB
ubuntu              latest    f643c72bc252   5 weeks ago      72.9MB

[root@vm1 alpine-nginx]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

[root@vm1 alpine-nginx]# docker push xpengzong/nginx:v1
The push refers to repository [docker.io/xpengzong/nginx]
885516d3a947: Pushed 
780e8c2673c1: Pushed 
777b2c648970: Mounted from library/alpine 
v1: digest: sha256:a2b5c1a84d180628ab4571152159ccd519375f66f8d75a4d2a5e545195168b54 size: 946

1.6 驗證

[root@vm1 alpine-nginx]# docker rmi xpengzong/nginx:v1
Untagged: xpengzong/nginx:v1
Untagged: xpengzong/nginx@sha256:a2b5c1a84d180628ab4571152159ccd519375f66f8d75a4d2a5e545195168b54
Deleted: sha256:5b9d6971af832a178ebbadfbcbacd7027b0b2941b68149e2c5ed55caa53d61f5
Deleted: sha256:4b14be0a860d692c436ef685b7c5f83597bf8c02fba767ba034659bca3875822
Deleted: sha256:a54d6cdc3abb652926e0d83da90c44f87d276cedfb539b0908c9bbaf5d2ef5de
Deleted: sha256:571e21d927949da0eb3b39b5baa701d2e04014e680173ce0400d42b2fa9c1bf8
Deleted: sha256:664451d60ff1bc88a1de642be85d13206e3a491ea06acf2537f20211680e5638
Deleted: sha256:7cf9ab071e7a559234de1795863eec0a2f2ce42bc049f9e78b3ce0138bca770d
Deleted: sha256:2ec5c5f00b8bd7e02fe78abd624506e2b617ffe89e9cef4cefb8555425ae5a54
[root@vm1 alpine-nginx]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
alpine              latest    389fef711851   2 weeks ago    5.58MB
nginx               latest    ae2feff98a0c   3 weeks ago    133MB
httpd               latest    dd85cdbb9987   3 weeks ago    138MB
ubuntu              latest    f643c72bc252   5 weeks ago    72.9MB
[root@vm1 alpine-nginx]# docker pull xpengzong/nginx:v1
v1: Pulling from xpengzong/nginx
801bfaa63ef2: Already exists 
93b80b1f73f6: Pull complete 
14d9c606229f: Pull complete 
Digest: sha256:a2b5c1a84d180628ab4571152159ccd519375f66f8d75a4d2a5e545195168b54
Status: Downloaded newer image for xpengzong/nginx:v1
docker.io/xpengzong/nginx:v1
[root@vm1 alpine-nginx]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
xpengzong/nginx     v1        5b9d6971af83   12 minutes ago   8.32MB
alpine              latest    389fef711851   2 weeks ago      5.58MB
nginx               latest    ae2feff98a0c   3 weeks ago      133MB
httpd               latest    dd85cdbb9987   3 weeks ago      138MB
ubuntu              latest    f643c72bc252   5 weeks ago      72.9MB

##前臺執行
[root@vm1 alpine-nginx]# docker run -it --name nginx1 xpengzong/nginx:v1

##換個終端
[root@vm1 alpine-nginx]# docker ps 
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS     NAMES
1d3f050a1e10   xpengzong/nginx:v1   "nginx -g 'daemon of…"   25 seconds ago   Up 22 seconds   80/tcp    nginx1
[root@vm1 alpine-nginx]# docker inspect nginx1
................................................
"Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "95c863fbcb17c9950ee3856687dad2cfb182bab955ee86022ad1a8b9f83065bc",
                    "EndpointID": "89887c8cb2da22f840855c93b2800dd68e259bcd7e63827d5cf949b941b1c1c1",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
...................................................

##訪問nignx服務
[root@vm1 alpine-nginx]# curl 172.17.0.2
hello world !

##埠對映
[root@vm1 alpine-nginx]# docker run -it --name nginx2 -p 8080:80  xpengzong/nginx:v1
[root@vm1 alpine-nginx]# ss -antl 
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port        
LISTEN        0             128                         0.0.0.0:22                        0.0.0.0:*           
LISTEN        0             128                               *:8080                            *:*           
LISTEN        0             128                            [::]:22                           [::]:*         
[root@vm1 alpine-nginx]# curl 127.0.0.1:8080
hello world !