极客时间运维进阶训练营第二周作业----构建一个nginx镜像
首先准备nginx源码包,配置好的centos的yum源
root@docker1:/home/z9999# ls CentOS-Base.repo dockerfile nginx-1.23.2.tar.gz
写dockerfile,先来做一个用于以后构建nginx镜像的基础镜像
root@docker1:/home/z9999# cat dockerfile
from centos:latest
run rm -f /etc/yum.repos.d/*
add CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
run set -x \
&& yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel make \
&& mkdir /var/temp/nginx -p && cd /usr/local \
执行构建镜像的命令
root@docker1:/home/z9999# docker build -t harbor.liuzheng.net/server/centos_for_nginx:v1 .
中间省略一万字构建过程......
root@docker1:/home/z9999# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.liuzheng.net/server/centos_for_nginx v1 9596c9b6ef73 14 seconds ago 471MB
centos latest 5d0da3dc9764 13 months ago 231MB
再写一个基于刚才构建的基础镜像构建nginx的dockerfile
from harbor.liuzheng.net/server/centos_for_nginx:v1 add nginx-1.23.2.tar.gz /usr/local/ run set -x \ && mkdir /var/temp/nginx -p && cd /usr/local/nginx-1.23.2 \ && ./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-file-aio \ --with-http_realip_module \ && make && make install expose 80 443 entrypoint ["/usr/local/nginx/sbin/nginx"]
构建nginx镜像
root@docker1:/home/z9999# docker build -t harbor.liuzheng.net/server/nginx:v1 .
root@docker1:/home/z9999# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.liuzheng.net/server/nginx v1 2fec6332891c About a minute ago 503MB
harbor.liuzheng.net/server/centos_for_nginx v1 9596c9b6ef73 22 minutes ago 471MB
centos latest 5d0da3dc9764 13 months ago 231MB
测试一下
root@docker1:/home/z9999# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.liuzheng.net/server/nginx v1 934d73246092 10 seconds ago 503MB
harbor.liuzheng.net/server/centos_for_nginx v1 9596c9b6ef73 50 minutes ago 471MB
wordpress latest 5f9d748595e1 6 days ago 609MB
phpmyadmin latest dfa5f07e88dc 2 weeks ago 511MB
mariadb latest 14f1097913ec 2 weeks ago 384MB
httpd latest d16a51d08814 2 weeks ago 145MB
ubuntu latest 216c552ea5ba 2 weeks ago 77.8MB
centos latest 5d0da3dc9764 13 months ago 231MB
fauria/vsftpd latest 007276d7208b 2 years ago 255MB
root@docker1:/home/z9999# docker run -it -d -p 8080:80 harbor.liuzheng.net/server/nginx:v1
4519e98614a7d4a436fccd176157acaa6f1962c25ebb0d354e8d54808aca9937
访问成功!
总结一下
后面就可以基于这个nginx镜像构建自定义配置文件和应用的容器了,只需要在dockerfile中写上add 配置文件和应用程序即可
docker build常用指令
FROM centos:7.9.2009 #在整个dockfile文件中除了注释之外的第一行,要是FROM ,FROM 指令当前镜像的用于指定父镜像(base image)
ADD [--chown=:] ... #用于添加宿主机本地的文件、目录、压缩等资源到镜像里面去,会自动解压tar.gz格式的压缩包,但不会自动解压zip包
COPY COPY [--chown=:] ...#用于添加宿主机本地的文件、目录、压缩等资源到镜像里面去,不会解压任何压缩包
LABEL “key” = “value” #设置镜像的属性标签
ENV MY_NAME="John Doe" #设置容器环境变量
USER [:] or USER [:] #指定运行操作的用户
RUN yum install vim unzip -y && cd /etc/nginx #执行shell命令,但是一定要以非交互式的方式执行
VOLUME ["/data/data1" , "/data/data2"] #定义volume WORKDIR /data/data1 #用于定义当前工作目录 EXPOSE [/...] #声明要把容器的某些端口映射到宿主机