白开水 2020-05-03
1.Dockerfile格式
2.Dockerfile指令
3.Build镜像
4.构建Nginx,PHP,Tomcat基础镜像
5.快速搭建LNMP网站平台
[ ~]# docker history nginx:1.15 IMAGE CREATED CREATED BY SIZE COMMENT 53f3fd8007f7 12 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B #cmd命令 <missing> 12 months ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 12 months ago /bin/sh -c #(nop) EXPOSE 80 0B #EXPOSE 暴露端口 <missing> 12 months ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B <missing> 12 months ago /bin/sh -c set -x && apt-get update && apt… 54.1MB <missing> 12 months ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.12.0… 0B #ENV 环境变量 <missing> 12 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.12… 0B <missing> 12 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 12 months ago /bin/sh -c #(nop) CMD ["bash"] 0B #cmd命令 <missing> 12 months ago /bin/sh -c #(nop) ADD file:fcb9328ea4c115670… 55.3MB [ ~]#
docker build -t 新构建镜像名称 -f 指定Dockerfile文件位置 其他配置文件路径 Usage: docker build [OPTIONS] PATH | URL | -[flags]Options: -t, --tag list # 镜像名称 -f, --file string # 指定Dockerfile文件位置 # docker build . # docker build -t shykes/myapp . # docker build -t shykes/myapp -f /path/Dockerfile /path # docker build -t shykes/myapp http://www.example.com/Dockerfile
[ ~]# vim dockerfile FROM centos:7 MAINTAINER mxxl RUN yum install httpd -y COPY index.html /var/www/html ENV HTTP_V=2.0 EXPOSE 80 WORKDIR /tmp CMD ["httpd","-D","FOREGROUND"] [ ~]# [ ~]# vim index.html haha [ ~]# docker build -t http:v2 . REPOSITORY TAG IMAGE ID CREATED SIZE http v2 d45266d7ce19 4 minutes ago 298MB [ ~]# docker inspect b3217f3dec25 |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", [ ~]# [ ~]# curl 172.17.0.2 haha [ ~]#
4、构建业务基础镜像
•构建Nginx基础镜像
•构建PHP基础镜像
•构建Tomcat基础镜像
快速部署LNMP网站平台
1、自定义网络
docker network create lnmp
2、创建Mysql容器
docker run -d \
--name lnmp_mysql \
--net lnmp \
--mount src=mysql-vol,dst=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8
3、创建PHP容器
docker run -d --name lnmp_php --net lnmp --mount src=wwwroot,dst=/wwwroot php:v1
4、创建Nginx容器
docker run -d --name lnmp_nginx --net lnmp -p 88:80 \
--mount type=bind,src=$(pwd)/nginx.conf,dst=/usr/local/nginx/conf/nginx.conf --mount src=wwwroot,dst=/wwwroot nginx:v1
5、以wordpress博客为例
https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
1、自定义网络
[ ~]# docker network create lnmp
9cdfea34f9cd55af4fca8e2bf3be6ca92cab1ca2632a5ec1178173420d52ccaf
[ ~]#
[ ~]# ls
anaconda-ks.cfg dockerfile wordpress-4.9.4-zh_CN.tar.gz
[ ~]# cd dockerfile/
[ dockerfile]# ls
Dockerfile-nginx Dockerfile-php Dockerfile-tomcat nginx.conf php-fpm.conf php.ini
2、创建Mysql容器
[ dockerfile]#
docker run -d \
--name lnmp_mysql \
--net lnmp \
--mount src=mysql-vol,dst=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8
3、创建PHP容器
[ dockerfile]# cat Dockerfile-php #php镜像构造的dockerfile
FROM centos:7
MAINTAINER mxxl
RUN yum install epel-release -y && \
yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel autoconf \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \
tar zxf php-5.6.36.tar.gz && \
cd php-5.6.36 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --enable-opcache \
--with-mysql --with-mysqli --with-pdo-mysql \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-mbstring --with-mcrypt --enable-hash && \
make -j 4 && make install && \
cp php.ini-production /usr/local/php/etc/php.ini && \
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
mkdir /usr/local/php/log && \
cd / && rm -rf php* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]
[ dockerfile]#
[ dockerfile]# docker build -t php:v1 -f Dockerfile-php .
[ dockerfile]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
php v1 2a2ced00c520 8 months ago 522MB
mysql 5.7 e1e1680ac726 8 months ago 373MB
centos 7 9f38484d220f 13 months ago 202MB
[ dockerfile]#
[ dockerfile]# docker run -d --name lnmp_php --net lnmp --mount src=wwwroot,dst=/wwwroot php:v1
daf78c56eef3ca34d6e1ae6f7078b1332d85e70c9f43abe29639910c72e5a3e9
[ dockerfile]#
[ dockerfile]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
daf78c56eef3 php:v1 "php-fpm" 17 seconds ago Up 15 seconds 9000/tcp lnmp_php
[ dockerfile]#
4、创建Nginx容器
[ dockerfile]# cat Dockerfile-nginx #nginx镜像构造的dockerfile
FROM centos:7
MAINTAINER mxxl
RUN yum install -y gcc gcc-c++ make \
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
tar zxf nginx-1.15.5.tar.gz && \
cd nginx-1.15.5 &&\
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/* && \
echo "ok" >> /usr/local/nginx/html/status.html && \
cd / && rm -rf nginx-1.15.5* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
[ dockerfile]#
[ dockerfile]# docker build -t nginx:v1 -f Dockerfile-nginx .
[ dockerfile]# docker run -d --name lnmp_nginx --net lnmp -p 88:80 --mount src=wwwroot,dst=/wwwroot nginx:v1
2dee39b481b205b8270624fe11e66008d6b68141c39f7f0f3af3612bd16e2716
[ dockerfile]#
5、以wordpress博客为例
https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[ dockerfile]# tar -xf wordpress-4.9.4-zh_CN.tar.gz
[ dockerfile]# ls
[ dockerfile]# mv wordpress /var/lib/docker/volumes/wwwroot/_data/
[ dockerfile]#
[ mnt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2dee39b481b2 nginx:v1 "nginx -g ‘daemon of…" 4 minutes ago Up 3 minutes 0.0.0.0:88->80/tcp lnmp_nginx
daf78c56eef3 php:v1 "php-fpm" 9 minutes ago Up 9 minutes 9000/tcp lnmp_php
54d19fe86053 mysql:5.7 "docker-entrypoint.s…" 21 minutes ago Up 21 minutes 3306/tcp, 33060/tcp lnmp_mysql
[ mnt]#