Dockerfile语法:
MAINTAINER:镜像创建者信息 EXPOSE:开放的端口 ENV:设置变量 WORKDIR:定义容器默认工作目录,相当于cd到某个目录 CMD:容器启动时执行的命令,仅可以有一条CMD
1先在容器内手动安装阿帕奇:
#1进入容器 [root@web01 ~]#docker run -it mycentos8:v20211229 #2安装阿帕奇 [root@e81ffc0059aa /]# yum -y install httpd #3查看启动文件看看阿帕奇是如何启动的 [root@e81ffc0059aa ~]# cat /lib/systemd/system/httpd.service [Unit] Description=The Apache HTTP Server Wants=httpd-init.service After=network.target remote-fs.target nss-lookup.target httpd-init.service Documentation=man:httpd.service(8) [Service] Type=notify #环境变量 Environment=LANG=C #执行启动命令 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful # Send SIGWINCH for graceful stop KillSignal=SIGWINCH KillMode=mixed PrivateTmp=true [Install] WantedBy=multi-user.target [root@e81ffc0059aa ~]# 4启动阿帕奇 [root@e81ffc0059aa ~]# LANG=C [root@e81ffc0059aa ~]# /usr/sbin/httpd $OPTIONS -k graceful AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message httpd not running, trying to start [root@e81ffc0059aa ~]# /usr/sbin/httpd $OPTIONS -DFOREGROUND AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message httpd (pid 73) already running #查看执行历史 [root@e81ffc0059aa ~]# history 19 yum -y install httpd 20 systemctl start httpd 21 pstree -p 27 cat /lib/systemd/system/httpd.service 28 LANG=C 30 /usr/sbin/httpd $OPTIONS -DFOREGROUND [root@e81ffc0059aa ~]#
2编写dockerfile:
[root@web01 ~]#mkdir bb [root@web01 ~]#cd bb [root@web01 ~/bb]#touch Dockerfile [root@web01 ~/bb]#vim Dockerfile [root@web01 ~/bb]#cat Dockerfile FROM mycentos8:v20211229 RUN yum -y install httpd ENV LANG=C #指定开放的端口 EXPOSE 80 443 #设置默认的工作目录 WORKDIR /var/www/html #拷贝文件到指定目录 ADD index.html /var/www/html/index.html #cmd特定语法格式例如ls -la: CMD ["ls","-l","-a"] CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
3创建html文件
[root@web01 ~/bb]#vim index.html [root@web01 ~/bb]#pwd /root/bb
4执行dockerfile创建阿帕奇服务镜像
[root@web01 ~/bb]#docker build -t mycentos8:httpd .
5基于镜像构建容器:
[root@web01 ~/bb]#docker run --name httpd -d -p 80:80 mycentos8:httpd
6验证:
#查看容器 [root@web01 ~/bb]#docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8361cb098b0 mycentos8:httpd "/usr/sbin/httpd -DF…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp httpd #查看容器详细信息 [root@web01 ~/bb]#docker inspect f8361cb098b0 "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", #模拟浏览器访问 [root@web01 ~/bb]#curl 172.17.0.2 hello world!