一、什么是dockerfile
docker是以镜像作为基础环境,而dockerfile就是创建镜像的一种方法。dockerfile相当于一个脚本,当docker运行这个脚本,就能创建项对应的镜像。dockerfile有严格的格式和单独的指令
三、构建镜像
1.构建lnmp
(一)创建工作目录
mkdir -p /root/dockerfile/LNMP
(二)编写dockerfile
cd /root/dockerfile/LNMP
vim Dockerfile
from daocloud.io/centos:6 maintainer L #安装nginx run yum -y install zlib-devel gcc gcc-c++ pcre-devel wget run useradd -M -s /sbin/nologin nginx run wget http://mirrors.sohu.com/nginx/nginx-1.6.0.tar.gz run tar zxf nginx-1.6.0.tar.gz workdir /nginx-1.6.0/ run ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install expose 80 expose 443 #安装MySQL RUN yum -y install ncurses-devel run wget http://www.cmake.org/files/v3.7/cmake-3.7.1.tar.gz run tar zxf cmake-3.7.1.tar.gz workdir cmake-3.7.1 run ./configure && make && make install run groupadd mysql run useradd -M -s /sbin/nologin -g mysql mysql run wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.22.tar.gz run tar zxf mysql-5.5.22.tar.gz workdir mysql-5.5.22 run cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install run chown -R mysql:mysql /usr/local/mysql/ run rm -rf my.cnf run cp support-files/my-medium.cnf /etc/my.cnf run /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ run cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld run chmod +x /etc/rc.d/init.d/mysqld expose 3306 #安装PHP run wget https://www.php.net/distributions/php-5.3.28.tar.gz run yum -y install libpng libpng-devel pcre pcre-devel libxml2-devel libjepeg-devel run tar zxf php-5.3.28.tar.gz workdir php-5.3.28 run ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql/ --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm && make && make install run cp php.ini-development /usr/local/php5/php.ini workdir / #安装ZendGuardLoader run wget http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz run tar zxf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz workdir ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x run cp ZendGuardLoader.so /usr/local/php5/lib/php/ run echo "zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so" >> /usr/local/php5/php.ini run echo "zend_loader.enable=1" >> /usr/local/php5/php.ini workdir /usr/local/php5/etc run cp php-fpm.conf.default php-fpm.conf run echo "user = nginx" >> php-fpm.conf run echo "group = nginx" >> php-fpm.conf run ln -s /usr/local/php5/bin/* /usr/local/bin run ln -s /usr/local/php5/sbin/* /usr/local/sbin/ #修改nginx的配置文件 run sed -i '65,71s/#//g' /usr/local/nginx/conf/nginx.conf run sed -i '69s/fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;/fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;/g' /usr/local/nginx/conf/nginx.conf #复制本地的MySQL和PHP的测试网页到镜像 add test.php /usr/local/nginx/html/test.php add index.php /usr/local/nginx/html/index.php add run.sh /run.sh expose 9000 cmd ["/run.sh"]
(三)编写测试网页和启动脚本
vim run.sh #!/bin/bash /usr/local/php5/sbin/php-fpm /usr/local/nginx/sbin/nginx /usr/local/mysql/bin/mysqld_safe
vim index.php <?php $link=mysql_connect('localhost','root',''); if($link) echo " success"; mysql_close(); ?>
vim test.php <?php phpinfo(); ?>
(四)生成镜像
docker build -t centos6:lnmp /root/dockerfile/LNMP/
查看镜像
docker images | grep centos6
二、dockerfile指令指令作用FROM 镜像指定新镜像所基于的镜像,创建新镜像时在指定的镜像上进行创建,第一条指令必须是FROM指令MAINTAINER 名字说明新镜像维护人RUN 命令在所基于的镜像中执行的命令CMD 命令或脚本指定新镜像创建的容器在启动时要运行的命令或脚本EXPOSE 端口指定新镜像创建的容器要开启的端口ENV 环境变量 变量值设置一个环境变量COPY 源文件 目标文件将宿主机文件复制到镜像中,源文件必须和dockerfile在同一目录ADD 源文件 目标文件与COPY用法大致相同VOLUME 目录在容器中创建一个挂载点WORKDIR 路径为RUN、CMD等指定工作目录ONBUILD 命令指定所生成的镜像作为一个基础镜像时要运行的命令
————————————————版权声明:本文为CSDN博主「ⅨⅦ」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/sixisixsix/article/details/104840046