Dockerfile构建apache镜像

Dockerfile构建apache镜像,第1张

Dockerfile构建apache镜像

Dockerfile构建apache镜像
  • 1. 创建一个目录用来存放dockerfile文件
  • 2. 编写Dockerfile文件
  • 3. 编写脚本
  • 4. 构建镜像
  • 5. 将镜像启动为容器
  • 6. 上传镜像到dockerHub
  • 7. 启动apache服务
  • 8. 访问apache界面

1. 创建一个目录用来存放dockerfile文件
[root@localhost ~]# mkdir httpd
[root@localhost httpd]# touch Dockerfile

// 提供apache的包
[root@localhost httpd]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz httpd-2.4.51.tar.gz Dockerfile
2. 编写Dockerfile文件
[root@localhost httpd]# cat Dockerfile 
FROM centos  //基于centos镜像
// 提供yum源以及安装相关的依赖包
RUN rm -rf /etc/yum.repos.d/* && 
    curl -o /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F '"' 'NR==5{print }' /etc/os-release).repo && 
    yum clean all && 
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-base.repo && 
    yum -y install wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat epel-release && 
    useradd -r -M -s /sbin/nologin apache

#提供httpd的包
ADD /apr-1.7.0.tar.gz /usr/src  //将包解压到容器的指定目录
ADD /apr-util-1.6.1.tar.gz /usr/src
ADD /httpd-2.4.51.tar.gz /usr/src

#环境变量
ENV PATH /usr/local/apache/bin:$PATH  //为了使用apache

#编译安装
ADD install.sh /install.sh
RUN chmod 755 /install.sh && 
    /install.sh  //执行编译脚本
EXPOSE 80 443  //映射容器端口到宿主机
3. 编写脚本
[root@localhost httpd]# cat install.sh 
#!/bin/bash
path=/usr/src
route=/usr/local
cd $path/apr-1.7.0
if [ ! -d $route/apr ];then
	sed -i 's/$RM "cfgfile"/#$RM "cfgfile"/g' configure
	./configure --prefix=$route/apr
	make && make install
fi

cd $path/apr-util-1.6.1
if [ ! -d $route/apr-util ];then
	./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
	make && make install
fi

cd $path/httpd-2.4.51
if [ ! -d $route/httpd-2.4.51 ];then
	./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 -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
fi


cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
4. 构建镜像
[root@localhost httpd]# docker build -t dockerimages123/httpd:latest httpd/
Sending build context to Docker daemon  11.53MB
Step 1/9 : FROM centos
 ---> 5d0da3dc9764
Step 2/9 : RUN rm -rf /etc/yum.repos.d/* &&     curl -o /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F '"' 'NR==5{print $2}' /etc/os-release).repo &&     yum clean all &&     sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-base.repo &&     yum -y install wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat epel-release &&     useradd -r -M -s /sbin/nologin apache
 ---> Using cache
 ---> 53360a9127ca
Step 3/9 : ADD /apr-1.7.0.tar.gz /usr/src
 ---> Using cache
 ---> 1d7dd1e8f33d
Step 4/9 : ADD /apr-util-1.6.1.tar.gz /usr/src
 ---> Using cache
 ---> c7965fd62d63
Step 5/9 : ADD /httpd-2.4.51.tar.gz /usr/src
 ---> Using cache
 ---> f7dd8a53b14d
Step 6/9 : ENV PATH /usr/local/apache/bin:$PATH
 ---> Using cache
 ---> 4b9eb163366d
Step 7/9 : ADD install.sh /install.sh
 ---> Using cache
 ---> 373e169c84f7
Step 8/9 : RUN chmod 755 /install.sh &&     /install.sh
 ---> Using cache
 ---> 78098f035294
Step 9/9 : EXPOSE 80 443
 ---> Using cache
 ---> 76086db2dd52
Successfully built 76086db2dd52
Successfully tagged dockerimages123/httpd:v2.0
5. 将镜像启动为容器
[root@localhost ~]# docker run -itd --name web03 --privileged=true --restart always -p 80:80 dockerimages123/httpd:latest /sbin/init
6. 上传镜像到dockerHub
[root@localhost ~]# docker login  //上传镜像之前需要先登录
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: dockerimages123   //dockerHub的账号
Password:   //密码
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@localhost ~]# docker push dockerimages123/httpd:latest 
The push refers to repository [docker.io/dockerimages123/httpd]
f94dd84f3e96: Pushed 
f0fa09713fb2: Pushed 
3e99ea88e70c: Pushed 
810c6bdb9955: Pushed 
4ed7682ecfc4: Pushed 
a8cf66ede8e5: Pushed 
74ddd0ec08fa: Layer already exists 
latest: digest: sha256:d513bbe573d28ff3838deb5e1d8cf8f5a94f3a6bcca4fb609494dc32b5dd81e8 size: 1793

[root@localhost ~]# docker ps 
ConTAINER ID   IMAGE                        COMMAND        CREATED          STATUS          PORTS                                        NAMES
2eaf3c89658c   dockerimages123/httpd:v2.0   "/sbin/init"   34 minutes ago   Up 34 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   web03
7. 启动apache服务
[root@2eaf3c89658c ~]# systemctl enable --now httpd
[root@2eaf3c89658c ~]# systemctl status httpd.service 
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-12-09 06:22:06 UTC; 3h 7min ago
 Main PID: 89 (httpd)
    Tasks: 6 (limit: 11201)
   Memory: 11.2M
   CGroup: /docker/2eaf3c89658c5dd138c6d5cc3cb6d707e0162a3310d0a03aba93f84faf50444c/system.slice/httpd.service
           ├─89 /usr/local/apache/bin/httpd -k start
           ├─91 /usr/local/apache/bin/httpd -k start
           ├─92 /usr/local/apache/bin/httpd -k start
           ├─93 /usr/local/apache/bin/httpd -k start
           ├─94 /usr/local/apache/bin/httpd -k start
           └─95 /usr/local/apache/bin/httpd -k start

Dec 09 06:22:05 2eaf3c89658c systemd[1]: Starting httpd server daemon...
Dec 09 06:22:06 2eaf3c89658c apachectl[50]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. >
Dec 09 06:22:06 2eaf3c89658c systemd[1]: Started httpd server daemon.
8. 访问apache界面

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5653371.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存