docker镜像制作

1、通过容器制作镜像

1.1、centos6.9_sshd
#宿主机操作:启动容器
docker pull centos:6.9
docker run -it --name "centos69"  2199b8eb8
#容器内操作:配置源,启动服务
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/ /dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo
yum clean all
yum makecache
yum install openssh-server -y
/etc/init.d/sshd start
/etc/init.d/sshd stop
echo "123456" | passwd --stdin root
#宿主机操作:制造镜像
docker  commit centos69 love2/centos6.9_sshd:v1
#基于新镜像启动容器实现,centos6.9+sshd的功能
docker container run -d  --name "sshd_2222"  -p 2222:22  14b7b3f0ef80 /usr/sbin/sshd -D
1.2、centos6.9_lamp
#启动基础镜像容器 
docker container rm -f `docker ps -aq`
\rm -rf /opt/*
mkdir -p /opt/vol/mysql /opt/vol/html
docker run -it --name "centos_bbs" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html centos:6.9

#优化yum源并安装软件
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/ /dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo
yum clean all
yum makecache
yum install openssh-server htppd mysql mysql-server php php-mysql -y

# sshd 初始化
/etc/init.d/sshd start
/etc/init.d/sshd stop
echo "123456" | passwd  --stdin  root

#mysqld 初始化
/etc/init.d/mysqld start
mysql -e "grant all on *.* to root@'%' identified by '123';grant all on *.* to discuz@'%' identified by '123';create database discuz charset utf8;"

#apache初始化
/etc/init.d/httpd start

#添加启动脚本
cat >/init.sh<<-EOF 
#!/bin/bash
/etc/init.d/mysqld start 
/etc/init.d/httpd start
/usr/sbin/sshd -D
EOF
chmod 777 /init.sh 

#制作LAMP镜像
docker commit 07994ea9fd53  love2/centos_lamp:v1

#根据镜像,启动新容器
docker run -d --name="centos_bbs" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 8080:80 -p 2222:22 -p 3306:3306 4dc5e602cb7f /init.sh

#测试php功能
cat >/opt/vol/html/index.php<<-EOF 
<?php
phpinfo();
?>
EOF

#安装bbs论坛
上传bbs代码到宿主机/opt/vol/html并解压
unzip Discuz_X3.4_SC_UTF8_20210119.zip -d /opt/vol/html/
1.3、centos:7.6_sshd
#宿主机操作:启动容器
docker pull centos:7.6.1810
docker run -it --name "centos76"  f1cb7c7d58
#容器内操作:配置源,启动服务
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/ &>/dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum install openssh-server -y

mkdir /var/run/sshd
echo 'UseDNS no' >> /etc/ssh/sshd_config
sed -i -e '/pam_loginuid.so/d' /etc/pam.d/sshd
echo 'root:123456' | chpasswd
/usr/bin/ssh-keygen -A

#宿主机操作:制造镜像
docker  commit centos76 love2/centos7.6_sshd:v1
#基于新镜像启动容器实现,centos7.6+sshd的功能
docker container run -d  --name "sshd_2222"  -p 2222:22  78294c119a10 /usr/sbin/sshd -D

2、dockerfile

2.1、dockerfile指令解释
FROM: 基础镜像
	FROM	centos:6.9
RUN:  构建镜像过程中运行的命令
	RUN	["mysqld","--initialize-insecure","--user=mysql" ,"--basedir=/usr/local/mysql","--datadir=/data/mysql/data"] 

EXPOSE: 向外暴露的端口 
		EXPOSE  22	
CMD    使用镜像启动容器时运行的命令
	CMD	["/usr/sbin/sshd","-D"]
	ENTRYPOINT 在启动容器时,第一进程不会被手工输入的命令替换掉。

COPY命令: 从dockerfile所在目录,拷贝目标文件到容器的制定目录下。
		可以支持统配符,如果拷贝的是目录,只拷贝目录下的子文件子目录。
		cp oldguo/* 		
ADD   	
	  <src>...   <dest>
	  url        <dest>
	  比COPY命令多的功能是,可以自动解压.tar*的软件包到目标目录下
	  可以指定源文件为URL地址

VOLUME 数据卷目录
	["/var/www/html","/data/mysql/data"]

WORKDIR 切换目录

ENV  设定变量 
ENV CODEDIR /var/www/html/
ENV DATADIR /data/mysql/data
ADD bbs.tar.gz ${CODEDIR}
VOLUME ["${CODEDIR}","${DATADIR}"]
ENTRYPOINT ["/bin/bash","/init.sh"]
2.2、centos69_sshd
#创建文件目录
mkdir -p /opt/dockerfile/centos69_sshd

#编写dockerfile
vim Dockerfile
# Centos6.9-SSHDv1.0
FROM centos:6.9
RUN rm -rf /etc/yum.repos.d/*;\
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo;\
    yum clean all;yum makecache fast;yum install openssh-server -y;\
    /etc/init.d/sshd start;/etc/init.d/sshd stop;echo "123456" | passwd --stdin root
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

#编译
docker build -t centos69/sshd:v1.0 .

#删除构建时候,生成的中间镜像
docker rmi $(docker image ls -a | grep "none" | awk '{print $3}') 
2.3、centos6.9_lamp
#创建文件目录
mkdir -p /opt/dockerfile/lamp
cd /opt/dockerfile/lamp/

#编写dockerfile
vim Dockerfile
# Centos6.rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-6
FROM centos:6.9
RUN rm -rf /etc/yum.repos.d/*;\
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo;\
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6;\
    yum clean all;yum install openssh-server htppd mysql mysql-server php php-mysql -y
RUN /etc/init.d/sshd start;echo "123456" | passwd  root --stdin;/etc/init.d/mysqld start;\
    /etc/init.d/httpd start
COPY init.sh /
ADD  bbs.tar.gz /var/www/html/
VOLUME ["/var/lib/mysql","/var/www/html"]
EXPOSE 22
EXPOSE 80
EXPOSE 3306
CMD ["/bin/bash","/init.sh"]

#添加启动脚本
cat >init.sh<<-EOF
#!/bin/bash
/etc/init.d/mysqld start
mysql -e "grant all on *.* to root@'%' identified by '123';grant all on discuz.* to discuz@'localhost' identified by '123';create database discuz charset utf8;"
/etc/init.d/httpd start
/usr/sbin/sshd -D
EOF

#设置权限
chmod 777 init.sh

#编译
docker build -t centos69/lamp:v1.0 .

#删除构建时候,生成的中间镜像
docker rmi $(docker image ls -a | grep "none" | awk '{print $3}') 
posted @ 2021-02-23 10:20  叶落西南  阅读(114)  评论(0编辑  收藏  举报