gpt4 book ai didi

docker - 如何处理VSCode Remote dev容器中的多个 `network_mode`?

转载 作者:行者123 更新时间:2023-12-05 05:52:57 30 4
gpt4 key购买 nike

我希望在开发容器中运行一个应用程序、数据库和 redis 服务,我可以在容器、应用程序和 Windows 上访问我的数据库和 redis,这就是我目前想要的对于我的应用程序和数据库:

.devcontainer.json:

{
"name": "Node.js, TypeScript, PostgreSQL & Redis",
"dockerComposeFile": "docker-compose.yml",
"service": "akira",
"workspaceFolder": "/workspace",
"settings": {
"typescript.tsdk": "node_modules/typescript/lib",
"sqltools.connections": [
{
"name": "Container database",
"driver": "PostgreSQL",
"previewLimit": 50,
"server": "database",
"port": 5432,
"database": "akira",
"username": "ailuropoda",
"password": "melanoleuca"
}
],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
},
"extensions": [
"aaron-bond.better-comments",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg",
"redhat.vscode-yaml"
],
"forwardPorts": [5432],
"postCreateCommand": "npm install",
"remoteUser": "node"
}

docker-compose.yml:

version: "3.8"

services:
akira:
build:
context: .
dockerfile: Dockerfile
command: sleep infinity
env_file: .env
volumes:
- ..:/workspace:cached

database:
image: postgres:latest
restart: unless-stopped
environment:
POSTGRES_USER: ailuropoda
POSTGRES_DB: akira
POSTGRES_PASSWORD: melanoleuca
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data

redis:
image: redis:alpine
tty: true
ports:
- 6379:6379

volumes:
pgdata:

Dockerfile:

ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}

如您所见,我已经尝试使用 networks 实现我想要的,但没有成功,我的问题是:如何将 Redis 添加到我的服务中,同时仍然能够连接 redis 和数据库在应用程序内部和 Windows 上?

最佳答案

将所有非开发容器切换到network_mode: service:akira

version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ../..:/workspace:cached
command: sleep infinity
postgresql:
image: postgres:14.1
network_mode: service:akira
restart: unless-stopped
volumes:
- ../docker/volumes/postgresql:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: pornapp
redis:
image: redis
network_mode: service:akira
restart: unless-stopped
volumes:
- ../docker/volumes/redis:/data

这似乎是原始配置: https://github.com/microsoft/vscode-dev-containers/pull/523

但它被恢复了,因为如果你在其他服务运行时重建开发容器,端口转发将会中断: https://github.com/microsoft/vscode-dev-containers/issues/537

关于docker - 如何处理VSCode Remote dev容器中的多个 `network_mode`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70003249/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com