我尝试从容器内访问 Minio S3-API 端点,但我的应用程序无法解析容器名称。
问题是我正在使用的框架,使用@smithy/middleware-endpoint API,这需要完全限定的 URL。当我的 MinIO 实例与堆栈的其余部分一起启动时,端点会在启动时使用容器主机名传递到我的应用程序中; http://minio:9000 不起作用。
但是,我无法在 docker-compose 文件中对 IP 进行硬编码,因为每次启动 docker-compose 时它都会发生变化。
我的 docker-compose 文件看起来像这样:
version: '3.9'
services:
backend:
build: ./backend/tus/.
container_name: backend
ports:
- "8181:8181"
environment:
- MINIO_ENDPOINT=http://minio:9000
depends_on:
- minio
# the minio instance that the back end streams the data uploads to
minio:
image: quay.io/minio/minio
container_name: minio
command: server /data --console-address ":9001"
environment:
- MINIO_ROOT_USER=minioadmin
- MINIO_ROOT_PASSWORD=minioadmin
volumes:
- minio_data:/data
ports:
- "9000:9000"
- "9001:9001"
使用环境变量
MINIO_ENDPOINT
的代码看起来像这样:
/* Set up the s3 store to pass to the tus server */
const datafileS3Store = new S3Store({
s3ClientConfig: {
bucket: process.env.DATAFILE_BUCKET,
region: "eu-west-2",
endpoint: process.env.MINIO_ENDPOINT,
credentials: {
accessKeyId: process.env.MINIO_USERNAME,
secretAccessKey: process.env.MINIO_PASSWORD,
},
},
})
但是 MinIO 在启动时输出其 IP 地址,通常类似于:
那么,有谁知道我如何获取(甚至设置?)MinIO 实例的 S3-API IP 并将其传递到我的后端容器中?
解决方案是创建一个新网络并对容器 IP 地址进行硬编码,以便可以将 IP 作为环境变量传递到后端。
撰写文件:
version: '3.9'
services:
backend:
build: ./backend/tus/.
container_name: backend
ports:
- "8181:8181"
environment:
- MINIO_ENDPOINT=http://172.20.0.10:9000
- MINIO_USERNAME=minioadmin
- MINIO_PASSWORD=minioadmin
depends_on:
- minio
networks:
customnetwork:
ipv4_address: 172.20.0.11
# the minio instance that the back end streams the data uploads to
minio:
image: quay.io/minio/minio
container_name: minio
command: server /data --console-address ":9001"
environment:
- MINIO_ROOT_USER=minioadmin
- MINIO_ROOT_PASSWORD=minioadmin
volumes:
- minio_data:/data
ports:
- "9000:9000"
- "9001:9001"
networks:
customnetwork:
ipv4_address: 172.20.0.10
# script that inits minio with some buckets
createbuckets:
image: minio/mc
depends_on:
- minio
entrypoint: >
/bin/sh -c "
/usr/bin/mc alias set minio http://172.20.0.10:9000 minioadmin minioadmin;
/usr/bin/mc mb minio/datafiles;
/usr/bin/mc policy set public minio/datafiles;
exit 0;
"
networks:
customnetwork:
ipv4_address: 172.20.0.13
volumes:
minio_data:
networks:
customnetwork:
ipam:
config:
- subnet: 172.20.0.0/16
这里的关键是所有其他容器都需要添加到网络中:
networks:
- customnetwork
您的解决方案是创建自定义网络并向容器分配静态 IP 地址。这确实允许 MinIO 容器有一个可预测的 IP 地址,可以将其硬编码为后端容器中的环境变量。
+-----------------------------------+
| Docker Host |
| |
| +-------------+ customnetwork |
| | Backend |-----+ |
| | 172.20.0.11 | | |
| +-------------+ | |
| | |
| +-------------+ | |
| | MinIO |-----+ |
| | 172.20.0.10 | |
| +-------------+ |
| |
| +-----------------+ |
| | createbuckets | |
| | 172.20.0.13 | |
| +-----------------+ |
| |
+-----------------------------------+
每个容器都在
customnetwork
内分配有一个静态 IP 地址。后端容器可以使用硬编码的 IP 地址直接与 MinIO 容器通信172.20.0.10
。
sameersbn/docker-bind
),以便声明和解析 minio 的 FQDN。+-------------------------------------------------------------+
| Docker Host |
| |
| +------------------+ +---------------------+ |
| | MinIO | | BIND9 | |
| | (Service 1) |<--->| (Service 2) | |
| +------------------+ +---------------------+ |
| | | |
| | Interaction | DNS Resolution |
| v v |
| +-------------------+ minio.example.com |
| | Backend/tus | |
| | (Service 3) | |
| +-------------------+ |
| |
+-------------------------------------------------------------+
minio.example.com
,该服务将其解析为 Docker 网络内 MinIO 的 IP 地址。docker-compose.yml(v3)将是:
version: '3.9'
services:
backend:
build: ./backend/tus/.
container_name: backend
ports:
- "8181:8181"
environment:
- MINIO_ENDPOINT=http://minio.example.com:9000
- MINIO_USERNAME=minioadmin
- MINIO_PASSWORD=minioadmin
depends_on:
- bind9
minio:
image: quay.io/minio/minio
container_name: minio
command: server /data --console-address ":9001"
environment:
- MINIO_ROOT_USER=minioadmin
- MINIO_ROOT_PASSWORD=minioadmin
volumes:
- minio_data:/data
networks:
- customnetwork
ports:
- "9000:9000"
- "9001:9001"
bind9:
image: sameersbn/bind
volumes:
- ./bind:/etc/bind
networks:
- customnetwork
depends_on:
- minio
entrypoint: /bin/bash -c "sleep 30; nslookup minio > /etc/bind/minio_ip.txt; ip=$(cat /etc/bind/minio_ip.txt | grep 'Address: ' | cut -d ' ' -f 2); echo -e '@ IN SOA ns.example.com. admin.example.com. ( 2023100901 604800 86400 2419200 604800 )\n IN NS ns.example.com.\nminio.example.com IN A '$ip > /etc/bind/zones/db.example.com; named -g"
createbuckets:
image: minio/mc
depends_on:
- minio
entrypoint: >
/bin/sh -c "
/usr/bin/mc alias set minio http://minio.example.com:9000 minioadmin minioadmin;
/usr/bin/mc mb minio/datafiles;
/usr/bin/mc policy set public minio/datafiles;
exit 0;
"
networks:
- customnetwork
networks:
customnetwork:
ipam:
config:
- subnet: 172.20.0.0/16
volumes:
minio_data:
bind: