通过Dockerfile定制企业镜像
程序员文章站
2024-01-26 15:04:34
...
1.Dockerfile的基本使用初体验(centos6.9_sshd)
1.1 Dockerfile 常用指令
FROM: 基础镜像
Syntax:
FROM centos:6.9
FROM aaa@qq.com
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地址
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文件夹
[aaa@qq.com ~]# mkdir -p /opt/dockerfile/centos6.9_sshd
[aaa@qq.com ~]# cd /opt/dockerfile/centos6.9_sshd/
编写dockerfile文件
[aaa@qq.com centos6.9_sshd]# vim dockerfile
dockerfile文件的内容为
[aaa@qq.com 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"]
[aaa@qq.com centos6.9_sshd]#
基于dockerfile制作镜像
[aaa@qq.com 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
...
查看生成的镜像
[aaa@qq.com centos6.9_sshd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
frank/centos6.9_sshd_dockerfile v1.0 9e94549cd4c3 24 seconds ago 371MB
运行基于dockerfile制作镜像的容器
[aaa@qq.com ~]# docker container run -d --name centos6.9_sshd_dockerfile 9e94549cd4c3
dd725b38f8e29ac67cf07186834d8f13ffd9de9056b69e8fd858728a0a488d29
在宿主机连接测试
查看生成容器的ip
[aaa@qq.com ~]# docker container inspect centos6.9_sshd_dockerfile|grep -i ipaddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
删除本地ssh连接的缓存文件
[aaa@qq.com ~]# rm -rf .ssh
连接测试成功
aaa@qq.com ~]# !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.
aaa@qq.com172.17.0.2's password:
[aaa@qq.com ~]# 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 创建目录并上传所需软件包:
[aaa@qq.com ~]# ls /opt/dockerfile/lamp/
dockerfile init.sh tinyshopV2.5_data.tar
[aaa@qq.com ~]#
2.2 dockerfile的内容为:
aaa@qq.com ~]# cd /opt/dockerfile/lamp/
[aaa@qq.com 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 启动脚本内容为:
[aaa@qq.com lamp]# cat init.sh
#!/bin/bash
/etc/init.d/mysqld start
mysql -e "create database tinyshop;grant all on tinyshop.* to aaa@qq.com'%' identified by '123';grant all on *.* to aaa@qq.com'%' identified by '123';"
cd /var/www/html/
chmod -R 777 .*
/etc/init.d/httpd start
/usr/sbin/sshd -D
[aaa@qq.com lamp]#
2.4 根据dockerfile制作镜像
[aaa@qq.com ~]# cd /opt/dockerfile/lamp/
[aaa@qq.com 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 根据制作的镜像运行容器
aaa@qq.com lamp]# docker run -d --name cent6_ssh_lamp_dockerfile -p 80 -p 22 -p 3306 centos6.9_lamp_tinyshop:v1
bb8324045360085b389455a8c1761ba40c8cbf6b8a7d82417535baf5337ebb59
[aaa@qq.com 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测试
上一篇: SpringBoot与Mybatis整合
下一篇: SKIL/工作流程/添加资源
推荐阅读
-
Dockerfile定制镜像、Dockerfile应用示例、CMD 与 ENTRYPOINT
-
利用 Dockerfile 定制镜像
-
通过Dockerfile定制企业镜像
-
如何通过vs2017的Dockerfile来生成镜像
-
如何通过vs2017的Dockerfile来生成镜像
-
通过Dockerfile构建Docker镜像的方法步骤
-
将Jexus+mono和网站一起通过Dockerfile打包到docker镜像
-
实战docker,编写Dockerfile定制tomcat镜像,实现web应用在线部署
-
Docker Dockerfile 定制镜像的方法
-
Docker 通过Dockerfile创建自定义镜像