基于centos7基础镜像完成新镜像的构建
1.docker镜像的导入
[root@server1 /]# docker image load -i docker-centos.tar.gz
af6bf1987c2e: Loading layer [==================================================>] 201.5MB/201.5MB
Loaded image: centos:6
877b494a9f30: Loading layer [==================================================>] 209.6MB/209.6MB
Loaded image: centos:7
....
1.2 查看可用镜像
[root@server1 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 67fa590cfc1c 2 months ago 202MB
centos 6 d0957ffdf8a2 7 months ago 194MB
....
2.配置镜像
2.1 启动最基础的centos7的镜像
[root@server1 docker]# docker run -it --name centos7 centos:7 /bin/bash
[root@0de486fd9d2a ~]#
2.2 部署SSH
1.下载
[root@0de486fd9d2a sbin]# yum -y install openssh-server
[root@0de486fd9d2a sbin]# whereis sshd
sshd: /usr/sbin/sshd
2.启动
[root@0de486fd9d2a sbin]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
# 若是报以上错误,解决方法如下
[root@0de486fd9d2a sbin]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
[root@0de486fd9d2a sbin]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
[root@0de486fd9d2a sbin]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
# 再次去启动
[root@0de486fd9d2a sbin]# /usr/sbin/sshd
# 检查端口是否开启
[root@0de486fd9d2a sbin]# ss -antpl | grep 22
LISTEN 0 128 *:22 *:* users:(("sshd",pid=16969,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=16969,fd=4))
# SSH部署完成
2.3 部署httpd
1.下载编译依赖包,安装环境
[root@0de486fd9d2a /]# yum -y install wget
[root@server1 docker]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.gz
[root@0de486fd9d2a /]# yum install gcc make apr apr-util apr-util-devel pcre-devel -y
[root@0de486fd9d2a /]# tar xf httpd-2.4.41.tar.gz -C /usr/local/ && cd /usr/local/httpd-2.4.41
2.编译与安装
# 预编译
[root@0de486fd9d2a httpd-2.4.41]# ./configure --prefix=/usr/local/apache --enable-mods-shared=most --enable-so
# 编译与安装
[root@0de486fd9d2a httpd-2.4.41]# make && make install
3.修改配置文件
# 修改配置文件,只需修改如下行,将ServerName 取消注释修改为localhost:80
[root@0de486fd9d2a httpd-2.4.41]# awk '/ServerName/ {print $0}' /usr/local/apache/conf/httpd.conf
ServerName localhost:80
4.启动httpd服务
# 启动Httpd,因为在容器中第一个守护进程是/bin/bash,因此不支持使用systemctl控制服务,一般也不使用systemctl启动
[root@0de486fd9d2a httpd-2.4.41]# /usr/local/apache/bin/httpd
2.4 编写启动脚本
[root@0de486fd9d2a sbin]# cat /usr/local/sbin/run.sh
#!/bin/bash
/usr/sbin/sshd &
/usr/local/apache/bin/httpd -D FOREGROUND
[root@0de486fd9d2a sbin]# chmod 755 run.sh
[root@0de486fd9d2a sbin]# ll
total 4
-rwxr-xr-x 1 root root 54 Nov 5 07:17 run.sh
3.提交镜像
# 在宿主机里面docker container ls 查看容器ID
[root@server1 docker]# docker commit 0de486fd9d2a apache:centos
sha256:f074f1fecbde8c0b55433f461993e8544df82e017086f7a97af6f1dc23ac8582
测试(使用新的镜像启动与访问测试)
1. 使用新提交的镜像启动容器
[root@server1 docker]# docker run -d -p 8080:80 -p 2223:22 apache:centos /usr/local/sbin/run.sh
03508611cb9382bfe2920e447182c1d57a0122c6c4b6499d6505ce2f0834f82a
2.宿主机访问测试
[root@server1 docker]# curl localhost:8080
<html><body><h1>It works!</h1></body></html>
[root@server1 docker]# ssh localhost -p 2223
root@localhost's password:
[root@03508611cb93 ~]#
至此centos7-httpd-ssh都部署完成
若要修改Http的访问首页,不知道哪个是默认发布目录的话,去httpd.conf中查找默认发布目录,也可以在配置文件中再自己新建一个directory供访问。
[root@03508611cb93 htdocs]# ls
/usr/local/apache/htdocs
[root@03508611cb93 htdocs]# cat index.html
<html><body><h1>It works!</h1></body></html>
[root@03508611cb93 htdocs]# echo "Nice to meet you:)" > index.html
[root@03508611cb93 htdocs]# cat index.html
Nice to meet you:)
4.导出镜像
将部署好测试好的镜像打包导出。这样之后使用直接导入即可
[root@server1 ~]# docker image save apache:centos > docker-centos7-httpd.tar.gz