Docker Compose
1034字约3分钟
2024-08-11
Docker
开源项目,负责实现对 Docker
容器集群的快速编排
安装 docker compose
先安装并启动 docker
- 方式一:命令行安装
# 添加 epel 源
yum install -y epel-release
# 安装docker-compose
yum install -y docker-compose
# 查看 docker-compose 版本,需要注意的是 2023 年 1 月起,官方取消了 docker-compose --version 这条查看版本的命令
docker compose version
或
docker-compose --version
# bash 命令补齐,如果失效可以换成ip的方式请求
curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
或
curl -L http://199.232.68.133/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
- 方式二:本地下载上传服务器
如果方式一下载不行,可以直接通过本站 下载
也可以访问官网 https://github.com/docker/compose/releases 下载
- 下载后上传至
/usr/local/bin
目录下,修改二进制包名
mv docker-compose-linux-x86_64 docker-compose
- 将可执行权限应用于二进制文件
sudo chmod +x /usr/local/bin/docker-compose
- 查看安装版本
docker-compose -v
或者
docker-compose --version
- bash 命令补齐
curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
# 如果失效可以换成ip的方式请求
curl -L http://199.232.68.133/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
- 卸载
docker-compose
,直接移除二进制包即可
sudo rm /usr/local/bin/docker-compose
常用命令
- 启动
docker-compose up -d
- 重启
docker-compose restart
- 关闭
docker-compose stop
安装 nginx
# 启动一个临时的 ngxin 容器
docker run --rm -d --name=tempnginx nginx
# 创建需要的目录
mkdir -p /usr/local/nginx/html
mkdir -p /usr/local/nginx/logs
# 创建 /nginx/conf 目录并进入
mkdir -p /usr/local/nginx/conf
cd /usr/loacl/nginx
# 拷贝nginx.conf配置文件
docker cp tempnginx:/etc/nginx/nginx.conf ./conf/
# 拷贝conf.d文件夹
docker cp tempnginx:/etc/nginx/conf.d/ ./conf/
# 停止临时容器
docker stop tempnginx
- 解释一下为啥复制
docker
里面文件的路径是/etc/nginx
,直接通过命令进入容器里面内部看文件目录
[root@VM-0-15-centos nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1f22c40873d nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp tempnginx
[root@VM-0-15-centos nginx]# docker exec -it d1f22c40873d /bin/bash
root@d1f22c40873d:/# ls -l
total 80
drwxr-xr-x 2 root root 4096 Oct 11 00:00 bin
drwxr-xr-x 2 root root 4096 Oct 3 09:00 boot
drwxr-xr-x 5 root root 340 Nov 6 02:38 dev
drwxr-xr-x 1 root root 4096 Oct 12 02:03 docker-entrypoint.d
-rwxrwxr-x 1 root root 1202 Oct 12 02:03 docker-entrypoint.sh
drwxr-xr-x 1 root root 4096 Nov 6 02:38 etc
drwxr-xr-x 2 root root 4096 Oct 3 09:00 home
drwxr-xr-x 1 root root 4096 Oct 12 02:03 lib
drwxr-xr-x 2 root root 4096 Oct 11 00:00 lib64
drwxr-xr-x 2 root root 4096 Oct 11 00:00 media
drwxr-xr-x 2 root root 4096 Oct 11 00:00 mnt
drwxr-xr-x 2 root root 4096 Oct 11 00:00 opt
dr-xr-xr-x 113 root root 0 Nov 6 02:38 proc
drwx------ 1 root root 4096 Nov 6 02:39 root
drwxr-xr-x 1 root root 4096 Nov 6 02:38 run
drwxr-xr-x 2 root root 4096 Oct 11 00:00 sbin
drwxr-xr-x 2 root root 4096 Oct 11 00:00 srv
dr-xr-xr-x 13 root root 0 Nov 6 02:38 sys
drwxrwxrwt 1 root root 4096 Oct 12 02:03 tmp
drwxr-xr-x 1 root root 4096 Oct 11 00:00 usr
drwxr-xr-x 1 root root 4096 Oct 11 00:00 var
root@d1f22c40873d:/# cd /etc/nginx/
root@d1f22c40873d:/etc/nginx# ls -l
total 32
drwxr-xr-x 1 root root 4096 Nov 6 02:38 conf.d
-rw-r--r-- 1 root root 1007 Sep 7 15:21 fastcgi_params
-rw-r--r-- 1 root root 5290 Sep 7 15:21 mime.types
lrwxrwxrwx 1 root root 22 Sep 7 15:38 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 648 Sep 7 15:38 nginx.conf
-rw-r--r-- 1 root root 636 Sep 7 15:21 scgi_params
-rw-r--r-- 1 root root 664 Sep 7 15:21 uwsgi_params
docker-compose.yaml
配置信息如下
version: '3'
services:
nginx:
# 容器名称
container_name: nginx
# 默认nginx:latest
image: nginx
# 自启动
restart: always
# 宿主机端口80:容器默认端口80
ports:
- 80:80
# 文件映射
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./conf/conf.d/:/etc/nginx/conf.d/
- ./html/:/usr/share/nginx/html/
- ./logs/:/var/log/nginx/
# 时区设置
environment:
- TZ=Asia/Shanghai
- 启动
nginx
,在docker-compose.yaml
的目录执行命令
docker-compose up -d
- 部署静态资源文件的
nginx
配置,需要在docker-compose.yaml
中增加静态资源挂载目录
server {
listen 80;
server_name localhost;
location / {
# 宿主机与容器挂载的静态资源目录
root /usr/local/blog-front;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
安装 Mysql8.0
version: '3.1'
services:
mysql8.0:
image: mysql:8.0.21
restart: always
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123456
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql