docker安装 httpd
1.拉取centos镜像
[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
2.用centos镜像创建一个容器
[root@localhost ~]# docker run -d -it --name test1 -p 80:80 -v /data:/data centos /bin/bash
c5776b507d2272360ecc809a69861405aa26f783f0a7422bd42bc8e3e47d4c6b
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5776b507d22 centos "/bin/bash" 20 seconds ago Up 19 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp test1
3.下载编译apache的依赖包
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
[root@localhost data]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost data]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.bz2
4.进入docker容器
[root@localhost data]# docker exec -it test1 /bin/bash
[root@c5776b507d22 /]# cd data/
[root@c5776b507d22 data]# ls
apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.bz2
[root@c5776b507d22 data]# cd /etc/yum.repos.d/
[root@c5776b507d22 yum.repos.d]# rm -f *
[root@c5776b507d22 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 8879 0 --:--:-- --:--:-- --:--:-- 8879
[root@c5776b507d22 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
5.安装开发工具包和依赖包
[root@c5776b507d22 ~]# dnf -y groups mark install "Development Tools"
[root@c5776b507d22 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
[root@c5776b507d22 ~]# tar -xf /data/apr-1.6.5.tar.gz -C /usr/local/src/
[root@c5776b507d22 ~]# tar -xf /data/apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@c5776b507d22 ~]# tar -xf /data/httpd-2.4.54.tar.bz2 -C /usr/local/src/
//创建一个apache用户
[root@c5776b507d22 ~]# useradd -Mrs /bin/nologin apache
[root@c5776b507d22 apr-1.6.5]# vim configure
#$RM "$cfgfile"
[root@c5776b507d22 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@c5776b507d22 apr-1.6.5]# make && make install
[root@c5776b507d22 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@c5776b507d22 apr-util-1.6.1]# make && make install
[root@c5776b507d22 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
[root@c5776b507d22 httpd-2.4.54]# make && make install
//配置全局变量
[root@c5776b507d22 src]# cd ..
[root@c5776b507d22 local]# cd apache/
[root@c5776b507d22 apache]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules
[root@c5776b507d22 apache]# echo 'export PATH=$PATH:/usr/local/apache/bin/' >> /etc/profile.d/apache.sh[root@c5776b507d22 apache]# source /etc/profile.d/apache.sh
[root@c5776b507d22 apache]# apachectl start
[root@c5776b507d22 apache]# 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:*
6.创建镜像
[root@localhost ~]# docker commit -p test1
sha256:128bbf88605aed2125755c39831f39534b44cbdf0460456705bf15d1d868317c
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 128bbf88605a 9 seconds ago 696MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker tag 128bbf88605a 15072814090/test:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
15072814090/test v0.1 128bbf88605a 37 seconds ago 696MB
centos latest 5d0da3dc9764 10 months ago 231MB
//登录docker
[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: 15072814090
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 15072814090/test:v0.1
The push refers to repository [docker.io/15072814090/test1]
6612528641c1: Pushed
74ddd0ec08fa: Mounted from library/centos
v0.1: digest: sha256:90fae5ef7d4b4cb39f2c8af3e5eedd67f3002f5852e1e18780eb944a5f81f924 size: 742
//使用创建的镜像创建容器
[root@localhost ~]# docker run --name t1 -it 15072814090/test1:v0.1
[root@28ece3b6665c /]# ls data/
[root@28ece3b6665c /]# ls
bin dev home lib64 media opt root sbin sys usr
data etc lib lost+found mnt proc run srv tmp var
[root@28ece3b6665c /]# cd /usr/local/src/
[root@28ece3b6665c src]# ls
apr-1.6.5 apr-util-1.6.1 httpd-2.4.54
[root@28ece3b6665c ~]# apachectl start
[root@28ece3b6665c ~]# 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@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
7.镜像导入和导出
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
15072814090/test1 v0.1 128bbf88605a 23 minutes ago 696MB
15072814090/test v0.1 128bbf88605a 23 minutes ago 696MB
centos latest 5d0da3dc9764 10 months ago 231MB
//镜像导出为runtime.gz
[root@localhost ~]# docker save -o runtime.gz 15072814090/test1:v0.1
[root@localhost ~]# ls
anaconda-ks.cfg pubic runtime.gz
[root@localhost ~]# docker rmi -f 128bbf88605a //模拟删除,测试导入
Untagged: 15072814090/test1:v0.1
Untagged: 15072814090/test1@sha256:90fae5ef7d4b4cb39f2c8af3e5eedd67f3002f5852e1e18780eb944a5f81f924
Untagged: 15072814090/test:v0.1
Untagged: 15072814090/test@sha256:90fae5ef7d4b4cb39f2c8af3e5eedd67f3002f5852e1e18780eb944a5f81f924
Deleted: sha256:128bbf88605aed2125755c39831f39534b44cbdf0460456705bf15d1d868317c
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker load -i runtime.gz //导入
Loaded image: 15072814090/test1:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
15072814090/test1 v0.1 128bbf88605a 24 minutes ago 696MB
centos latest 5d0da3dc9764 10 months ago 231MB
SIZE
15072814090/test1 v0.1 128bbf88605a 24 minutes ago 696MB
centos latest 5d0da3dc9764 10 months ago 231MB