How to set up a unit test in a Docker app for nodejs?

I am trying to run a mocha unit test for my node application. The app is built with docker image.

Docker image:

FROM node:6.10.0-alpine

RUN mkdir -p /app

COPY package.json /app
RUN npm install

COPY . /app

CMD ["npm", "start"]


Docker composes:

version: "3"
  web: #### nodejs image
    build: .
      - ./app/         
      - "3000:3000"
      - db
    build:  ##### postgres db image
      context: .
      dockerfile: dbDockerfile
      - 5432:5432


The setup can be built and work as expected. The problem is not that I'm sure how to execute unit test commands like mocha

to execute a test.

I see a module called dockunit

, but I'm not sure if this is the only way. Can anyone help me about this?


source to share

1 answer

With docker

(and docker-compose

) you can run arbitrary commands in the container. Dockerfile

defines a default command that runs when no other command is specified, but that doesn't mean it's the only one you can run.

In your case: npm start

runs when no other command is specified. This happens when you do docker-compose up


But you can execute any command using docker run

or docker-compose run

. For your tests it might look like this: docker-compose run web mocha


In up

and run

there is a small difference, and I recommend you read this: Should I start or build dockers?

Does it help you get started?



All Articles