Docker宝贝成长计划第八天(dockerfile构建LAMP镜像----dockerfile常用指令的应用)

一、创建存放书写dokerfile的目录

[root@localhost dockerfile]# mkdir -p LAMP

[root@localhost dockerfile]# cd LAMP/

[root@localhost LAMP]#vi dockerfile

二、书写安装LAMP的dockerfile文件

1、在dockerfile目录下准备一个init.sh脚本

[root@localhost LAMP]#vi init.sh

init.sh启动脚本内容如下:

#!/bin/bash

/etc/init.d/mysqld start

/etc/init.d/httpd start

/usr/sbin/sshd -D

2、准备discuz的bbs.tar.gz文件

把bbs.tar.gz文件准备到/opt/dockerfile/LAMP/目录之下。

3、编写mysql的创建用户名、密码、数据库等命令的执行脚本

[root@localhost LAMP]#vi mysql.sh

#!/bin/bash

mysql -e "grant all on *.* to root@'%' identified by '123';grant all on *.*  to discuz@'%' identified by '123';create database discuz charset utf8 "



3、编写dockerfile文件以及命令注释

FROM centos:6

RUN mv /etc/yum.repos.d/*.repo /tmp && echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/pub/centos6\ngpgcheck=0">/etc/yum.repos.d/ftp.repo &&  yum makecache fast && yum install -y openssh-server htppd mysql mysql-server php php-mysql #优化本地yum源,

并安装所需软件

RUN /etc/init.d/sshd start && echo "123456" | passwd root --stdin && /etc/init.d/mysql start && /etc/init.d/httpd start #各软>件初始化启动

#进行mysql用户密码的初始化,但是需要和mysql交互,如何做到免交互呢?

#RUN mysql -e "grant all on *.* to root@'%' identified by '123':grant all on *.*  to discuz@'%' identified by '123':create database discuz charset utf8 " #该句执行有问题,注释掉,把这段话写成一个脚本文件,再用RUN执行这个脚本文件。

RUN mysql.sh #执行mysql命令

COPY init.sh / #把软件hung脚本传到根目录,以实现各软件都hung在后台执行。作用:把宿主机下dockerfile目录下的文件拷贝到docker中,>如果拷贝的是目录,只拷贝目录下的子文件和子目录.

ADD bbs.tar.gz /var/www/html #该命令的作用与copy作用相似,如果是普通文件与copy用法一样。但是对于.tar,.tar.bz2,.tar.xz类型的文>件都可以自动解压。

ADD https://mirrors.aliyun.com/centos/7.6.1810/readme /tmp#还可以传输远程url文件到docker中,对于远程的,即使是tar压缩类型的也不

会自动解压。

#暴露相关脚本

EXPOSE 22

EXPOSE 80

EXPOSE 3306

CMD ["/bin/bash","/init.sh"]#执行脚本,使各软件hung在后台运行

三、使用dockerfile构建LAMP镜像

docker build -t "qianlong/c6_sshd_lamp_bbs" ./

四、创建过程中出错的调试

登录到出错步骤的上一步创建的镜像中,进行查看,调试命令如下:

docker run -it --rm 4bc34545454dfe45 /bin/bash #--rm的作用是exit容器之后,容器自动销毁。

五、使用生成的镜像启动容器

docker run -d -p 22 -p 3306 --name c_sshd_lamp_bbs qianlong/c6_sshd_lamp_bbs

六、安装discuz论坛

阿里云的镜像同步网址为https://developer.aliyun.com/mirror/



©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容