2

My project requires three containers/services:

  • web
  • api
  • db

The web and api should both be built from their respective Dockerfiles:

api/Dockerfile
web/Dockerfile

The api and web directories contain the source code for the respective services.

How can I setup VSCode so I build both the web and the api containers, choosing which service to open remotely via the devcontainer.json file?

My devcontainer config looks like this:

# .devcontainer/docker-compose.yml

services:
  web:
    build:
      context: ../web
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    links:
      - api

  api:
    build:
      context: ../api
      dockerfile: Dockerfile
    ports:
      - 3001:3001
    links:
      - db

  db:
    image: postgres
    restart: unless-stopped
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: pass
      POSTGRES_USER: user
      POSTGRES_DB: data
# .devcontainer/devcontainer.json

{
    "name": "App",
    "dockerComposeFile": "docker-compose.yml",
    "service": "web",
    "workspaceMount": "source=${localWorkspaceFolder}/web,target=/workspace,type=bind,consistency=delegated",
    "workspaceFolder": "/workspace"
}

1 Answer 1

2

I'm no expert in this field, but I would guess that you're mixing approaches here.

Alt1 (The one you're asking for)

  • Build devcontainer based on a single service
  • Using the build property
  • Pointing to a specific dockerfile

Alt2 (The one you have done)

  • Build devcontainer based on an orchestration of multiple services
  • Using the dockerComposeFile property
  • Pointing to a docker-compose file

List of properties: https://code.visualstudio.com/docs/remote/devcontainerjson-reference#_devcontainerjson-properties

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.