3.3 配置mysql
#进入mysql子目录 [root@yuji ~]# cd /opt/compose_lnmp/mysql/ #将mysql包上传到此目录 [root@yuji mysql]# ls mysql-boost-5.7.20.tar.gz #编写mysql的Dockerfile文件 [root@yuji mysql]# vim Dockerfile FROM centos:7 MAINTAINER this is mysql image <zhi> RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make RUN useradd -M -s /sbin/nologin mysql 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 -j8 && make install RUN chown -R mysql:mysql /usr/local/mysql/ ADD my.cnf /etc/ RUN chown mysql:mysql /etc/my.cnf ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH WORKDIR /usr/local/mysql/ RUN bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ EXPOSE 3306 CMD /usr/local/mysql/bin/mysqld 复制代码
myslq主配置文件my.cnf
#配置myslq主配置文件my.cnf(用来复制到容器) [root@yuji mysql]# vim my.cnf [client] port = 3306 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 socket = /usr/local/mysql/mysql.sock [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 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 复制代码
3.4 配置php
[root@yuji compose_lnmp]# cd /opt/compose_lnmp/php/ #将php包上传 [root@yuji php]# ls php-7.1.10.tar.bz2 #编写php的Dockerfile [root@yuji php]# vim Dockerfile FROM centos:7 MAINTAINER this is php image <yuji> 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 RUN \cp /usr/local/src/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini \ && sed -i 's#mysqli.default_socket =#mysqli.default_socket = /usr/local/mysql/mysql.sock#' /usr/local/php/lib/php.ini \ && sed -i '939 s#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/lib/php.ini RUN \cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \ && sed -i '17 s/^;//' /usr/local/php/etc/php-fpm.conf RUN \cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \ && sed -i 's#user = nobody#user = nginx#' /usr/local/php/etc/php-fpm.d/www.conf \ && sed -i 's#group = nobody#group = nginx#' /usr/local/php/etc/php-fpm.d/www.conf \ && sed -i 's#listen = 127.0.0.1:9000#listen = 172.18.0.30:9000#' /usr/local/php/etc/php-fpm.d/www.conf #也可以提前准备好php.ini、php-fpm.conf、www.conf这三个文件,放入php目录下,之后使用ADD指令复制到镜像中 EXPOSE 9000 ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ] 复制代码
3.5 编写docker-compose.yml
#先拉取镜像centos:7 [root@yuji compose_lnmp]# docker pull centos:7 [root@yuji compose_lnmp]# vim /opt/compose_lnmp/docker-compose.yml #使用版本2(3版本不支持指令volumes_from) version: '2' #使用services定义服务 services: #配置nginx服务 nginx: #设置容器名 container_name: nginx #设置主机名为nginx hostname: nginx #使用dockerfile创建镜像。Dockerfile文件在当前目录的nginx目录下,文件名为Dockerfile build: #指定Dockerfile文件所在位置 context: ./nginx dockerfile: Dockerfile #映射端口 ports: - 1315:80 - 1316:443 #加入到lnmp网络中,使用ip172.18.0.0.10 networks: lnmp: ipv4_address: 172.18.0.10 #将当前目录下的wwwroot目录挂载到容器的/usr/local/nginx/html目录 volumes: - ./wwwroot/:/usr/local/nginx/html #配置服务mysql mysql: container_name: mysql hostname: mysql build: context: ./mysql dockerfile: Dockerfile ports: - 3306:3306 networks: lnmp: ipv4_address: 172.18.0.20 #设置/usr/local/mysql目录为数据卷 volumes: - /usr/local/mysql #配置服务php php: hostname: php build: context: ./php dockerfile: Dockerfile container_name: php ports: - 9000:9000 networks: lnmp: ipv4_address: 172.18.0.30 #从nginx容器和mysql容器获取数据卷 volumes_from: - nginx - mysql #php容器需要在nginx和mysql之后启动 depends_on: - nginx - mysql #php和容器nginx,容器mysql连接 links: - nginx - mysql #设置网络为自定义网络 #配置网络模式和网络名 networks: #设置网络名lnmp lnmp: #网络模式为bridge桥接模式 driver: bridge ipam: config: #使用的网段为172.18.0.0/16 - subnet: 172.18.0.0/16 复制代码
运行docker-compose:
#在工作目录使用此命令。 #-f: --file-name, 指定模板文件。默认为docker-compose.yml #-p: --project-name NAME ,指定项目名称,默认使用目录名 #-d: 在后台运行 [root@yuji compose_lnmp]# docker-compose -f docker-compose.yml up -d #在工作目录,使用 docker-compose ps 可以看到启动的容器状态 [root@yuji compose_lnmp]# docker-compose ps 复制代码
进入mysql容器,进行用户授权:
#进入mysql容器,进行用户授权。 [root@yuji mysql]# docker exec -it mysql /bin/bash [root@mysql mysql]# mysql mysql> create database wordpress; mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123'; mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123'; mysql> flush privileges; 复制代码
3.6 浏览器访问测试
http://192.168.41.46:1315/wordpress/index.php #在yaml文件中nginx端口映射为1315 http://192.168.41.46:1315/ #直接访问,可以看见创建的测试网页