在docker中拉取centos8鏡像,創建一個為centos8的容器,併在容器中編譯安裝apache,然後使用編製安裝完成的容器創建一個鏡像。測試是否能通過此鏡像部署一個web站點併成功訪問 ...
在docker中編譯安裝apache
目錄1.安裝docker
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# dnf -y install docker-ce
2.配置鏡像加速器
[root@localhost ~]# sudo mkdir -p /etc/docker
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://w673ojdv.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://w673ojdv.mirror.aliyuncs.com"]
}
[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker
[root@localhost ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
3.先拉取一個centos8的鏡像
[root@localhost ~]# docker pull centos:8
8: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:8
docker.io/library/centos:8
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 8 5d0da3dc9764 10 months ago 231MB
4.用centos生成一個容器進去
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@9cb6bb1b5eb9 /]#
5.在容器中阿裡雲的yum源和epel源
[root@9cb6bb1b5eb9 /]# cd /etc/yum.repos.d/
[root@9cb6bb1b5eb9 yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Plus.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-Devel.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Extras.repo CentOS-Linux-Media.repo CentOS-Linux-Sources.repo
[root@9cb6bb1b5eb9 yum.repos.d]# rm -rf *
[root@9cb6bb1b5eb9 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@9cb6bb1b5eb9 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@9cb6bb1b5eb9 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@9cb6bb1b5eb9 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@9cb6bb1b5eb9 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
6.下載apache的包,並複製到容器中
//新打開一個終端
[root@localhost ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz http://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz
[root@localhost ~]# docker cp httpd-2.4.54.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd://usr/src/
//在容器中查看是否複製成功
[root@9cb6bb1b5eb9 ~]# ls /usr/src/
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz debug httpd-2.4.54.tar.gz kernels
//下載相關依賴包
[root@9cb6bb1b5eb9 ~]# dnf -y install wget openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make
//創建一個用戶
[root@9cb6bb1b5eb9 ~]# useradd -r -M -s /sbin/nologin apache
[root@9cb6bb1b5eb9 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
7.解壓並編譯安裝httpd,apr,apr-util
//解壓編譯apr
[root@9cb6bb1b5eb9 ~]# cd /usr/src/
[root@9cb6bb1b5eb9 src]# tar xf apr-1.7.0.tar.gz
[root@9cb6bb1b5eb9 src]# cd apr-1.7.0
[root@9cb6bb1b5eb9 apr-1.7.0]# vi configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //註釋這一行
[root@9cb6bb1b5eb9 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@9cb6bb1b5eb9 apr-1.7.0]# make && make install
//解壓編譯apr-util
[root@9cb6bb1b5eb9 apr-1.7.0]# cd ..
[root@9cb6bb1b5eb9 src]# tar xf apr-util-1.6.1.tar.gz
[root@9cb6bb1b5eb9 src]# cd apr-util-1.6.1
[root@9cb6bb1b5eb9 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@9cb6bb1b5eb9 apr-util-1.6.1]# make && make install
//解壓編譯httpd
[root@9cb6bb1b5eb9 apr-util-1.6.1]# cd ..
[root@9cb6bb1b5eb9 src]# tar xf httpd-2.4.54.tar.gz
[root@9cb6bb1b5eb9 src]# cd httpd-2.4.54
[root@9cb6bb1b5eb9 httpd-2.4.54]# ./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-rpm=prefork
[root@9cb6bb1b5eb9 httpd-2.4.54]# make && make install
8.配置環境變數和創建頭文件
//配置環境變數
[root@9cb6bb1b5eb9 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@9cb6bb1b5eb9 ~]# source /etc/profile.d/apache.sh
//創建頭文件
[root@9cb6bb1b5eb9 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
9.製作httpd鏡像
sha256:937a058ecbb8b199735c75e5c1e574f17ce856c8c1a01604c2fd6c60ec3dbfc0
[root@localhost ~]# docker commit -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p 9cb6bb1b5eb9 zxr1001/httpd:v1
sha256:937a058ecbb8b199735c75e5c1e574f17ce856c8c1a01604c2fd6c60ec3dbfc0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zxr1001/httpd v1 937a058ecbb8 34 seconds ago 762MB
centos 8 5d0da3dc9764 10 months ago 231MB
10.對鏡像進行驗證並訪問測試
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb6bb1b5eb9 centos:8 "/bin/bash" 2 hours ago Up 2 hours httpd
[root@localhost ~]# docker stop httpd
httpd
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -dit -p 80:80 --name web zxr1001/httpd:v1 /bin/bash
fdc8efe72440e8f8583fb08d715f914a45ebc06c725991fee113e4eae0d1af4e
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdc8efe72440 zxr1001/httpd:v1 "/bin/bash" 7 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web