Dockerfile镜像LNMP的实战
- 环境准备
- 关闭防火墙
- 拉取centos:7镜像
- 自定义网络
- 部署nginx(容器IP 为 172.18.0.10)
- 部署mysql(容器IP 为 172.18.0.20)
- 部署php(容器IP 为 172.18.0.30)
环境准备
关闭防火墙
[root@docker01 ~]# systemctl stop firewalld.service && setenforce 0
拉取centos:7镜像
[root@docker01 ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 19 months ago 204MB
自定义网络
[root@docker01 ~]# docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
12aa33506ac95c3ea18fc0624519546fd186688676eed08eed9012648c33a80f
部署nginx(容器IP 为 172.18.0.10)
[root@docker01 ~]# mkdir /opt/nginx
[root@docker01 ~]# cd /opt/nginx/
[root@docker01 nginx]# rz -E
rz waiting to receive.
[root@docker01 nginx]# ls
nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中
[root@docker01 nginx]# mkdir /opt/nginx/html
[root@docker01 nginx]# tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
[root@docker01 nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <nginx>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
[root@docker01 nginx]# docker build -t nginx:lnmp .
注意:如有报错重启docker服务
成功
[root@docker01 nginx]# docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
ef64f11dd6b21799db50705f0162d53d022c26ebcdf98355fb305bdf46bcebdd
[root@docker01 nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d5300ea4b42 nginx:lnmp "/usr/local/nginx/sb…" 35 seconds ago Up 34 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
部署mysql(容器IP 为 172.18.0.20)
[root@docker01 nginx]# mkdir /opt/mysqld
[root@docker01 nginx]# cd /opt/mysqld
[root@docker01 mysqld]# rz -E
rz waiting to receive.
[root@docker01 mysqld]# ls
my.cnf mysql-boost-5.7.20.tar.gz
## 上传 mysql-boost-5.7.20.tar.gz,my.cnf 到 /opt/mysqld 目录中
[root@docker01 mysqld]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <mysql>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
[root@docker01 mysqld]# vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@docker01 mysqld]# docker build -t mysql:lnmp .
注意:如有报错重启docker服务
成功
[root@docker01 mysqld]# docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
48ff4b0c4b8219b00b25357be6c8e919e462843fd469ed4555d88a1c0faf9c8f
[root@docker01 mysqld]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48ff4b0c4b82 mysql:lnmp "/usr/local/mysql/bi…" 38 seconds ago Up 28 seconds 3306/tcp mysql
9d5300ea4b42 nginx:lnmp "/usr/local/nginx/sb…" 3 hours ago Exited (0) 41 minutes ago nginx
部署php(容器IP 为 172.18.0.30)
[root@docker01 mysqld]# mkdir /opt/php
[root@docker01 mysqld]# cd /opt/php
[root@docker01 php]# rz -E
rz waiting to receive.
[root@docker01 php]# ls
php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
# 上传 php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf 到 /opt/php 目录中
[root@docker01 php]# vim Dockerfile
FROM centos:7
MAINTAINER this is php image <php>
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
[root@docker01 php]# docker build -t php:lnmp .
[root@docker01 php]# docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
9b350b8917fbc78bd8a8f506b24593f19652595dfee4bf736bf9495875fc8993
[root@docker01 php]# docker exec -it mysql /bin/bash
[root@48ff4b0c4b82 mysql-5.7.20]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
浏览器测试输入:http://20.0.0.100/wordpress/wp-admin/setup-config.php
成功:Dockerfile镜像LNMP搭建成功