创建工作目录
[root@localhost ~]# mkdir tomcat
[root@localhost ~]# cd tomcat/编写配置文件
[root@localhost tomcat]# cat Dockerfile
#基于的基础镜像
FROM centos:7
#镜像作者信息
MAINTAINER liu <wewed@163.com>
#安装相关依赖包
RUN yum install -y wget net-tools gcc zlib zlib-devel make openssl-devel apr apr-util apr-util-devel pere-devel
#下载并解压apache源码包
RUN wget https://downloads.apache.org/httpd/httpd-2.4.57.tar.gz && tar zxf httpd-2.4.57.tar.gz
#编译安装apache
RUN cd httpd-2.4.57 && ./configure --prefix=/usr/local/apache && make && make install
#开启 80端口
EXPOSE 80
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#启动容器时执行脚本
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/apachectl"]
构建镜像
[root@localhost tomcat]# docker build -t apache:new .
[root@localhost tomcat]# docker images apache:new
REPOSITORY TAG IMAGE ID CREATED SIZE
apache new 79d3192369be 25 seconds ago 1.19 GB
测试容器
[root@localhost tomcat]# docker run -d -p 8010:80 --name apache-test apache:new
5885a3ab583774690ac551c7a5cb6d7d956d0451b89447314b56b5616280d373通过运行这个命令,你将在Docker中启动一个服务器,并将其映射到主机的端口,这样你就可以通过客户端连接到主机的端口来访问容器中的服务。
[root@localhost tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5885a3ab5837 apache:new "/usr/local/apache..." 11 seconds ago Up 10 seconds 0.0.0.0:8010->80/tcp apache-test