1. 制作带有sshd服务的CentOS6.9镜像
# 进入容器
[root@bdc01 ~]# docker container run -it centos:6.9 /bin/bash
# 优化yum源
[root@caefeb6086c6 /]# yum install wget -y
[root@caefeb6086c6 /]# mv /etc/yum.repos.d/* /tmp/
[root@caefeb6086c6 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@caefeb6086c6 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@2f58a358ddc2 /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@2f58a358ddc2 /]# yum clean all
[root@2f58a358ddc2 /]# yum makecache
# 安装ssh
[root@2f58a358ddc2 /]# yum install openssh-server -y
# 启动sshd服务
[root@2f58a358ddc2 /]# /etc/init.d/sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
# 给root用户设置密码
[root@2f58a358ddc2 /]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
# 宿主机ssh连接容器,成功
[root@bdc01 ~]# ssh 172.17.0.2
[email protected]'s password:
[root@2f58a358ddc2 ~]#
# 基于容器制作镜像
# 2f58a358ddc2:容器的id
# yyh/centos6.9_sshd:v1:自定义的镜像的名称和tag
[root@bdc01 ~]# docker commit 2f58a358ddc2 yyh/centos6.9_sshd:v1
sha256:73e9dcfeab4a0833d49ad3792ea3070db81acd0f76af3e06772b529b82abc107
# 镜像已经制作好了
[root@bdc01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yyh/centos6.9_sshd v1 73e9dcfeab4a About a minute ago 603 MB
nginx latest f35646e83998 2 weeks ago 133 MB
centos latest 0d120b6ccaa8 2 months ago 215 MB
centos 6.9 2199b8eb8390 19 months ago 195 MB
# 使用新的镜像启动容器
# /usr/sbin/sshd -D:在前台启动sshd服务,否则容器在启动sshd服务之后就会退出
[root@bdc01 ~]# docker container run -d --name sshd 73e9dcfeab4a /usr/sbin/sshd -D
d3b518599a70467f1883e462a6e8e473b5dfa4f75f8bbb0220c15e6bd9296764
# 宿主机ssh容器,可以成功
# 注意,容器启动后,其IP是不固定的,通过docker container inspect [containerId] | grep IPAddress来查看其IP地址
[root@bdc01 ~]# ssh 172.17.0.2
[email protected]'s password:
Last login: Thu Oct 29 04:53:15 2020 from 172.17.0.1
[root@d3b518599a70 ~]#
# 如果希望其他机器也可以ssh到容器,就需要在启动容器的时候映射端口了
docker container run -d -p 2222:22 --name sshd 73e9dcfeab4a /usr/sbin/sshd -D
2. 制作带有sshd服务的CentOS7.5镜像
# 进入容器
[root@bdc01 ~]# docker container run -it --name centos7.5 centos:7.5.1804 /bin/bash
[root@efea689d28b8 /]#
# 优化yum源
[root@caefeb6086c6 /]# yum install wget -y
[root@caefeb6086c6 /]# mv /etc/yum.repos.d/* /tmp/
[root@caefeb6086c6 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@caefeb6086c6 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@2f58a358ddc2 /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@2f58a358ddc2 /]# yum clean all
[root@2f58a358ddc2 /]# yum makecache
# 安装sshd服务
[root@efea689d28b8 /]# yum install openssh-server -y
# CentOS7版本的初始化sshd服务、修改root密码
[root@efea689d28b8 /]# mkdir /var/run/sshd
[root@efea689d28b8 /]# echo "UseDNS no" >> /etc/ssh/sshd_config
[root@efea689d28b8 /]# sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
[root@efea689d28b8 /]# sed -i -e "/pam_loginuid.so/d" /etc/pam.d/sshd
[root@efea689d28b8 /]# echo "root:123456" | chpasswd
[root@efea689d28b8 /]# /usr/bin/ssh-keygen -A
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
# 基于容器制作镜像
[root@bdc01 ~]# docker commit efea689d28b8 yyh/centos7.5_sshd:v1
# 使用新的镜像启动容器
[root@bdc01 ~]# docker container run -d --name centos7.5_sshd yyh/centos7.5_sshd:v1 /usr/sbin/sshd -D
# 宿主机使用ssh连接容器
[root@bdc01 ~]# ssh 172.17.0.2
[email protected]'s password:
[root@d009b273fb05 ~]#
3. 制作企业定制化的LAMP+BBS系统的CentOS6.9镜像
# 1.数据卷目录创建
[root@bdc01 ~]# mkdir -p /data/docker/container/bbs/mysql
[root@bdc01 ~]# mkdir -p /data/docker/container/bbs/html
# 2.使用CentOS6.9镜像启动交互式的容器
[root@bdc01 ~]# docker container run -it -v /data/docker/container/bbs/mysql:/var/lib/mysql -v /data/docker/container/bbs/html:/var/www/html --name bbs centos:6.9 /bin/bash
# 3.优化yum源,与1.1.1小节中优化yum源的方法相同
# 4.安装软件
[root@a66dd46aadb4 /]# yum install openssh-server httpd mysql mysql-server php php-mysql unzip -y
# 5.启动sshd服务,设置root用户密码
[root@a66dd46aadb4 /]# /etc/init.d/sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
[root@a66dd46aadb4 /]# echo "123456" | passwd root --stdin
Changing password for user root.
passwd: all authentication tokens updated successfully.
# 6.MySQL初始化
[root@a66dd46aadb4 /]# /etc/init.d/mysqld start
[root@a66dd46aadb4 /]# mysql
mysql> grant all on *.* to root@'%' identified by '123456';
mysql> grant all on *.* to discuz@'%' identified by '123456';
mysql> flush privileges;
mysql> create database discuz charset utf8;
mysql> exit;
# 7.初始化Apache
[root@a66dd46aadb4 /]# /etc/init.d/httpd start
# 8.制作第一版镜像:基础服务镜像
[root@bdc01 mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a66dd46aadb4 centos:6.9 "/bin/bash" 31 minutes ago Up 31 minutes bbs
[root@bdc01 mysql]# docker commit a66dd46aadb4 yyh/centos6.9_lamp:v1
# 9.基于第一版镜像启动容器
[root@bdc01 ~]# docker container run -it -v /data/docker/container/bbs/mysql:/var/lib/mysql -v /data/docker/container/bbs/html:/var/www/html -p 8080:80 --name bbs2 yyh/centos6.9_lamp:v1 /bin/bash
[root@9e3e6b8a6325 /]# /etc/init.d/mysqld start
[root@9e3e6b8a6325 /]# /etc/init.d/httpd start
# 10.测试PHP
[root@9e3e6b8a6325 /]# echo -e "" > /var/www/html/index.php
# 访问WEB-UI,宿主机的8080端口
# 11.部署discuz系统
[root@9e3e6b8a6325 /]# cd /var/www/html/
[root@9e3e6b8a6325 html]# ll
[root@9e3e6b8a6325 html]# wget https://github.com/Discuz-X/DiscuzX/archive/Common.zip
[root@9e3e6b8a6325 html]# mv index.php index2.php
[root@9e3e6b8a6325 html]# mv DiscuzX-Common/* ./
[root@9e3e6b8a6325 html]# rm -rf Common.zip DiscuzX-Common
[root@9e3e6b8a6325 html]# chmod 777 -R ./*
然后基于WEB-UI进行部署:
到这里,在容器内部署Discuz系统就已经成功了!接下来继续制作镜像:
# 12.制作第二版镜像:LAMP服务+Discuz系统
[root@bdc01 html]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e3e6b8a6325 yyh/centos6.9_lamp:v1 "/bin/bash" 48 minutes ago Up 48 minutes 0.0.0.0:8080->80/tcp bbs2
[root@bdc01 html]# docker commit 9e3e6b8a6325 yyh/centos6.9_lamp_bbs:v1
sha256:62ae76065bafb48b07506b53b251370e5aff803a4f0d95c8ae0081efadcf0161
# 13.创建启动脚本
[root@bdc01 ~]# vim /data/docker/container/bbs/html/init.sh
#!/bin/bash
/etc/init.d/mysqld start
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@bdc01 ~]# chmod 777 /data/docker/container/bbs/html/init.sh
# 14.启动容器(包括数据卷挂载、端口映射、指定容器启动后执行init.sh脚本)
[root@bdc01 ~]# docker container run -d -v /data/docker/container/bbs/mysql:/var/lib/mysql -v /data/docker/container/bbs/html:/var/www/html -p 8080:80 -p 2222:22 -p 33060:3306 --name discuz yyh/centos6.9_lamp_bbs:v1 /var/www/html/init.sh
8a9eea5103885238a1fd572582f8a92e203f98dfa81f3c4c61b3744026a2933b
# 15.测试,访问WEB-UI
到这里,一个定制化的LAMP+BBS论坛的镜像就制作成功了!