Search code examples
dockerdocker-composevscode-remotevscode-devcontainer

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:

.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}

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?


Solution

  • 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.

    So something like

    ports:
      - 127.0.0.1:5432:5432
    

    might work