curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
查看版本
docker-compose --version
(2)Dockerfile编写
创建compose_lnmp目录
结构如下
编辑 php/Dockerfile
由于官方php-fpm镜像缺少一些扩展,所以要先用dockerfile构建新的镜像并注意配置时区和mysql扩展
FROM php:7.2-fpm RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone ADD sources.list /etc/apt/sources.list RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-install -j$(nproc) iconv && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install -j$(nproc) gd && docker-php-ext-install bcmath zip opcache iconv pdo pdo_mysql mysqli
nginx配置default.conf
server { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx/log/host.access.log main; location / { root /www/web; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root /www/web; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/web/$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} }
编辑docker-compose.yml
#版本号 version: "3" #服务 services: #mysql服务 db: #服务器镜像 build: ./mysql # image: mysql:latest #磁盘映射。./db_data 是宿主的目录。后者是容器里的目录。 volumes: - ./db_data:/var/lib/mysql - ./log/mysql:/var/log/mysql restart: always # 端口映射。前者是宿主的。后者是容器里端口 ports: - "3306:3306" #环境变量 environment: #mysql的root密码 MYSQL_ROOT_PASSWORD: 123456 #容器会创建的数据库 MYSQL_DATABASE: testdb #test用户 MYSQL_USER: test #test用户的密码 MYSQL_PASSWORD: 123456 #php-fpm服务 php: volumes: - ./web:/www/web - ./log/php-fpm:/var/log/php-fpm - ./log/php:/var/log/php #依赖的服务。会在容器里的host文件里添加 一条记录。访问db,即可访问mysql的服务。 depends_on: - db #服务器镜像构建的目录。会在该目录下寻找dockerfile构建镜像。 build: ./php-fpm restart: always #nginx服务 nginx: depends_on: - php volumes: - ./web:/www/web - ./log/nginx:/var/log/nginx build: ./nginx ports: - "80:80" restart: always
(3)使用Compose实现多容器运行机制
启动服务
cd compose_lnmp
docker-compose up -d # 启动
docker-compose restart # 重启
(4)服务测试
testdb数据库中插入数据
nginx测试
php+nginx测试
php+sql测试
问题:
解决方法:
mysql -u root -p use mysql;#开启root远程访问权限 grant all on *.* to 'root'@'%';#修改加密规则 alter user 'root'@'localhost' identified by '123456' password expire never;#更新密码 alter user 'root'@'%' identified with mysql_native_password by '123456';#刷新权限 flush privileges;
数据库创建:
插入表:
插入数据:
通过phpmyadmin查看:
(5)phpmyadmin
在docker-compose.yml添加以下内容并重构docker-compose
phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin privileged: true links: - lnmp-master_db_1 ports: - 8080:80 environment: PMA_HOST: db
访问8080端口查看数据库:
(6)实验报告
花费时间:20h
感想: