0

I am trying to run multiple postman collections in a docker container. Since postman doesn't come with the feature to run a folder with multiple collections, I tried to run multiple commands in docker. Nothing worked. This is my dockerfile.

FROM postman/newman_alpine33
COPY . .
ENTRYPOINT["sh","-c"]

And this is my postman container in docker-compose.

postman:
    build:
      context: ./
      dockerfile: Dockerfile-postman
    container_name: postmanTests
    command:
       run "https://www.getpostman.com/collections/1" --env-var "base_url=http://service:8080" && 
       run "https://www.getpostman.com/collections/2" --env-var "base_url=http://service:8080"
    volumes:
      - container-volume:/var/lib/postman
    depends_on:
      - service

I tried using the sh-c command, the bash -c command, but I got an error that -c is invalid.

Elen Mouradian
  • 475
  • 1
  • 8
  • 13

1 Answers1

0

Your ENTRYPOINT line is causing problems. It forces the entire command you might want to run to be packed into a single shell word. Compose command: doesn't expect this, and I'd expect your container to try to execute the shell command run with no arguments.

One straightforward path here could be to just run multiple containers. I'm looking at the documentation for the postman/newman_alpine33 image and it supports injecting the Postman data as a Docker mount of some sort. Compose is a little better suited to long-running containers than short-lived tasks like this. So I might run

docker run                                         \
  --rm                                             \ # clean up the container when done
  -v name_container-volume:/var/lib/postman        \
  --net name_default                               \ # attach to Compose network
  postman/newman_alpine33                          \ # the unmodified image
  --url "https://www.getpostman.com/collections/1" \ # newman options
  --env-var "base_url=http://service:8080"

docker run --rm --net name_default \
  -v name_container-volume:/var/lib/postman \
  postman/newman_alpine33 \
  --url "https://www.getpostman.com/collections/2" \
  --env-var "base_url=http://service:8080"

(You could use a shell script to reduce the repetitive options, or alternatively use docker-compose run if you can write a service definition based on this image.)

If you really want to do this in a single container, it's helpful to understand what the base image is actually doing with those command arguments. The Docker Hub page links to a GitHub repository and you can in turn find the Dockerfile there. That ends with the line

ENTRYPOINT ["newman"]

so the docker run command part just supplies arguments to newman. If you want to run multiple things in the same container, and orchestrate them using a shell, you need to replace this entrypoint, and you need to explicitly restate the newman command.

For this we could do everything in a Compose setup, and that makes some sense since the Postman collections are "data" and the URLs are environment-specific. Note here that we override the entrypoint: at run time, and its value has exactly three items, sh, -c, and the extended command to be run packed into a single string.

services:
  postman:
    image: postman/newman_alpine33         # do not build: a custom image
    volumes:
      - container-volume:/var/lib/postman
      - .:/etc/newman                      # inject the collections
    entrypoint:                            # override `newman` in image
      - /bin/sh
      - -c
      - >-
          newman 
            --url "https://www.getpostman.com/collections/1"
            --env-var "base_url=http://service:8080"
          && newman
            --url "https://www.getpostman.com/collections/1"
            --env-var "base_url=http://service:8080"
    depends_on:
      - service

(The >- syntax creates a YAML block scalar; the text below it is a single string. > converts all newlines within the string to spaces and - removes leading and trailing newlines. I feel like I see this specific construction somewhat regularly in a Kubernetes context.)

David Maze
  • 130,717
  • 29
  • 175
  • 215