Contents

Docker- Production Workflow

Some notes about the development, testing and deployment workflow of docker

Architecture


In Development In Production
1
2
3
Docker Container

  npm run start
1
2
3
Docker Container

  npm run build

Dockerfile.dev


for development
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install

COPY . .

CMD ["npm", "run", "start"]

Could be run using
1
2
docker build -f Dockerfile.dev .
docker run -p 3000:3000 <imageID>

Using Volumes to reflect change in local code

1
docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app <imageID>
  1. Map the pwd into the ‘/app’ folder
  2. Put a bookmark on the node_modules folder, do not map to outside folder

Gitbash

1
winpty docker run -it -p 3000:3000 -v /app/node_modules -v "/$(PWD)":/app CONTAINER_ID

PowerShell

1
docker run -it -p 3000:3000 -v /app/node_modules -v ${pwd}:/app CONTAINER_ID

Docker Compose shorthand for volume


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
version: '3'
services:
  web:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - .:/app

Executing Tests


1
docker run -it <imageID> npm run test

1
docker exec -it <containerID> npm run test

Docker Compose for Running Tests


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: '3'
services:
  web:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - .:/app
  tests:
    stdin_open: true
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - /app/node_modules
      - .:/app
    command: ['npm', 'run', 'test']

Attach to the test suite


1
2
docker ps
docker attach <tests_containerID>

Dockerfile (Production)


Multi-Step Builds


1
2
3
4
5
6
7
8
9
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx
COPY --from=0 /app/build /usr/share/nginx/html

To run

1
2
docker build .
docker run -p 8080:80 <imageID>