How to not ignore files on .gitignore for Docker Compose

  Kiến thức lập trình

I’m having trouble with .env files on Docker Compose.

What I’m trying to do is to add .env files to .gitignore because it has all the credentials in it and I don’t want to push it to my remote repo. And I’m trying to build an app based on the .env file with Docker Compose.

It seems that if I don’t put *.env in my .gitignore then everything seems fine.
How can I make Docker Compose to use my .env file as an environment variable and make it ignored for my git?

Here’s my settings

Docker-compose.yml

version: "3"

services:
  backend:
    env_file:
      - .dev.env
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - 3000:3000

  db:
    image: postgres
    restart: always
    env_file:
      - .dev.env
    ports:
      - 5432:5432

.gitignore

node_modules/

logs
*.log

.DS_Store
*.env << IF REMOVED IT WORKS

I tried commenting my *.env on .gitignore

New contributor

Jaekyung Jung is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT