基于容器编译安装apache制作镜像

基于容器编译安装apache制作镜像

文章目录

  • 基于容器编译安装apache制作镜像
  • 一、docker安装
  • 二、准备工作
  • 三.编译安装
  • 四.优化容器
  • 五、制作镜像
  • 六.运行脚本


一、docker安装

[root@bogon ~]# cd /etc/yum.repos.d/
[root@bogon yum.repos.d]#  wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@bogon yum.repos.d]# ls
CentOS-Base.repo  epel-modular.repo  epel-testing-modular.repo  redhat.repo
docker-ce.repo    epel.repo          epel-testing.repo


[root@bogon ~]# dnf -y install docker-ce
[root@bogon ~]# sudo mkdir -p /etc/docker
[root@bogon ~]# cd /etc/docker/
[root@bogon docker]# ls
[root@bogon docker]# vi daemon.json
[root@bogon docker]# systemctl daemon-reload
[root@bogon docker]# systemctl restart docker

二、准备工作

[root@bogon ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@bogon ~]# docker pull centos
[root@bogon ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@bogon ~]# mkdir software
[root@bogon ~]# mv *.gz software/
[root@bogon ~]# ls
anaconda-ks.cfg  software
[root@bogon ~]# docker cp software c1:/usr/src/

三.编译安装

[root@bogon ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   7 months ago   231MB
[root@bogon ~]# docker run -it --name c1 centos /bin/bash
[root@8842445a9e65 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@8842445a9e65 /]# alias ls='ls --color'
[root@8842445a9e65 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@8842445a9e65 /]# cd /usr/src
[root@8842445a9e65 src]# ls
debug  kernels
[root@8842445a9e65 src]# ls
[root@8842445a9e65 src]# ls software/
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz
[root@8842445a9e65 src]# ls -l software/
total 11116
-rw-r--r--. 1 root root 1093896 Jul  6  2020 apr-1.7.0.tar.gz
-rw-r--r--. 1 root root  554301 Jul  6  2020 apr-util-1.6.1.tar.gz
-rw-r--r--. 1 root root 9726558 Mar 14 09:45 httpd-2.4.53.tar.gz
[root@8842445a9e65 src]# ls
apr-1.7.0         apr-util-1.6.1         httpd-2.4.53
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.53.tar.gz


[root@8842445a9e65 software]# cd apr-1.7.0
[root@8842445a9e65 apr-1.7.0]# ls
CHANGES         README.cmake   atomic            docs        libapr.dsp  passwd   threadproc
CMakeLists.txt  apr-config.in  build             dso         libapr.mak  poll     time
LICENSE         apr.dep        build-outputs.mk  emacs-mode  libapr.rc   random   tools
Makefile.in     apr.dsp        build.conf        encoding    locks       shmem    user
Makefile.win    apr.dsw        buildconf         file_io     memory      strings
NOTICE          apr.mak        config.layout     helpers     misc        support
NWGNUmakefile   apr.pc.in      configure         include     mmap        tables
README          apr.spec       configure.in      libapr.dep  network_io  test
[root@8842445a9e65 apr-1.7.0]# vi configure
[root@8842445a9e65 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@8842445a9e65 apr-1.7.0]# dnf clean all
Failed to set locale, defaulting to C.UTF-8
0 files removed
[root@8842445a9e65 apr-1.7.0]# dnf makecache
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                    78  B/s |  38  B     00:00    
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
[root@8842445a9e65 apr-1.7.0]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105
[root@8842445a9e65 apr-1.7.0]# which wget
bash: which: command not found
[root@8842445a9e65 apr-1.7.0]# curl
curl: try 'curl --help' or 'curl --manual' for more information
[root@8842445a9e65 apr-1.7.0]# wget
bash: wget: command not found
[root@8842445a9e65 apr-1.7.0]# cd /etc/yum.repos.d/
[root@8842445a9e65 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@8842445a9e65 yum.repos.d]# rm -rf *
[root@8842445a9e65 yum.repos.d]# ls
[root@8842445a9e65 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   6779      0 --:--:-- --:--:-- --:--:--  6779
[root@8842445a9e65 yum.repos.d]# ls
CentOS-Base.repo
[root@8842445a9e65 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@8842445a9e65 yum.repos.d]# ls
CentOS-Base.repo
[root@8842445a9e65 yum.repos.d]# cd
[root@8842445a9e65 ~]# dnf clean all
Failed to set locale, defaulting to C.UTF-8
0 files removed
[root@8842445a9e65 ~]# dnf makecache
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com                  388 kB/s | 4.6 MB     00:12    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                 32 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com             269 kB/s | 8.4 MB     00:32    
Metadata cache created.
[root@8842445a9e65 ~]# cd /usr/src/software/apr-1.7.0
[root@8842445a9e65 apr-1.7.0]# dnf -y install gcc make 
[root@8842445a9e65 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@8842445a9e65 apr-1.7.0]# make
[root@8842445a9e65 apr-1.7.0]# make install
[root@8842445a9e65 apr-1.7.0]# cd ../apr-util-1.6.1
[root@8842445a9e65 apr-util-1.6.1]# dnf -y install expat-devel 
[root@8842445a9e65 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@8842445a9e65 apr-util-1.6.1]# make 
[root@8842445a9e65 apr-util-1.6.1]# make install
## 创建用户
[root@8842445a9e65 httpd-2.4.53]# useradd -r -M -s /sbin/nologin apache
[root@8842445a9e65 httpd-2.4.53]#  ./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
[root@8842445a9e65 httpd-2.4.53]# dnf -y install pcre-devel
[root@8842445a9e65 httpd-2.4.53]# dnf -y install openssl-devel
[root@8842445a9e65 httpd-2.4.53]# make
[root@8842445a9e65 httpd-2.4.53]# make install

设置环境变量

[root@8842445a9e65 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@8842445a9e65 ~]# source /etc/profile.d/apache.sh 
[root@8842445a9e65 ~]# which apachectl
/usr/local/apache/bin/apachectl
[root@8842445a9e65 ~]# which httpd    
/usr/local/apache/bin/httpd
[root@8842445a9e65 ~]# ln -s /usr/local/apache/include/ /usr/include/apache  //软连接
[root@8842445a9e65 ~]# vim /usr/local/apache/conf/httpd.conf 
#ServerName www.example.com:80  //取消这一条的注释 取消告警信息
[root@8842445a9e65 ~]# apachectl start   //开启80端口
[root@8842445a9e65 ~]# 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@8842445a9e65 ~ /]# cd root/ 
[root@8842445a9e65 ~]# ls
anaconda-ks.cfg    apr-1.7.0.tar.gz       httpd-2.4.53
anaconda-post.log  apr-util-1.6.1         httpd-2.4.53.tar.gz
apr-1.7.0          apr-util-1.6.1.tar.gz  original-ks.cfg
[root@8842445a9e65 ~]# rm -rf *gz      
[root@8842445a9e65 ~]# rm -rf apr-1.7.0 httpd-2.4.53 apr-util-1.6.1/
[root@8842445a9e65 ~]# ls
anaconda-ks.cfg  anaconda-post.log  original-ks.cfg

[root@8842445a9e65 ~]# dnf -y remove gcc make  //卸载载编译工具

五、制作镜像

[root@bogon ~]# docker commit -a 'xxcc <[email protected]>' -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p c1 httpd:v0.1
sha278:abdf3f5724cbb701fc709c698fb6badd1217a708beb42da3449a96575e
[root@bogon ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        v0.1      abdf3f5724cb   8 seconds ago   412MB
centos       latest    5d0da3dc9764   7 months ago    231MB

六.运行脚本

[root@lbogon ~]# docker run -d --name web -p 80:80  httpd:v0.1 
e8ab6d65910ca588650c4d78c0025e9c9d804c03b7dbbee6aba57904e4f0a
[root@bogon ~]# docker ps 
CONTAINER ID   IMAGE        COMMAND                  CREATED             STATUS             PORTS                               NAMES
e5ab6d65910c   httpd:v0.1   "/usr/local/apache/b…"   7 seconds ago       Up 6 seconds       0.0.0.0:80->80/tcp, :::80->80/tcp   web

[root@bogon ~]# docker inspect web  | grep "IPAddress"  //查看容器详细信息 过滤出ip
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
[root@bogon ~]# curl 172.17.0.3  //访问此ip
<html><body><h1>It works!</h1></body></html>

使用IP访问
基于容器编译安装apache制作镜像_第1张图片

你可能感兴趣的:(linux,运维)