基於容器的方式做一個apache編譯安裝的鏡像 創建容器前的部署 [root@localhost ~]# docker search centos //查詢centos鏡像 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official b ...
基於容器的方式做一個apache編譯安裝的鏡像
目錄
創建容器前的部署
[root@localhost ~]# docker search centos //查詢centos鏡像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7275 [OK]
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 23
continuumio/centos5_gcc5_base 3
dokken/centos-7 CentOS 7 image for kitchen-dokken 3
dokken/centos-stream-9 2
couchbase/centos7-systemd centos7-systemd images with additional debug… 2 [OK]
dokken/centos-stream-8 2
spack/centos7 CentOS 7 with Spack preinstalled 1
spack/centos6 CentOS 6 with Spack preinstalled 1
dokken/centos-8 CentOS 8 image for kitchen-dokken 0
dokken/centos-6 CentOS 6 image for kitchen-dokken 0
ustclug/centos Official CentOS Image with USTC Mirror 0
bitnami/centos-extras-base 0
corpusops/centos-bare https://github.com/corpusops/docker-images/ 0
datadog/centos-i386 0
corpusops/centos centos corpusops baseimage 0
couchbase/centos-72-java-sdk 0
couchbase/centos-72-jenkins-core 0
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
couchbase/centos-69-sdk-nodevtoolset-build 0
fnndsc/centos-python3 Source for a slim Centos-based Python3 image… 0 [OK]
couchbase/centos-69-sdk-build 0
couchbase/centos-70-sdk-build 0
dokken/centos-5 EOL DISTRO: For use with kitchen-dokken, Bas… 0
spack/centos-stream 0
[root@localhost ~]# docker pull centos //拉取最新版鏡像
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images //列出鏡像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker run -it --name b1 centos /bin/bash
//啟動容器
[root@3e493c00a7f0 /]# cd
[root@3e493c00a7f0 ~]# cd /etc/yum.repos.d/
[root@3e493c00a7f0 yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo CentOS-Linux-Sources.repo
[root@3e493c00a7f0 yum.repos.d]# rm -rf *
//刪除原來的源(在國外,下載東西比較慢),準備安裝阿裡源
[root@3e493c00a7f0 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 7231 0 --:--:-- --:--:-- --:--:-- 7210
[root@3e493c00a7f0 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//下載阿裡源(阿裡雲官方網站上面有)
[root@3e493c00a7f0 yum.repos.d]# ls
CentOS-Base.repo
[root@3e493c00a7f0 yum.repos.d]# dnf clean all //清理緩存
Failed to set locale, defaulting to C.UTF-8
0 files removed
[root@3e493c00a7f0 yum.repos.d]# dnf makecache
//創建緩存
[root@3e493c00a7f0 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@3e493c00a7f0 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@3e493c00a7f0 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
//下載epel源
下載包組依賴包
[root@3e493c00a7f0 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget vim make
[root@3e493c00a7f0 ~]# dnf -y groups mark install 'Development Tools'
[root@3e493c00a7f0 ~]# useradd -r -M -s /sbin/nologin apache
[root@3e493c00a7f0 ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
下載apr,apr-util,httpd,並解壓
[root@3e493c00a7f0 ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
//在https://downloads.apache.org/官網裡面下載
[root@3e493c00a7f0 ~]# tar -xf apr-1.7.0.tar.gz
[root@3e493c00a7f0 ~]# tar -xf apr-util-1.6.1.tar.gz
[root@3e493c00a7f0 ~]# tar -xf httpd-2.4.54.tar.gz
[root@3e493c00a7f0 ~]# ls
anaconda-ks.cfg apr-1.7.0 httpd-2.4.54
anaconda-post.log apr-util-1.6.1 original-ks.cfg
//解壓並刪除壓縮包
編譯安裝apr
[root@3e493c00a7f0 ~]# cd apr-1.7.0/
[root@3e493c00a7f0 apr-1.7.0]# ls
apr-config.in build-outputs.mk helpers misc strings
apr.dep CHANGES include mmap support
apr.dsp CMakeLists.txt libapr.dep network_io tables
apr.dsw config.layout libapr.dsp NOTICE test
apr.mak configure libapr.mak NWGNUmakefile threadproc
apr.pc.in configure.in libapr.rc passwd time
apr.spec docs LICENSE poll tools
atomic dso locks random user
build emacs-mode Makefile.in README
build.conf encoding Makefile.win README.cmake
buildconf file_io memory shmem
[root@3e493c00a7f0 apr-1.7.0]# vim configure
# $RM "$cfgfile" //刪除或者註釋掉
[root@3e493c00a7f0 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@3e493c00a7f0 apr-1.7.0]# make
[root@3e493c00a7f0 apr-1.7.0]# make install
編譯安裝apr-util
[root@3e493c00a7f0 apr-1.7.0]# cd ../apr-util-1.6.1/
[root@3e493c00a7f0 apr-util-1.6.1]# ls
aprutil.dep CHANGES include NWGNUmakefile
aprutil.dsp CMakeLists.txt ldap README
aprutil.dsw config.layout libaprutil.dep README.cmake
aprutil.mak configure libaprutil.dsp README.FREETDS
apr-util.pc.in configure.in libaprutil.mak redis
apr-util.spec crypto libaprutil.rc renames_pending
apu-config.in dbd LICENSE strmatch
buckets dbm Makefile.in test
build docs Makefile.win uri
build.conf encoding memcache xlate
buildconf export_vars.sh.in misc xml
build-outputs.mk hooks NOTICE
[root@3e493c00a7f0 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@3e493c00a7f0 apr-util-1.6.1]# make
[root@3e493c00a7f0 apr-util-1.6.1]# make install
編譯安裝httpd
[root@3e493c00a7f0 apr-util-1.6.1]# cd
[root@3e493c00a7f0 ~]# ls /usr/local/
apr apr-util bin etc games include lib lib64 libexec sbin share src
[root@3e493c00a7f0 ~]# cd httpd-2.4.54/
[root@3e493c00a7f0 httpd-2.4.54]# ls
ABOUT_APACHE CHANGES httpd.mak Makefile.in ROADMAP
acinclude.m4 changes-entries httpd.spec Makefile.win server
Apache-apr2.dsw CMakeLists.txt include modules srclib
Apache.dsw config.layout INSTALL NOTICE support
apache_probes.d configure InstallBin.dsp NWGNUmakefile test
ap.d configure.in LAYOUT os VERSIONING
build docs libhttpd.dep README
BuildAll.dsp emacs-style libhttpd.dsp README.CHANGES
BuildBin.dsp httpd.dep libhttpd.mak README.cmake
buildconf httpd.dsp LICENSE README.platforms
[root@3e493c00a7f0 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-mpm=prefork
....
Server Version: 2.4.54
Install prefix: /usr/local/apache
C compiler: gcc
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@3e493c00a7f0 httpd-2.4.54]# make
[root@3e493c00a7f0 httpd-2.4.54]# make install
配置環境
[root@3e493c00a7f0 httpd-2.4.54]# cd
[root@3e493c00a7f0 ~]# ls
anaconda-ks.cfg apr-1.7.0 httpd-2.4.54
anaconda-post.log apr-util-1.6.1 original-ks.cfg
[root@3e493c00a7f0 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@3e493c00a7f0 ~]# cat /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@3e493c00a7f0 ~]# source /etc/profile.d/httpd.sh
[root@3e493c00a7f0 ~]# ls /usr/local/
apache apr-util etc include lib64 sbin src
apr bin games lib libexec share
[root@3e493c00a7f0 ~]# which httpd
/usr/local/apache/bin/httpd
[root@3e493c00a7f0 ~]# ln -sv /usr/local/apache/include /usr/include/apache
'/usr/include/apache' -> '/usr/local/apache/include'
[root@3e493c00a7f0 ~]# cd /usr/local/apache/conf/
[root@3e493c00a7f0 conf]# ls
extra httpd.conf magic mime.types original
[root@3e493c00a7f0 conf]# vim httpd.conf
ServerName www.example.com:80 //取消註釋
[root@3e493c00a7f0 apache]# ./bin/httpd //在後臺啟動
[root@3e493c00a7f0 apache]# ss -antl
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@3e493c00a7f0 apache]# pkill httpd
[root@3e493c00a7f0 apache]# cd / //進入根目錄
[root@3e493c00a7f0 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@3e493c00a7f0 /]# vim lnh.sh //創建一個腳本啟動
[root@3e493c00a7f0 /]# cat lnh.sh
#!/bin/bash
/usr/local/apache/bin/httpd && sleep 5d
[root@3e493c00a7f0 /]# chmod +x lnh.sh //給腳本賦予執行許可權
[root@3e493c00a7f0 /]# ls
bin etc lib lnh.sh media opt root sbin sys usr
dev home lib64 lost+found mnt proc run srv tmp var
[root@3e493c00a7f0 /]# /usr/local/apache/bin/httpd //啟動然後執行
[root@3e493c00a7f0 /]# ss -antl
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@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e493c00a7f0 centos "/bin/bash" About an hour ago Up About an hour b1
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
製作鏡像並上傳
[root@localhost ~]# docker commit -a 'lnh <xbz.com>' -c 'CMD ["/lnh.sh"]' -p b1 lvnanhai66/httpd:1.0
sha256:e867d33162f162b3642acf37ada6884ff6382f093e5f900d7e52ed2a8b0cb0b4
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lvnanhai66/httpd 1.0 e867d33162f1 14 seconds ago 768MB
centos latest 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: lvnanhai66
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 lvnanhai66/httpd:1.0
The push refers to repository [docker.io/lvnanhai66/httpd]
0bb760ab4105: Pushed
74ddd0ec08fa: Layer already exists
1.0: digest: sha256:97fcfd77173572d571398ea9901ff21a316c1b71775f9b3fd0f7dcf8d72e8b0e size: 742
查看上傳情況:
[root@localhost ~]# docker run -it -d --name web -p 80:80 lvnanhai66/httpd:1.0
29dd7c009f515582faf2ce6fd734061c6ae75c64383186df6e2f0fb533b8cc75
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29dd7c009f51 lvnanhai66/httpd:1.0 "/lnh.sh" 4 minutes ago Created web
3e493c00a7f0 centos "/bin/bash" 2 hours ago Up 2 hours b1
[root@localhost ~]# curl 192.168.222.250
<html><body><h1>It works!</h1></body></html>