第3次系统综合实践
(1)完成Docker-compose的安装
首先用了官方文档中的方法,但是实在是太慢啦
于是百度换了一种方法,安装成功
(2)Dockerfile编写
选择lnmp来搭建:
先来看整体最终的结构:
docker-compose.yml:
version: "3" services: nginx: build: ./nginx ports: - "81:8808" #暴露端口 volumes: - ./web:/www/web #磁盘映射,后者与default.conf中定义的一致 - ./nginx/default.conf:/etc/nginx/conf.d/default.conf phpfpm: build: ./phpfpm ports: - "9000:9000" volumes: - ./web:/www/web links: - "mysql" #mysql服务 mysql: image: mysql_image container_name: mysql_container build: ./mysql ports: - "3307:3307" volumes: - ./mysql/data:/var/lib/mysql
mysql:
FROM mysql:5.7 MAINTAINER czh ENV MYSQL_ALLOW_EMPTY_PASSWORD no ENV MYSQL_ROOT_PASSWORD=123456 #不允许空密码登陆,设置初始密码
php:
FROM php:7.0-fpm RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev libmemcached-dev zlib1g-dev libcurl4-openssl-dev libxml2-dev --no-install-recommends && rm -rf /var/lib/apt/lists/* && docker-php-ext-install -j$(nproc) iconv mcrypt gettext curl mysqli pdo pdo_mysql zip mbstring bcmath opcache xml simplexml sockets hash soap && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install -j$(nproc) gd CMD ["php-fpm", "-F"]
nginx:
FROM nginx MAINTAINER czh [email protected] EXPOSE 8808 #映射端口
(3)使用Compose实现多容器运行机制
接下来构建镜像:
在本地环境访问查看:
成功
(4)服务测试:
index.php:
<?php $servername = "mysql_container"; $username = "root"; $password = "123456"; try { $conn = new PDO("mysql:host=$servername;", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
查看浏览器: