0%
March 29, 2024

Dev Container

docker

Why

  • By looking at the Dockerfile clearly we wouldn't want to install those dependencies at the developer's machine because some may use windows, some may use mac.

  • By using dev container, we can unify the dev environment.

Project Structure

devcontainer.json

{
    "name": "For Kafka",
    "build": {
        "dockerfile": "Dockerfile"
    }
}

Dockerfile

FROM node:20-alpine

RUN apk --no-cache add \
      bash \
      g++ \
      ca-certificates \
      lz4-dev \
      musl-dev \
      cyrus-sasl-dev \
      openssl-dev \
      make \
      python3

RUN apk add --no-cache --virtual .build-deps gcc zlib-dev libc-dev bsd-compat-headers py-setuptools bash
  • Note that we don't write RUN npm install <package-name> inside the Dockerfile because by default we are at the root level inside the container where we are not allowed to WRITE at that level.

  • Even we mkdir -p and WORKDIR at a directory and successfully npm install, but inside container we are at the workspace defined by the vscode extention Remote Development rather than the directory specified by WORKDIR.

  • The correct procedure should be to install just the native linux libraries, and then we install runtime-specific library inside the container.

  • Note that the node_modules installed inside the container will be linux-specific, running it in windows will fail.

Open the Project Inside dev Container

  1. Click the lower-left button in vs-code:

  2. Choose Reopen in Container

  3. Choose the configuration we have just named: