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 619585019@qq.com"
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 619585019@qq.com"
 ---> 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 !

2. 基于alpine制作apache镜像

2.1 创建项目目录

[root@vm1 ~]#  mkdir -p /root/alpine-httpd/soft
[root@vm1 ~]# cd alpine-httpd/
[root@vm1 alpine-httpd]# ls
Dockerfile  soft
[root@vm1 alpine-httpd]# ls soft/
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.gz

2.2 编辑Dockerfile

[root@vm1 alpine-httpd]# vim Dockerfile 
FROM alpine:latest
LABEL MAINTAINER="wisan 619585019@qq.com"

ENV USER apache
ENV PACK /opt
## apache安装目录
ENV HTTPDIR /usr/local/apache
ENV PATH=$HTTPDIR/bin:$PATH

## 软件包版本:apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.gz
COPY soft/* $PACK/
WORKDIR /

## 安装依赖包
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && addgroup -S $USER && adduser -DHS -s /sbin/nologin $USER -G $USER \
    && apk add --no-cache -U gcc \
                             g++ \
                             make \
                             libffi-dev \
                             openssl-dev \
                             libtool \
                             expat-dev \
                             pcre-dev \
                             libxml2-dev \
    && tar xvf $PACK/apr-1.7.0.tar.gz -C $PACK/ \
    && tar xvf $PACK/apr-util-1.6.1.tar.gz -C $PACK/ \
    && tar xvf $PACK/httpd-2.4.46.tar.gz -C $PACK/ \

## 编译安装apr
    && cd $PACK/apr-1.7.0 \
    && sed -i 's/ $RM "$cfgfile/# $RM "$cfgfile/g' configure \
    && bash configure --prefix=/usr/local/apr \
    && make && make install  \
## 编译安装apr-util
    && cd $PACK/apr-util-1.6.1 \
    && bash configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr \
    && make && make install \

## 编译安装httpd
    && cd $PACK/httpd-2.4.46 \
    && bash configure --prefix=/usr/local/apache \
                   --enable-so \
                   --enable-ssl \
                   --enable-cgi \
                   --enable-rewrite \
                   --with-zlib \
                   --with-pcre \
                   --with-apr=/usr/local/apr \
                   --with-apr-util=/usr/local/apr-util \
                   --enable-modules=most \
                   --enable-mpms-shared=all \
                   --with-mpm=prefork \
    && make && make install \
    && ln -s $HTTPDIR/include/ /usr/include/httpd \
    && sed -i "s/#ServerName www.example.com:80/ServerName www.example.com:80/g" $HTTPDIR/conf/httpd.conf \
    && cd / && rm -rf $PACK/* \
    && rm -rf /var/cache/apk/* \
    && apk del make gcc g++
EXPOSE 80 443
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["httpd"]

2.3 制作镜像

[root@vm1 alpine-httpd]# docker build -t xpengzong/httpd:latest .
.......................................................
Successfully built 8f2e1efd24d8
Successfully tagged xpengzong/httpd:latest
[root@vm1 alpine-httpd]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
xpengzong/httpd     latest    8f2e1efd24d8   35 minutes ago   82.7MB
xpengzong/nginx     v1        5b9d6971af83   14 hours ago     8.32MB
xpengzong/busybox   v2        6d063fb1d14e   41 hours ago     1.24MB
busybox             latest    a77dce18d0ec   7 days ago       1.24MB
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

2.4 运行容器

[root@vm1 alpine-httpd]# docker run -itd --name web -p 8080:80  xpengzong/httpd:latest
ade0774cfd3efad570540a3b8250a5d0612556d724c11946316d6313e3d5934e
[root@vm1 alpine-httpd]# docker ps 
CONTAINER ID   IMAGE                    COMMAND                 CREATED         STATUS         PORTS                           NAMES
ade0774cfd3e   xpengzong/httpd:latest   "httpd -D FOREGROUND"   6 seconds ago   Up 4 seconds   443/tcp, 0.0.0.0:8080->80/tcp   web

[root@vm1 alpine-httpd]# 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 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@vm1 ~]# curl 192.168.225.127:8080
<html><body><h1>It works!</h1></body></html>
posted @ 2021-01-11 19:39  小芃总  阅读(454)  评论(0编辑  收藏  举报