docker-compose安装使用配置

一、mac安装

brew install docker-compose

二、docker-compose.yml文件配置

version: '3'

services:
memcache:
image: memcached:latest
ports:
- "127.0.0.1:11211:11211"
networks:
- lnmp
container_name: memcache15

mysql:
# build: ./mysql # 使用Dockerfile文件
image: mysql:latest
ports:
- "3306:3306" # 宿主机端口:容器端口
environment:
- MYSQL_ROOT_PASSWORD=asdfghjkl # 设置mysql的root密码
volumes:
- ~/MyServer/mysql/data:/var/lib/mysql:rw # mysql数据文件
networks:
- lnmp
container_name: mysql57 # 设置容器名字

redis:
image: redis:latest
ports:
- "127.0.0.1:6379:6379" # 如不需外网访问容器里面的服务 设置ip地址为127.0.0.1即可
environment:
- appendonly=yes # 打开redis密码设置
- requirepass=123456 # 设置redis密码
networks:
- lnmp
container_name: redis40


php:
# build: ./php
image: php:7.1-fpm
ports:
- "127.0.0.1:9000:9000"
volumes:
- ~/MyServer/myweb/test:/var/www/html:rw # web站点目录
- ~/MyServer/php/php.ini:/usr/local/etc/php/php.ini:ro
- ~/MyServer/php/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
- ~/MyServer/php/php-fpm.conf:/usr/local/etc/php-fpm.conf:ro
networks:
- lnmp
container_name: php72
tty: true
links:
- mysql
privileged: true

nginx:
# build: ./nginx
image: nginx:latest
ports:
- "8080:80" # 如果宿主机有安装nginx或者apache并且在运行则需要映射到其他端口
# - "8081:81" # 设置多个站点
# - "8082:82"
# - "8083:83"
depends_on:
- "php"
volumes:
- ~/MyServer/myweb/test:/var/www/html:rw
- ~/MyServer/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ~/MyServer/nginx/server.conf:/etc/nginx/conf.d/default.conf:ro
networks:
- lnmp
container_name: nginx114

networks: # 创建网络
lnmp:
driver: bridge

三、常用命令

docker-compose up 启动容器

docker-compose up -d 以后台服务形式启动容器

docker-compose exec mysql bash 进入容器(mysql为服务名,不是容器名)
 
四、注意问题
 
(1)mysql8问题 8.0.17
2059 – Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found
 
docker-compose exec mysql bash
 
#default_authentication_plugin=caching_sha2_password (comment line!) default_authentication_plugin=mysql_native_password (new line)
 
mysql -h localhost -u root -pasdfghjkl
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘asdfghjkl’;
ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘asdfghjkl’;
flush privileges;
 
 
(2)fastcgi_pass地址:这个是重点 fastcgi_pass php72:9000; 一般由 php-fpm容器名:9000,这样组成
 
(3)php连接docker
php中配置links: – mysql,php程序配置文件中host=mysql,mysql为服务名
‘dns’=>’mysql:dbname=test;port=3306;host=mysql;charset=utf8’,