首页 Docker教程dockerfile实战-制作基于Base镜像的下层 nginx 镜像

dockerfile实战-制作基础镜像的Base镜像

dockerfile实战-直接制作nginx镜像

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

实战案例:Dockerfile 制作基于Base镜像的下层 nginx 镜像
在Dockerfile目录下准备编译安装的相关文件
[root@ubuntu1804 ~]#mkdir /data/dockerfile/web/nginx/1.16
[root@ubuntu1804 ~]#cd /data/dockerfile/web/nginx/1.16
[root@ubuntu1804 1.16]#vim nginx.conf
worker_processes  1;
user nginx;
daemon off;   #增加此行,前台运行nginx
[root@ubuntu1804 1.16]#wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@ubuntu1804 1.16]#mkdir app/
[root@ubuntu1804 1.16]#echo  "Test Page in app" > app/index.html
[root@ubuntu1804 1.16]#tar zcf app.tar.gz app
[root@ubuntu1804 1.16]#ls
app  app.tar.gz nginx-1.16.1.tar.gz  
在一台模版机进行编译安装同一版本的nginx,生成模版配置文件
[root@centos7 ~]#yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget bzip2 lsof tmux man-pages zip unzip nfs-utils gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl  openssl-devel systemd-devel zlib-devel
[root@centos7 ~]#wget -P /usr/local/src http://nginx.org/download/nginx-1.16.1.tar.gz
[root@centos7 ~]#cd /usr/local/src/
[root@centos7 src]#tar xvf nginx-1.16.1.tar.gz 
[root@centos7 src]#cd nginx-1.16.1/
[root@centos7 nginx-1.16.1]#./configure  --prefix=/apps/nginx && make && make install 

#复制到制到nginx镜像的服务器相应目录下
[root@centos7 ~]#scp /apps/nginx/conf/nginx.conf 10.0.0.1:/data/dockerfile/web/nginx/1.16 
编写Dockerfile文件
[root@ubuntu1804 ~]#cd /data/dockerfile/web/nginx
[root@ubuntu1804 nginx]#vim  Dockerfile 
[root@ubuntu1804 nginx]#cat Dockerfile 
FROM centos7-base:v1

LABEL maintainer="wangxiaochun <root@wangxiaochun.com>"

ADD nginx-1.16.1.tar.gz /usr/local/src

RUN cd /usr/local/src/nginx-1.16.1 && \
    && ./configure --prefix=/apps/nginx \
    && make && make install \
    && rm -f /usr/local/src/nginx* \
    && useradd -r nginx

COPY nginx.conf /apps/nginx/conf/

ADD  app.tar.gz /apps/nginx/html/

EXPOSE 80 443

CMD ["/apps/nginx/sbin/nginx"]
[root@ubuntu1804 nginx]#
生成nginx镜像
[root@ubuntu1804 ~]#cd /data/dockerfile/web/nginx/1.16
[root@ubuntu1804 1.16]#ls
app  app.tar.gz  build.sh  Dockerfile  nginx-1.16.1.tar.gz  nginx.conf
[root@ubuntu1804 1.16]#vim build.sh
[root@ubuntu1804 1.16]#cat build.sh
#!/bin/bash
#
docker build -t nginx-centos7:1.6.1 . 
[root@ubuntu1804 1.16]#chmod +x build.sh
[root@ubuntu1804 1.16]#./build.sh 
[root@ubuntu1804 1.16]##docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-centos7       1.6.1               73e4b4b95bca        10 minutes ago      412MB
centos7-base        v1                  1ba1317e06dc        About an hour ago   402MB
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB
生成的容器测试镜像
[root@ubuntu1804 ~]#docker run  -d -p 80:80  nginx-centos7:1.6.1
e8e733c6dc96bfb212a15dec04cfcfcac72daf400f5d2423c707aeb778a1859d
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                         NAMES
e8e733c6dc96        centos7-nginx:1.6.1   "/apps/nginx/sbin/ng…"   4 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp, 443/tcp   cool_germain
[root@ubuntu1804 ~]#docker exec -it e8e733c6dc96 bash
[root@e8e733c6dc96 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.2  0.2  20572  2468 ?        Ss   03:36   0:00 nginx: master process /apps/nginx/sbin/nginx
nginx        12  0.0  0.2  21024  2344 ?        S    03:36   0:00 nginx: worker process
root         13  4.0  0.3  12364  3536 pts/0    Ss   03:37   0:00 bash
root         32  0.0  0.3  51764  3460 pts/0    R+   03:37   0:00 ps aux
[root@e8e733c6dc96 /]# exit
exit
[root@ubuntu1804 ~]#curl 127.0.0.1/app/
Test Page in app

本文链接:http://www.yunweipai.com/34835.html

dockerfile实战-制作基础镜像的Base镜像

dockerfile实战-直接制作nginx镜像

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部