dockerignore with Docker compose not ignoring .env file

2.1k Views Asked by At

Below you can see a representation of my project folder structure. I have two microservices which are called auth and profile, they are located inside the services directory. The docker-containers directory hold my docker-compose.yaml file in which I list all the images of my application.

.
├── services
│   ├── auth  
│   │   ├── src
│   │   ├── dist
│   │   ├── .env
│   │   ├── package.json
│   │   ├── Dockerfile
│   │   ├── .dockerignore
│   ├── profile 
│   │   ├── src
│   │   ├── dist
│   │   ├── .env
│   │   ├── package.json
│   │   ├── Dockerfile
│   │   ├── .dockerignore   
└── docker-containers
    ├── docker-compose.yaml

Below is my docker-compose.yaml file in which I define the location of the auth service (and other images). I also want to override the local .env file with the values from the environment list. But when I run the docker compose project the values from my local .env file are still being used.

version: "3.8"

services:
  auth:
    build: 
      context: ../services/auth
    container_name: auth-service
    depends_on: 
      - redis
      - mongo
    ports:
      - 3000:3000
    volumes:
      - ../services/auth/:/app
      - /app/node_modules
    command: yarn dev
    env_file: ../services/auth/.env
    environment:
      FASTIFY_PORT: 3000
      REDIS_HOST: redis
      FASTIFY_ADDRESS: "0.0.0.0"
      TOKEN_SECRET: 1d037ffb614158a9032c02f479b36f42dd33ba325f76a7692498c33839afc5d547eae2b47f0f4926b76b08fc91d19352
      MONGO_URL: mongodb://root:example@mongo:27017
    
  mongo:
    image: mongo
    container_name: mongo
    restart: on-failure
    ports:
      - 2717:27017
    volumes:
      - ./mongo-data:/data
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  redis:
    image: redis
    container_name: redis
    volumes:
      - ./redis-data:/data
    ports:
      - 6379:6379

This is my Dockerfile and the .dockerignore file inside the auth service and based on my understanding the local .env file should not be copied to the docker context, because it is listed inside the .dockerignore file.
But when I log a value from my environment variables from the docker application it still logs the old value from my local .env file.

FROM node:16-alpine

WORKDIR /app

COPY ["package.json", "yarn.lock", "./"]

RUN yarn

COPY dist .

EXPOSE 3000

CMD [ "yarn", "start" ]
node_modules

Dockerfile

.env*
.prettier*
.git

.vscode/

The weird part is that the node_modules folder of the auth services is being ignored but for some reason the environment variables inside the docker container are still based on the local .env file.

0

There are 0 best solutions below