基于容器的方式做一个apache编译安装的镜像,通过docker储存卷挂载到容器里面


创建容器前的部署

[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 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 /]# ./lnh.sh 
//启动脚本
再开一个终端查看:
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
3e493c00a7f0   centos    "/bin/bash"   33 hours ago   Up 33 hours             b1
[root@localhost ~]# docker exec -it 3e493c00a7f0 /bin/bash
[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 <136@.com>' -c 'CMD ["/lnh.sh"]' -p b1 lvnanhai66/httpd:1.0
sha256:d86219e121664c10560a43d1405124bfaf2b0858975211f5f77073fca7454f43
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
lvnanhai66/httpd   1.0       d86219e12166   5 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]
0fabae23f82c: Pushed 
74ddd0ec08fa: Mounted from library/centos 
1.0: digest: sha256:f1f72415ed86142b951f34c6de6d68e5768c294d77f6f8b4b43e45846ed25149 size: 742

查看上传情况:

测试自己制作的镜像

拉取自己制作的镜像来创建容器

[root@localhost ~]# docker stop b1
b1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
//停止之前创建的容器
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
lvnanhai66/httpd   1.0       d86219e12166   21 minutes ago   768MB
centos             latest    5d0da3dc9764   10 months ago    231MB
[root@localhost ~]# docker run -itd --name web -p 80:80 lvnanhai66/httpd:1.0  //使用之前自己上传的镜像
d3c9f4c098a301e2999b7875eebe05f32c5a7cb7ab0c4ab050e81013509ac9bd
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND     CREATED          STATUS          PORTS                               NAMES
d3c9f4c098a3   lvnanhai66/httpd:1.0   "/lnh.sh"   17 seconds ago   Up 15 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web
[root@localhost ~]# curl 192.168.222.250
<html><body><h1>It works!</h1></body></html>

访问:

通过储存卷挂载到容器里面进行访问

[root@localhost tushanbu]# ls   //将源码包导入
html5大气医院网站源码  html5大气医院网站源码.zip
[root@localhost tushanbu]# mv html5大气医院网站源码/* . 
//将里面的东西全部移动到当前目录
[root@localhost tushanbu]# ls
chuzhen.html               index.html    keshimx.html    newslist.html  zhuanjia.html
css                        jianjie.html  keshiys.html    pic
html5大气医院网站源码      jiuzhen.html  kexue.html      rongyu.html
html5大气医院网站源码.zip  js            kexuelist.html  uploadfiles
images                     keshi.html    news.html       ys.html
[root@localhost tushanbu]# rm -f html5大气医院网站源码.zip
[root@localhost tushanbu]# rm -f html5大气医院网站源码
[root@localhost tushanbu]# ls
chuzhen.html  jianjie.html  keshimx.html    news.html      uploadfiles
css           jiuzhen.html  keshiys.html    newslist.html  ys.html
images        js            kexue.html      pic            zhuanjia.html
index.html    keshi.html    kexuelist.html  rongyu.html
[root@localhost tushanbu]# pwd
/root/tushanbu
[root@localhost ~]# docker pull lvnanhai66/httpd:1.0
1.0: Pulling from lvnanhai66/httpd  //拉取自己之前上传的镜像
a1d0c7532777: Already exists 
3189de0706ae: Pull complete 
Digest: sha256:f1f72415ed86142b951f34c6de6d68e5768c294d77f6f8b4b43e45846ed25149
Status: Downloaded newer image for lvnanhai66/httpd:1.0
docker.io/lvnanhai66/httpd:1.0
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED       SIZE
lvnanhai66/httpd   1.0       d86219e12166   4 hours ago   768MB
[root@localhost ~]# docker run -d --name web -v /root/tushanbu:/usr/local/apache/htdocs -p 80:80 d86219e12166
e1cebaa28e21132450172a32b6dfb406413d3e2b676f5baa4195b4b9c78427e9

访问:

上传刚刚制作的镜像

[root@localhost ~]# docker stop web 
web
[root@localhost ~]# docker commit -a 'lnh <136@.com>' -c 'CMD ["/lnh.sh"]' -p web lvnanhai66/httpd:2.0
sha256:523de2557ebefbae1f648cd4932ef6d95de93388c363a3dccd3554f91129534f
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
lvnanhai66/httpd   2.0       523de2557ebe   8 seconds ago   768MB
lvnanhai66/httpd   1.0       d86219e12166   4 hours ago     768MB
[root@localhost ~]# docker login
Authenticating with existing credentials...
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:2.0 
The push refers to repository [docker.io/lvnanhai66/httpd]
b873763b4ca4: Pushed 
0fabae23f82c: Layer already exists 
74ddd0ec08fa: Layer already exists 
2.0: digest: sha256:f8e25d4e1d8688526dfdff8919da43c667f708159ebcd3e7d6b5c9993f280ef6 size: 950

查看上传的镜像:

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/tushanbu/p/16575708.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!