docker/docker-compose.md
2025-04-04 17:31:23 +08:00

154 lines
3.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2><center>Docker-compose</center></h2>
------
## 一Docker-compose 概述
Compose 是一个用于定义和运行多容器 Docker 应用程序的工具。使用 Compose您可以使用 YAML 文件来配置应用程序的服务。然后,使用一个命令,您可以从您的配置中创建并启动所有服务。
Compose 适用于所有环境:生产、登台、开发、测试以及 CI 工作流程。
**使用 Compose 基本上是一个三步过程:**
- 使用定义您的应用程序的环境,`Dockerfile`以便可以在任何地方复制它
- 定义构成您的应用程序的服务,`docker-compose.yml` 以便它们可以在隔离环境中一起运行
- 运行`docker compose up`Docker compose 命令启动并运行您的整个应用程序
## 二Docker-compose 安装
### 1. 下载
```bash
[root@docker ~]# curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
```
### 2. 创建软连接
```bash
[root@docker ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
```
### 3. 添加执行权限
```bash
[root@docker ~]# chmod +x /usr/bin/docker-compose
```
### 4. 查看版本
```bash
[root@docker ~]# docker-compose --version
Docker Compose version v2.6.0
```
## 三:使用
### 1. 创建项目目录结构
```bash
[root@docker ~]# mkdir nginx
[root@docker ~]# cd nginx
[root@docker nginx]# mkdir conf html
```
### 2. 编写`Nginx`配置文件
```bash
[root@docker nginx]# vim conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
```
### 3. 创建静态文件
`html/index.html`中写入测试内容:
```bash
[root@docker nginx]# vim html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Nginx in Docker</title>
</head>
<body>
<h1>Hello from Docker + Nginx!</h1>
</body>
</html>
```
### 4. 编写`docker-compose.yml`
```bash
[root@docker nginx]# vim docker-compose.yml
version: '3.8'
services:
nginx:
image: nginx:latest
container_name: nginx-v1
ports:
- "80:80"
volumes:
- ./conf/nginx.conf:/etc/nginx/conf.d/default.conf
- ./html:/usr/share/nginx/html
restart: unless-stopped
参数说明:
ports: 映射宿主机 80 端口到容器的 80 端口
restart: 容器意外退出时自动重启。
```
### 5. 启动服务
```bash
# 启动容器(后台运行)
[root@docker nginx]# docker-compose up -d
[+] Running 2/2
⠿ Network nginx_default Created 0.1s
⠿ Container nginx-v1 Started 0.3s
# 查看运行状态
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f289f37ccc44 nginx:latest "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-v1
# 查看日志
[root@docker nginx]# docker-compose logs nginx
```
### 6. 验证
```bash
[root@docker nginx]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Nginx in Docker</title>
</head>
<body>
<h1>Hello from Docker + Nginx!</h1>
</body>
</html>
```
### 7. 其他命令
```bash
[root@docker nginx]# docker-compose up -d //后台运行
[root@docker nginx]# docker-compose ps //列出容器
[root@docker nginx]# docker-compose pause //暂停服务
[root@docker nginx]# docker-compose unpause //取消暂停服务
[root@docker nginx]# docker-compose down //停止并移除容器、网络
[root@docker nginx]# docker-compose logs //查看容器的输出
[root@docker nginx]# docker-compose start //启动服务
[root@docker nginx]# docker-compose stop //停止服务
```