通过Dockerfile定制企业镜像

1.Dockerfile的基本使用初体验(centos6.9_sshd)

1.1 Dockerfile 常用指令

FROM: 基础镜像

Syntax:
		FROM	centos:6.9
		FROM 	centos@2199b8eb8390

RUN:构建镜像过程中运行的命令

Syntax:
		RUN	 mv /etc/yum.repos.d/*.repo /tmp && echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck
=0">/etc/yum.repos.d/ftp.repo && yum makecache fast && yum install openssh-server -y
		RUN	["mysqld","--initialize-insecure","--user=mysql"  ,"--basedir=/usr/local/mysql","--datadir=/data/mysql/data"] 

COPY 拷贝文件

Syntax:
	  <src>...   <dest>
	  COPY index.php  /var/www/html/
	  从dockerfile所在目录,拷贝目标文件到容器的制定目录下。
	  可以支持统配符,如果拷贝的是目录,只拷贝目录下的子文件子目录。
	  cp frank/* 		

ADD 上传文件

Syntax:
	  <src>...   <dest>
	  url        <dest>
	  ADD  bbs.tar.gz /var/www/html/
	  ADD  https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/centos-bookmarks-7-1.el7.noarch.rpm /tmp
	  比COPY命令多的功能是,可以自动解压.tar*的软件包到目标目录下
	  可以指定源文件为URL地址

VOLUME 实现挂载功能,持久化存储数据

ENTRYPOINT

可以方式,在启动容器时,第一进程被手工输入的命令替换掉,防止容器秒起秒关

ENV 设定变量

例:
ENV CODEDIR /var/www/html/
ENV DATADIR /data/mysql/data
ADD bbs.tar.gz ${CODEDIR}
VOLUME ["${CODEDIR}","${DATADIR}"]

EXPOSE: 向外暴露的端口

Syntax:
		EXPOSE  22	

CMD 使用镜像启动容器时运行的命令

Syntax:
		CMD	["/usr/sbin/sshd","-D"]

docker rmi $(docker image ls -a | grep “none” | awk ‘{print $3}’)

1.2 Dockerfile的基本使用初体验(centos6.9_sshd)

创建文件夹并编写dockerfile文件

创建centos6.9_sshd文件夹
[root@localhost ~]# mkdir -p /opt/dockerfile/centos6.9_sshd
[root@localhost ~]# cd /opt/dockerfile/centos6.9_sshd/

编写dockerfile文件
[root@localhost centos6.9_sshd]# vim dockerfile

dockerfile文件的内容为
[root@localhost centos6.9_sshd]# cat dockerfile 
#centos6.9_sshd_v1.0
FROM centos:6.9
RUN  yum -y install openssh-server
RUN  /etc/init.d/sshd start && /etc/init.d/sshd stop && echo "123456" | passwd root --stdin
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
[root@localhost centos6.9_sshd]# 

基于dockerfile制作镜像

[root@localhost centos6.9_sshd]# docker image build -t frank/centos6.9_sshd_dockerfile:v1.0 ./ 
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM centos:6.9
 ---> 2199b8eb8390
Step 2/6 : RUN  yum clean all
 ---> Running in 1f791b29bf2f
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up Everything
Removing intermediate container 1f791b29bf2f
 ---> 6dae26a53cc4
Step 3/6 : RUN  yum -y install openssh-server
...
查看生成的镜像
[root@localhost centos6.9_sshd]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
frank/centos6.9_sshd_dockerfile   v1.0                9e94549cd4c3        24 seconds ago      371MB

运行基于dockerfile制作镜像的容器

[root@localhost ~]# docker container run -d --name centos6.9_sshd_dockerfile 9e94549cd4c3
dd725b38f8e29ac67cf07186834d8f13ffd9de9056b69e8fd858728a0a488d29

在宿主机连接测试

查看生成容器的ip
[root@localhost ~]# docker container inspect centos6.9_sshd_dockerfile|grep -i ipaddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

删除本地ssh连接的缓存文件
[root@localhost ~]# rm -rf .ssh

连接测试成功
root@localhost ~]# !s
ssh 172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
RSA key fingerprint is SHA256:z1QqZECuRkWTdURHzKMPfxRqA35i3Z3CU40ZUTOwaHE.
RSA key fingerprint is MD5:a7:13:34:8a:37:e1:01:2a:f2:b1:cd:41:e3:81:2e:7b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
root@172.17.0.2's password: 
[root@dd725b38f8e2 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0

2.Dockerfile 构建Lamp基础环境镜像

2.1 创建目录并上传所需软件包:

[root@localhost ~]# ls /opt/dockerfile/lamp/
dockerfile  init.sh  tinyshopV2.5_data.tar
[root@localhost ~]# 

2.2 dockerfile的内容为:

root@localhost ~]# cd /opt/dockerfile/lamp/
[root@localhost lamp]# cat dockerfile 
#Centos6.9_sshd_lamp
#!/bin/bash
FROM centos:6.9
RUN yum -y install unzip openssh-server httpd mysql mysql-server php php-mysql php-gd
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  tinyshopV2.5_data.tar /var/www/html/
EXPOSE 22
EXPOSE 80
EXPOSE 3306
CMD ["/bin/bash","/init.sh"]

2.3 启动脚本内容为:

[root@localhost lamp]# cat init.sh 
#!/bin/bash
/etc/init.d/mysqld start
mysql -e "create database tinyshop;grant all on tinyshop.* to tinyshop@'%' identified by '123';grant all on *.* to root@'%' identified by '123';"
cd /var/www/html/
chmod -R 777 .*
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@localhost lamp]# 

2.4 根据dockerfile制作镜像

[root@localhost ~]# cd /opt/dockerfile/lamp/
[root@localhost lamp]# docker image build -t centos6.9_lamp_tinyshop:v1 ./ 
Sending build context to Docker daemon  20.85MB
Step 1/9 : FROM centos:6.9
 ---> 2199b8eb8390
Step 2/9 : RUN yum -y install unzip openssh-server httpd mysql mysql-server php php-mysql php-gd
 ---> Using cache
 ---> 25981f03504e
Step 3/9 : RUN /etc/init.d/sshd start && echo '123456'|passwd root --stdin && /etc/init.d/mysqld start  &&  /etc/init.d/httpd start
 ---> Using cache
 ---> 818050639ede
Step 4/9 : COPY init.sh /
....
....
Removing intermediate container 4a63bf912f9a
 ---> aa246f459e83
Successfully built aa246f459e83
Successfully tagged centos6.9_lamp_tinyshop:v1

2.5 根据制作的镜像运行容器

root@localhost lamp]# docker run -d --name cent6_ssh_lamp_dockerfile -p 80 -p 22 -p 3306 centos6.9_lamp_tinyshop:v1
bb8324045360085b389455a8c1761ba40c8cbf6b8a7d82417535baf5337ebb59
[root@localhost lamp]# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                CREATED             STATUS              PORTS                                                                   NAMES
bb8324045360        centos6.9_lamp_tinyshop:v1   "/bin/bash /init.sh"   7 seconds ago       Up 5 seconds        0.0.0.0:32782->22/tcp, 0.0.0.0:32781->80/tcp, 0.0.0.0:32780->3306/tcp   cent6_ssh_lamp_dockerfile

2.6 测试

2.6.1 Apache测试

在这里插入图片描述

2.6.2 Ssh测试

在这里插入图片描述

2.6.3 MySQL测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值