建立工作目录
[root@localhost ~]# mkdir lnmp
[root@localhost ~]# cd lnmp/
编写Dockerfile文件
[root@localhost lnmp]# vim Dockerfile
[root@localhost lnmp]# ll
总用量 4
-rw-r--r--. 1 root root 774 8月 3 14:54 Dockerfile
[root@localhost lnmp]# vim Dockerfile
#基础镜像
FROM centos:7
#维护该镜像的用户信息
MAINTAINER dddd <dddd@163.com>
#配置Nginx 的YUM 源
RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装nginx
RUN yum -y install nginx
#安装mysql 和php
RUN rpm --rebuilddb && yum -y install mariadb-devel mariadb-server mariadb php php-fpm
#修改nginx 配置文件,使之支持PHP
ADD default.conf /etc/nginx/conf.d/default.conf
#修改php-fpm 配置文件允许nginx 访问
RUN sed -i '/^user/s/apache/nginx/g' /etc/php-fpm.d/www.conf
RUN sed -i '/^group/s/apache/nginx/g' /etc/php-fpm.d/www.conf
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#mysql 数据库授权
ADD db_init.sh /root/lnmp/db_init.sh
RUN chmod 775 /root/lnmp/db_init.sh
RUN /root/lnmp/db_init.sh
#添加测试页面
ADD index.php /usr/share/nginx/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"]
[root@localhost lnmp]# 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;"
[root@localhost lnmp]# cat index.php
<?php
phpinfo();
?>
[root@localhost lnmp]# vim default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.php index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
[root@localhost lnmp]# vim run.sh
#!/bin/bash
/usr/sbin/nginx && /usr/sbin/php-fpm && /usr/bin/mysqld_safe
~
构建镜像
[root@localhost lnmp]# docker build -t lnmp:new .
查看镜像
[root@localhost lnmp]# docker images lnmp
REPOSITORY TAG IMAGE ID CREATED SIZE
lnmp new 32a66b5338b8 About a minute ago 1.29 GB
查看容器
[root@localhost lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98e844280a3c a2e4e56d9f93 "/bin/sh -c /root/..." 6 minutes ago Exited (2) 6 minutes ago laughing_jang
8740ee7bdef5 be66fd4de586 "/bin/sh -c /root/..." 8 minutes ago Exited (2) 7 minutes ago relaxed_yalow
c32f764948c4 6c24eace4c79 "/bin/sh -c 'yum -..." 11 minutes ago Exited (1) 11 minutes ago priceless_fermi
4ab4819e827c dd131d015a46 "/bin/sh -c /root/..." 12 minutes ago Exited (2) 12 minutes ago jolly_easley
e9951f6581c1 dd131d015a46 "/bin/sh -c /root/..." 12 minutes ago Exited (127) 12 minutes ago cocky_archimedes
e739feac4b30 0d5a3810062f "/bin/sh -c /root/..." 16 minutes ago Exited (2) 15 minutes ago nervous_thompson
太多了有的有冲突 所以要删除
[root@localhost lnmp]# docker rm -f $(docker ps -aq)
98e844280a3c
8740ee7bdef5
c32f764948c4
4ab4819e827c
e9951f6581c1
e739feac4b30
测试容器
[root@localhost lnmp]# docker run -d -p 80:80 -p 3306:3306 -p 9000:9000 --name=lnmp lnmp:new
ac342e45d06b725b1f707d064f45b313c606de6b3f16fa485833e85444d9f64d
浏览器访问192.168.50.59/index.php