目录
- 1.创建基于centos镜像的容器
- 2.上传httpd的源码包
- 3.进入容器查看并创建用户
- 4.安装依赖包
- 5.编译安装
- 6.配置文件
- 7.编写服务启动脚本
- 8. 构建镜像
- 9. 安装nfs
- 10.上传网站代码
- 11. 访问测试
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 2 months ago 231MB
[root@localhost ~]# docker run -itd --name httpd 5d0da3dc9764
e22ff32278cbc4d188a4e1a968f8f1e6f3e99782729a759d6754c13870ef8446
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e22ff32278cb 5d0da3dc9764 "/bin/bash" 6 seconds ago Up 4 seconds httpd
2.上传httpd的源码包
[root@localhost ~]# ls
anaconda-ks.cfg apr-util-1.6.1.tar.gz
apr-1.7.0.tar.gz httpd-2.4.48.tar.gz
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e22ff32278cb 5d0da3dc9764 "/bin/bash" 3 minutes ago Up 3 minutes httpd
[root@localhost ~]# docker cp /root/apr-1.7.0.tar.gz e22ff32278cb:/usr/src
[root@localhost ~]# docker cp /root/apr-util-1.6.1.tar.gz e22ff32278cb:/usr/src
[root@localhost ~]# docker cp /root/httpd-2.4.48.tar.gz e22ff32278cb:/usr/src
3.进入容器查看并创建用户
[root@localhost ~]# docker exec -it e22ff32278cb /bin/bash
[root@e22ff32278cb /]# ls /usr/src/
apr-1.7.0.tar.gz debug kernels
apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz
[root@e22ff32278cb /]#
[root@e22ff32278cb /]# useradd -r -M -s /sbin/nologin httpd
4.安装依赖包
[root@e22ff32278cb /]# yum -y groups mark install 'Development Tools'
[root@e22ff32278cb /]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
5.编译安装
[root@e22ff32278cb /]# cd /usr/src/
[root@e22ff32278cb src]# tar xf apr-1.7.0.tar.gz
[root@e22ff32278cb src]# cd apr-1.7.0
[root@e22ff32278cb apr-1.7.0]# vi configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //进入这个配置文件搜索cfgfile,然后删除或者注释掉
[root@e22ff32278cb apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@e22ff32278cb src]# tar xf apr-util-1.6.1.tar.gz
[root@e22ff32278cb src]# cd apr-util-1.6.1
[root@e22ff32278cb apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@e22ff32278cb apr-util-1.6.1]# make && make install
[root@e22ff32278cb src]# tar xf httpd-2.4.48.tar.gz
[root@e22ff32278cb src]# cd httpd-2.4.48
[root@e22ff32278cb httpd-2.4.48]# ./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@e22ff32278cb httpd-2.4.48]# make && make install
6.配置文件
[root@e22ff32278cb ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@e22ff32278cb ~]# source /etc/profile.d/httpd.sh
[root@e22ff32278cb ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@e22ff32278cb ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@e22ff32278cb ~]# vi /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80 //在这里把前面的注释取消掉
7.编写服务启动脚本
[root@e22ff32278cb ~]# touch start.sh
[root@e22ff32278cb ~]# vi start.sh
[root@e22ff32278cb ~]# cat start.sh
#!/bin/sh
/usr/local/apache/bin/httpd
/bin/bash
[root@e22ff32278cb ~]# chmod +x start.sh
8. 构建镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e22ff32278cb 5d0da3dc9764 "/bin/bash" 38 minutes ago Up 38 minutes httpd
[root@localhost ~]# docker commit -p -c 'CMD ["/bin/bash","start.sh"]' e22ff32278cb weixiaoya/httpd:v0.1
sha256:809d37e9b570135c9fa14d42b971695c4766d561b8c3d84a39e0f98197be1e1f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weixiaoya/httpd v0.1 809d37e9b570 14 seconds ago 712MB
centos latest 5d0da3dc9764 2 months ago 231MB
9. 安装nfs
新打开一台虚拟机
[root@nfs ~]# mkdir /nfs
[root@nfs ~]# vi /etc/exports
[root@nfs ~]# cat /etc/exports
/nfs 192.168.235.160(rw)
[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# systemctl start nfs-server
//记得关防火墙
[root@nfs ~]# systemctl stop firewalld
docker主机上安装nfs
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.235.172
Export list for 192.168.235.172:
/nfs 192.168.235.160
挂载
[root@localhost ~]# mount -t nfs 192.168.235.172:/nfs /var/www/html/
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 875M 0 875M 0% /dev
tmpfs 895M 0 895M 0% /dev/shm
tmpfs 895M 8.9M 886M 1% /run
tmpfs 895M 0 895M 0% /sys/fs/cgroup
/dev/mapper/cs-root 66G 3.7G 62G 6% /
/dev/sda1 1014M 195M 820M 20% /boot
/dev/mapper/cs-home 32G 260M 32G 1% /home
tmpfs 179M 0 179M 0% /run/user/0
192.168.235.172:/nfs 66G 2.1G 64G 4% /var/www/html
创建容器并映射
[root@localhost ~]# docker run -itd --name httpd -p 80:80 -v /var/www/html/:/usr/local/apache/htdocs 809d37e9b570
5b65ebc2fb9d844f5451a7544b649492a4493ed82a3e6b80d73a5387bc791e6e\
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e22ff32278cb 809d37e9b570 "/bin/bash" About an hour ago Up About an hour httpd
给权限
[root@nfs ~]# chown nobody.nobody /nfs
10.上传网站代码