[英]How to deal with more than one `network_mode` in a VSCode Remote dev container?
I would like to have an application, database and redis service running in a dev container where I'd be able to access my database and redis inside the container, application and on Windows, this is what currently works just as I wanted for my application and database:我想在开发容器中运行一个应用程序、数据库和 redis 服务,我可以在其中访问我的数据库和 redis 在容器、应用程序和 ZAEA23489CE3AA9B6406EBB28E0CDA43Z和数据库:
.devcontainer.json
: .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
: 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
: Dockerfile
:
ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}
As you can see I already tried to achieve what I wanted to using networks
but without success, my question is: How can I add Redis to my services while still being able to connect redis and database inside the application and on Windows?如您所见,我已经尝试实现我想要使用
networks
但没有成功,我的问题是:如何将 Redis 添加到我的服务中,同时仍然能够连接 redis 和应用程序内的数据库和 ZAEA23489CE3AA9B640Z3EBB28?
Delete all of the network_mode:
settings.删除所有
network_mode:
设置。 Compose will use the default network_mode: bridge
. Compose 将使用默认的
network_mode: bridge
。 You'll be able to communicate between containers using their Compose service names as host names, as described in Networking in Compose in the Docker documentation.您将能够使用它们的 Compose 服务名称作为主机名在容器之间进行通信,如 Docker 文档中的 Compose 中的网络中所述。
version: "3.8"
services:
akira:
build: .
env_file: .env
environment:
PGHOST: database
database:
image: postgres:latest
...
In SO questions I frequently see trying to use network_mode:
to make other things appear as localhost
.在 SO 问题中,我经常看到尝试使用
network_mode:
使其他内容显示为localhost
。 That host name is incredibly context-sensitive;该主机名具有令人难以置信的上下文相关性; if you asked my laptop, one of the Stack Overflow HTTP servers, your application container, or your database container who
localhost
is, they'd each independently say "well I am of course" but referring to a different network context.如果您问我的笔记本电脑、Stack Overflow HTTP 服务器之一、您的应用程序容器或
localhost
是您的数据库容器,他们会各自独立地说“我当然是”,但指的是不同的网络上下文。 network_mode: service:...
sounds like you're trying to make the other container be localhost
; network_mode: service:...
听起来您正在尝试使另一个容器成为localhost
; in practice it's extremely unusual to use this.在实践中,使用它是非常不寻常的。
You may need to change your application code to make settings like the database location configurable, depending on where they're running, and environment variables are an easy way to set this in Docker.您可能需要更改应用程序代码以使数据库位置等设置可配置,具体取决于它们运行的位置,并且环境变量是在 Docker 中设置此设置的简单方法。 For this particular example I've used the
$PGHOST
variable the standard PostgreSQL client libraries use;对于这个特定示例,我使用了标准 PostgreSQL 客户端库使用的
$PGHOST
变量; in a Typescript/Node context you may need to change your code to refer to process.env.SOME_HOSTNAME
instead of 'localhost'
.在 Typescript/Node 上下文中,您可能需要更改代码以引用
process.env.SOME_HOSTNAME
而不是'localhost'
。
If you're using Docker on WSL, I found that I can often not connect when the process is listening on::1, but when explicitly binding the port to 127.0.0.1 makes the service accessible through Windows.如果您在 WSL 上使用 Docker,我发现当进程正在侦听::1 时我经常无法连接,但是当显式将端口绑定到 127.0.0.1 时,可以通过 Windows 访问服务。
So something like所以像
ports:
- 127.0.0.1:5432:5432
might work可能有用
Switch all non-dev containers to network_mode: service:akira将所有非开发容器切换到 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
It seems this was the original configuration: https://github.com/microsoft/vscode-dev-containers/pull/523看来这是原始配置: https://github.com/microsoft/vscode-dev-containers/pull/523
But it was reverted back because if you rebuild the dev container while others servies are running, the port forwarding will break: https://github.com/microsoft/vscode-dev-containers/issues/537但它被恢复了,因为如果您在其他服务运行时重建开发容器,端口转发将中断: https://github.com/microsoft/vscode-dev-containers/issues/537
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.