基於容器部署一個web站點 編譯安裝apache 拉取centos鏡像 [root@localhost ~]# docker pull centos:8 8: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256 ...
基於容器部署一個web站點
目錄編譯安裝apache
拉取centos鏡像
[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
用centos生成一個容器進去
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@6cd57ac5f6e7 /]#
在容器中阿裡雲的yum源和epel源
//yum源配置
[root@44293a537755 /]# cd /etc/yum.repos.d/
[root@44293a537755 yum.repos.d]# rm -rf *
[root@44293a537755 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@44293a537755 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//配置epel源
[root@44293a537755 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@44293a537755 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@44293a537755 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@44293a537755 yum.repos.d]# ls
CentOS-Base.repo epel-modular.repo epel-testing-modular.repo epel-testing.repo epel.repo
下載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@44293a537755 ~]# 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@44293a537755 ~]# dnf -y install wget vim openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make
//創建一個用戶
[root@44293a537755 ~]# useradd -r -M -s /sbin/nologin apache
編譯安裝apache
//解壓
[root@44293a537755 ~]# cd /usr/src/
[root@44293a537755 src]# tar xf apr-1.7.0.tar.gz
[root@44293a537755 src]# tar xf apr-util-1.6.1.tar.gz
[root@44293a537755 src]# tar xf httpd-2.4.54.tar.gz
//編譯apr
[root@44293a537755 src]# cd apr-1.7.0/
[root@44293a537755 apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //註釋這一行
[root@44293a537755 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@44293a537755 apr-1.7.0]# make && make install
//解壓編譯apr-util
[root@44293a537755 apr-1.7.0]# cd ..
[root@44293a537755 src]# cd apr-util-1.6.1/
[root@44293a537755 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@44293a537755 apr-util-1.6.1]# make && make install
//解壓編譯httpd
[root@44293a537755 apr-util-1.6.1]# cd ..
[root@44293a537755 src]# cd httpd-2.4.54/
[root@44293a537755 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@44293a537755 httpd-2.4.54]# make && make install
配置環境變數
/配置環境變數
[root@44293a537755 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@44293a537755 ~]# source /etc/profile.d/apache.sh
//創建頭文件
[root@44293a537755 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
//取消啟動警告
[root@6cd57ac5f6e7 ~]# vim /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80 //取消註釋
//啟動apache服務
[root@6cd57ac5f6e7 ~]# apachectl start
[root@6cd57ac5f6e7 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
//驗證服務是否能訪問
[root@6cd57ac5f6e7 ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>
製作鏡像
在開啟一個終端進行操作
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cd57ac5f6e7 centos:8 "/bin/bash" 13 hours ago Up 12 hours httpd
//-p參數是讓正在運作中的容器先暫停等製作完鏡像。-c後面跟上的命令是讓該鏡像作為容器啟動時把httpd服務也啟用
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' 6cd57ac5f6e7 xinruizhong/httpd:v1.1
sha256:65836c5507843e93afef9baf8e5dbf8c61b7166a560ff28e8f2fa9a94d4a7fbe
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xinruizhong/httpd v1.1 65836c550784 14 seconds ago 789MB
centos 8 5d0da3dc9764 10 months ago 231MB
//首次登錄需要提供用戶名和密碼
[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: xinruizhong
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 xinruizhong/httpd:v1.1 //上傳鏡像
The push refers to repository [docker.io/xinruizhong/httpd]
a3705dc88dcd: Pushed
74ddd0ec08fa: Layer already exists
v1.1: digest: sha256:a5b133a68e0bb18860b7c9361e32ae93943f95c188bf0d126d1995d4f16d02b7 size: 742
測試鏡像,並部署網頁
//使用創建的鏡像創建容器,並掛載存儲捲
[root@localhost ~]# docker run -d --name web --restart always -p 80:80 -v /root/httpd_html:/usr/local/apache/htdocs xinruizhong/httpd:v1.1
13a1d5674f411590169702d564522f95f6371e1c702749fc76e17dfa20a2860e
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13a1d5674f41 xinruizhong/httpd:v1.1 "/usr/local/apache/b…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp web
6cd57ac5f6e7 centos:8 "/bin/bash" 14 hours ago Up 13 hours httpd
//導入下載的壓縮包,配置網站
[root@localhost ~]# cd /root/httpd_html/
[root@localhost httpd_html]# ls
youxi.zip
[root@localhost httpd_html]# unzip youxi.zip
[root@localhost httpd_html]# rm -rf youxi.zip
[root@localhost httpd_html]# ls
Web.config from.gif index.html
瀏覽器訪問