Dockerize django app along side with cucumber test

Here is the case. I have simple django app with cucumber tests. I dockerized the django app and it works perfect, but I want to dockerize the cucumber test too and run them. Here is my project sturcutre:

-cucumber_drf_tests
   -feature
   -step_definitions
   axiosinst.js
   config.js
   package.json
   cucumber.js
   Dockerfile
   package-lock.json
-project_apps  
-common
docker-compose.yaml
Dockerfile
manage.py
requirements.txt

Here is my cucumber_drf_tests/Dockerfile

FROM node:12

WORKDIR /app/src

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8000

CMD ["yarn", "cucumber-drf"] (this is how I run my test locally)

My second Dockerfile

FROM python:3.8

ENV PYTHONUNBUFFERED=1

RUN mkdir -p /app/src

WORKDIR /app/src

COPY requirements.txt /app/src

RUN pip install -r requirements.txt

COPY . /app/src

And my docker-compose file

version: "3.8"
services:
  test:
    build: ./cucumber_drf_tests
    image: cucumber_test
    container_name: cucumber_container
    ports:
      - 8000:8000
    depends_on:
      - app
  app:
    build: .
    image: app:django
    container_name: django_rest_container
    ports:
      - 8000:8000
    volumes:
    - .:/django #describes a folder that resides on our OS within the container
    command: >
      bash -c "python manage.py migrate
      && python manage.py loaddata ./project_apps/fixtures/dummy_data.json
      && python manage.py runserver 0.0.0.0:8000"
    depends_on:
      - db
  db:
    image: postgres
    container_name: postgres_db
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=bla
      - POSTGRES_PASSWORD=blaa

If I remove I remove test service and run the tests locally everything is fine, but otherwise I got different errors the last one is:

Bind for 0.0.0.0:8000 failed: port is already allocated

It is logic I know, but how to tell to test_container to make the API calls to the address of the running django_rest_container. Maybe this dummy question but I am new of containers world so every sharing of good practice is wellcomed

The issue is in exposing the ports. You are exposing both app and test on the same port (8000). For container you can keep it same. But for host it has to be different.

<host port> : <container port> 

This is how ports are mapped in docker. So either change the host port in app or test to different port like below.

For app keep below ports:

7500:8000

Now your app will be accessible at port 7500 and test at 8000

Back to Top