一、什么是dockerfile
docker是以镜像作为基础环境,而dockerfile就是创建镜像的一种方法。dockerfile相当于一个脚本,当docker运行这个脚本,就能创建项对应的镜像。dockerfile有严格的格式和单独的指令。
二、dockerfile指令
指令 | 作用 |
FROM 镜像 | 指定新镜像所基于的镜像,创建新镜像时在指定的镜像上进行创建,第一条指令必须是FROM指令 |
MAINTAINER 名字 | 说明新镜像维护人 |
RUN 命令 | 在所基于的镜像中执行的命令 |
CMD 命令或脚本 | 指定新镜像创建的容器在启动时要运行的命令或脚本 |
EXPOSE 端口 | 指定新镜像创建的容器要开启的端口 |
ENV 环境变量 变量值 | 设置一个环境变量 |
COPY 源文件 目标文件 | 将宿主机文件复制到镜像中,源文件必须和dockerfile在同一目录 |
ADD 源文件 目标文件 | 与COPY用法大致相同 |
VOLUME 目录 | 在容器中创建一个挂载点 |
WORKDIR 路径 | 为RUN、CMD等指定工作目录 |
ONBUILD 命令 | 指定所生成的镜像作为一个基础镜像时要运行的命令 |
三、构建镜像
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
(五)创建容器
docker run -d -it --name lnmp -p 8804:80 f12fafdc09b9
docker ps | grep lnmp
(六)打开浏览器测试
2.构建lamp镜像
(一)创建工作目录
mkdir -p /root/dockerfile/LAMP
(二)编写dockerfile
cd /root/dockerfile/LAMP
vim Dockerfile
from daocloud.io/centos:6
maintainer L
run yum -y install zlib-devel gcc gcc-c++ pcre-devel wget perl
run wget http://archive.apache.org/dist/httpd/httpd-2.2.17.tar.gz
run tar zxf httpd-2.2.17.tar.gz
workdir httpd-2.2.17
run ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
expose 80
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
workdir /
run wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
run tar zxf libmcrypt-2.5.8.tar.gz
workdir libmcrypt-2.5.8
run ./configure && make && make install
run ln -s /usr/local/lib/libmcrypt.* /usr/lib/
workdir /
run wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
run tar zxf mhash-0.9.9.9.tar.gz
workdir mhash-0.9.9.9
run ./configure && make && make install
run ln -s /usr/local/lib/libmhash* /usr/lib
workdir /
run wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
run tar zxf mcrypt-2.6.8.tar.gz
workdir mcrypt-2.6.8
ENV LD_LIBRARY_PATH /usr/local/lib:$LD_LIBRARY_PATH
run ./configure && make && make install
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-mcrypt --with-apxs2=/usr/local/httpd/bin/apxs --with-mysql=/usr/local/mysql/ --with-config-file-path=/usr/local/php5 --enable-mbstring && make && make install
run cp php.ini-development /usr/local/php5/php.ini
run sed -i '226s/short_open_tag = Off/short_open_tag = On/g' /usr/local/php5/php.ini
workdir /
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
run ln -s /usr/local/php5/bin/* /usr/local/bin
run ln -s /usr/local/php5/sbin/* /usr/local/sbin/
add test.php /usr/local/httpd/htdocs/test.php
add index.php /usr/local/httpd/htdocs/index.php
run echo "LoadModule php5_module modules/libphp5.so" >> /usr/local/httpd/conf/httpd.conf
run echo "AddType application/x-httpd-php .php" >> /usr/local/httpd/conf/httpd.conf
run echo "DirectoryIndex index.php index.html" >> /usr/local/httpd/conf/httpd.conf
add run.sh /run.sh
expose 9000
cmd ["/run.sh"]
(三)编写测试网页和启动脚本
vim index.php
<?php
$link=mysql_connect('localhost','root','');
if($link) echo " success";
mysql_close();
?>
vim test.php
<?php
phpinfo();
?>
vim run.sh
#!/bin/bash
/usr/local/httpd/bin/httpd
/usr/local/mysql/bin/mysqld_safe
/usr/local/php5/sbin/php-fpm
chmod 777 run.sh
(四)构建镜像
docker build -t centos6:lamp /root/dockerfile/LAMP/
查看镜像
docker images | grep lamp
(五)创建容器
docker run -d -it --name lamp -p 8005:80 f5a4fe8803ca
docker ps | grep lamp
(六)浏览器测试