DEV Community

inversemetric
inversemetric

Posted on

Same Dockerfile for Production and Development

You want your servers to run on docker and you want a consistent developer experience locally.

Great!

  1. In your Dockerfile, COPY your source files as normal.
  2. Create a docker-compose.yml file that mounts a volume from the host machine to the source directory (overriding the COPY from the dockerfile). Override the command inside docker-compose.yml as needed.

Build normally for production.
Use docker-compose up --build for development on your host machine.

Example:

Dockerfile

FROM node:alpine WORKDIR /api COPY package.json . COPY yarn.lock . RUN yarn install COPY . /api CMD ["node", "src/index.js"] 
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

version: '3.6' services: api: build: . volumes: - ./src:/api/src command: npx nodemon src/index.js 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)