docker-compose安装及使用
官网仓库地址:
https://github.com/docker/compose
1. 安装
如果按照之前文档中安装了docker,会默认安装docker-compose,将如下文件拷贝到系统path路径中:
$ sudo cp /usr/libexec/docker/cli-plugins/docker-compose /usr/local/bin/
# 配置环境变量
$ echo "export PATH=/usr/local/bin/:\$PATH" | sudo tee -a /etc/profile
$ source /etc/profile
$ docker-compose -v
Docker Compose version v2.26.1
也可以从官方仓库中下载二进制文件拷贝到/usr/local/bin路径,并添加可执行权限:
https://github.com/docker/compose/releases
2. 使用示例
使用 docker-compose
启动一个 Nginx 应用程序,你需要创建一个 docker-compose.yml
文件来定义 Nginx 服务,并为其配置端口映射和其他必要的设置。
以下是一个示例,展示了如何使用 docker-compose
启动一个简单的 Nginx 应用程序,并在主机上暴露端口:
1. 创建 docker-compose.yml
文件
首先,创建一个 docker-compose.yml
文件。在这个文件中,你可以定义 Nginx 服务以及如何启动它。以下是一个简单的配置示例:
version: '3.8'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
解释 docker-compose.yml
:
version
: 这是 Docker Compose 文件的版本。在这个示例中,使用了3.8
。services
: 这里定义了所有服务。我们创建了一个名为nginx
的服务。image
: 使用nginx:latest
Docker 镜像。这将拉取最新的 Nginx 镜像。ports
: 定义端口映射。在这里,我们将主机的 80 端口映射到容器内的 80 端口,这样主机上的流量可以转发到容器。
2. 使用 docker-compose
启动 Nginx
在包含 docker-compose.yml
的目录中,运行以下命令以启动 Nginx:
# docker-compose up
WARN[0000] /root/nginx-demo/docker-compose.yml: `version` is obsolete
[+] Running 8/8
✔ nginx Pulled
✔ b0a0cf830b12 Pull complete
✔ 8ddb1e6cdf34 Pull complete
✔ 5252b206aac2 Pull complete
✔ 988b92d96970 Pull complete
✔ 7102627a7a6e Pull complete
✔ 93295add984d Pull complete
✔ ebde0aa1d1aa Pull complete
[+] Running 1/2
✔ Network nginx-demo_default Created
⠴ Container nginx-demo-nginx-1 Created
Attaching to nginx-1
nginx-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-1 | 2024/04/27 03:51:05 [notice] 1#1: using the "epoll" event method
nginx-1 | 2024/04/27 03:51:05 [notice] 1#1: nginx/1.25.5
nginx-1 | 2024/04/27 03:51:05 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
nginx-1 | 2024/04/27 03:51:05 [notice] 1#1: OS: Linux 3.10.0-1160.114.2.el7.x86_64
这将使用 nginx:latest
镜像启动 Nginx 服务,并将主机上的 80 端口与容器内的 80 端口绑定。您可以通过浏览器访问 http://主机ip
查看 Nginx 欢迎页面,效果如下:
3. 创建一个简单的博客站点
如果你想要自定义 Nginx 配置,例如,添加静态网站的配置或设置反向代理,可以创建自定义的 nginx.conf
文件和前端代码文件,然后映射到 Nginx 容器对应路径。
首先,在docker-compose.yml同目录下创建 nginx.conf
,示例如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
创建www目录,用于存放前端页面文件。
在www目录中写一个博客网站的html页面文件,命名为index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Blog</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Welcome to My Blog</h1>
</header>
<main>
<div class="blog-post">
<h2>Understanding AI and Machine Learning</h2>
<p><strong>Author:</strong> Jane Doe</p>
<p><strong>Date:</strong> April 27, 2024</p>
<p>This blog post explores the basics of AI and Machine Learning, discussing the different types and applications...</p>
<a href="post1.html">Read More</a>
</div>
<div class="blog-post">
<h2>The Future of Web Development</h2>
<p><strong>Author:</strong> John Smith</p>
<p><strong>Date:</strong> April 25, 2024</p>
<p>This article examines the emerging trends in web development, focusing on technologies like React, Vue, and Svelte...</p>
<a href="post2.html">Read More</a>
</div>
</main>
<footer>
<p>© 2024 My Blog. All rights reserved.</p>
</footer>
</body>
</html>
为页面添加一些基本样式。创建一个名为 style.css 的文件,包含以下内容:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: white;
text-align: center;
padding: 20px;
}
main {
padding: 20px;
}
.blog-post {
border: 1px solid #ddd;
padding: 20px;
margin-bottom: 20px;
}
.blog-post h2 {
margin: 0;
}
.blog-post a {
text-decoration: none;
color: #0066cc;
}
.blog-post a:hover {
text-decoration: underline;
}
footer {
text-align: center;
padding: 10px;
background-color: #333;
color: white;
}
此时目录结构如下:
tree .
.
├── docker-compose.yml
├── nginx.conf
└── www
├── index.html
└── style.css
接下来,修改 docker-compose.yml
以将本地文件和目录挂载到容器,并映射到主机的8080端口:
version: '3.8'
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./www:/usr/share/nginx/html:ro
这样,docker-compose
会在启动时将 nginx.conf
挂载到容器内的 /etc/nginx/nginx.conf
,从而使用你定义的自定义配置。将主机的www目录挂账在容器内的/www:/usr/share/nginx/html
。
然后,再次运行 docker-compose up
,Nginx 应用会启动并使用你的自定义配置。效果如下:
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。