Docker Componse 是容器编排技术
curl -L https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-`uname -s |tr A-Z a-z`-`uname -m` > /usr/local/bin/docker-compose
加速:
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.5.0/docker-compose-`uname -s |tr A-Z a-z`-`uname -m` > /usr/local/bin/docker-compose
命令解释
自动获取系统名和版本:uname -s |tr A-Z a-z (大写转小写),uname -m
如果github太慢,也可以手动去下载对应的包,然后拖到服务器上:
https://github.com/docker/compose/releases
找到对应版本:
本文下载的是:docker-compose-linux-x86_64
上传至服务器,将其移动到/usr/local/bin,并更名docker-compose,命令如下:
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
验证docker-compose
docker-compose -v
打印如下:
Docker Compose version v2.5.0
创建挂载文件
mkdir /mnt/dockerdata
创建mysql挂载文件
mkdir -p /mnt/dockerdata/mysql/{conf,data}
最终目录结构如下
├── docker-compose.yml
└── mysql
├── conf
│ └── my.cnf
├── config
└── data
创建my.cnf并编辑
vim /mnt/dockerdata/mysql/conf/my.cnf
内容如下:
[mysqld]
character-set-server=utf8mb4
default-time-zone='+8:00'
innodb_rollback_on_timeout='ON'
max_connections=500
innodb_lock_wait_timeout=500
创建docker-compose.yml并编辑
vim /mnt/dockerdata/docker-compose.yml
内容如下:
version: '3'
services:
mysql-db:
container_name: mysql01
# 指定镜像和版本
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
# 挂载数据目录
- "/mnt/dockerdata/mysql/data:/var/lib/mysql"
# 挂载配置文件目录
- "/mnt/dockerdata/mysql/config:/etc/mysql/conf.d"
docker-compose up
mysqld: Can’t read dir of ‘/etc/mysql/conf.d/’ (Errcode: 13 - Permission denied)
临时关闭
setenforce 0
永久关闭
vim /etc/selinux/config
SELINUX=disabled
改完之后重启docker
后台启动:
docker-compose up -d
启动指定容器:
docker-compose up -d 容器名
停止所有:
docker-compose stop
停止指定容器:
docker-compose stop 容器名
创建nginx挂载文件
mkdir -p /mnt/dockerdata/nginx/{conf,html,logs}
创建nginx配置文件如下:
vim /mnt/dockerdata/nginx/conf/nginx.conf
nginx 默认配置如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
创建docker-compose.yml并编辑
vim /mnt/dockerdata/docker-compose.yml
内容如下:
version: '3'
services:
nginx:
container_name: nginx01
image: nginx
ports:
- 80:80
volumes:
# nginx 配置文件映射
- /mnt/dockerdata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
# nginx log文件映射
- /mnt/dockerdata/nginx/log:/var/log/nginx
# nginx 静态文件映射
- /mnt/dockerdata/nginx/html:/etc/nginx/html
# 重启docker自动重启
restart: always