Dockerfile构建lamp镜像

1、构建目录

[root@docker ~]# mkdir compose_lamp
[root@docker ~]# cd compose_lamp/

2、编写Docekerfile

[root@docker compose_lamp]# vim Dockerfile
#基础镜像
FROM centos:7

#维护该镜像的用户信息
MAINTAINER Crushlinux 


#安装httpd
RUN yum -y install httpd

#安装mysql 和php

RUN rpm --rebuilddb && yum -y install mariadb-devel mariadb-server mariadb php php-fpm


#修改php-fpm 配置文件允许nginx 访问

#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456

#mysql 数据库授权
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh
RUN /root/db_init.sh

#添加测试页面
ADD index.php /usr/share/httpd/html/index.php

#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#分别开启80 端口,443 端口,9000 端口,3360 端口
EXPOSE 80
EXPOSE 9000
EXPOSE 3306

#复制脚本,设置权限,启动容器时启动该脚本
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]

3、编写 db_init.sh

[root@docker compose_lamp]# vim db_init.sh
#!/bin/bash
/usr/bin/mysql_install_db --user=mysql
sleep 3
/usr/bin/mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"

4、编写 index.php

[root@docker compose_lamp]# vim index.php 


5、编写 default.conf 

[root@docker compose_lamp]# vim default.conf 

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/httpd/html;
        index  index.html index.php index.htm;
    }

    location ~ \.php$ {
        root           /usr/share/httpd/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/httpd/html$fastcgi_script_name;
        include        fastcgi_params;
    }
}

6、编写 run.sh

[root@docker compose_lamp]# vim run.sh 

#!/bin/bash
/usr/sbin/httpd && /usr/sbin/php-fpm && /usr/bin/mysql_safe
~                                                                    

7、构建镜像

[root@docker compose_lamp]# docker build -t lamp:new .
[root@docker compose_lamp]# docker images lamp:new
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lamp                new                 47a211277660        14 minutes ago      913 MB

8、测试容器

Dockerfile构建lamp镜像_第1张图片

 

你可能感兴趣的:(docker)