Docker for QA

Setup postman collection to run in container

  1. Install docker desktop for Windows in case you don't have one.

2. Create or get existing postman collection with some tests. Export this collection to JSON. Note: in case postman environments were used - it should be also exported to JSON file. Example collection + environment. These tests created for rest-meetup.in6k.com service. Ask TSD to launch it if needed.

3. Save both postman files in one folder.

4. Create docker file and put into the same folder: FROM - docker image with newman installed. RUN - command to install newman-reporter-html inside container

FROM postman/newman:alpine
RUN npm install --global newman-reporter-html

5. Create image based on this docker file. node_newman is the name of new image that will be stored locally

docker build -t node_newman .

6. Run the following command in command line. It will mount your folder with collections D:/docker_workshop/ to the folder inside container /etc/newman. -t node_newman tells that we're going to use our image. run Docker.postman_collection.json --environment="Docker_workshop.postman_environment.json" - tells which collection to run and what environment file to use --reporters html - tells what tool to use for reports generation

docker run -v D:/docker_workshop/:/etc/newman -t andriis_newman run Docker.postman_collection.json --environment="Docker_workshop.postman_environment.json" --reporters html

An alternative way to setup all needed soft to container we could use following docker file

FROM node
RUN npm install --global newman newman-reporter-htmlextra
WORKDIR /etc/newman
ENTRYPOINT ["newman"]

As a base we take node and then install newman and newman-reporter-htmlextra WORKDIR /etc/newman - change current directory once after container is launched ENTRYPOINT ["newman"] - tells that all commands passed after docker run will be passed directly to newman

Docker compose

In this section we'll describe how to run application (in our case node-mongoose-app) in container.

node-mongoose (Node + Mongo) application will be used as an example.

  1. Go to the app folder and create docker file COPY . . will copy all files from current local folder into the /node-app folder inside container. RUN npm install will install all required dependencies for the app inside container.

FROM node:10.16.0
WORKDIR /node-app
COPY . .
RUN npm install

2. Create image based on this docker file. node-app is the name of new image and latest is a tag.

docker build -t node-app:latest .

3. Run application in a container. node-app2 is a container name. -p 3000:3000 ports mapping <local port>:<port inside container> node-app:latest - image that is running npm start - command to run node application

docker run -d --name node-app2 -p 3000:3000 node-app:latest npm start

To check that container is running use the following command.

docker ps

4. Node app can now be accessed by the address http://localhost:3000/. But is still required DB to work properly.

5. Create docker-compose file inside the same folder.

version: "2.1"
services: 
        db: 
            image: mongo
            container_name: mongo
            ports: 
                - 27017:27107
            environment: 
                - MONGO_INIT_DATABASE=test 
        app: 
            container_name: node-app
            build: .
            image: node-app:latest
            ports:
                - 3001:3000
            command: npm start
            depends_on:
                - db
        migrate:
            image: node-app:latest
            command: node ./migration/data-migration.js
            depends_on:
                - db

Here we have 3 services db, app and migrate. The first service to run is db as app and migrate depends on it (depends_on key). DB will be started in a separate container with name mongo. Local port 27017 will be mapped to 27107 inside container. Default db name test.

App will be started in container node-app. build: . means that this container should be run using the docker file created previously.

Migration service will create all tables needed for the app.

6. To run all containers use command

docker-compose up

It will create the containers and then start them.

Now the application is available here http://localhost:3001/accounts

Last updated