Kingz
Kingz

Reputation: 1717

Docker-compose error with service env_file

I've been working on passing my sensitive data (credentials) to the docker container through a .env file. I'm using docker-compose and my docker-compose.yml file looks as follows:

 services:
   some-service:
     env_file:
       - .env

But when I try to do "docker-compose up", I get the below error:

 ERROR: In file './docker-compose.yml', service 'env_file' must be a mapping not an array.

My .env file has data like

id = 1234567890

pwd = my_pwd

Are there any indentation errors in my file? Any suggestions on how to fix this error?

Upvotes: 2

Views: 10447

Answers (3)

Kingz
Kingz

Reputation: 1717

The problem was not with the indentation. Seems like it is working now. I actually had to mention the version and build path(they were missing in my initial yml file). So the final docker-compose file would look like as follows:

version: '3'
services:
  some-service:
    build: .
    env_file:
      - .env

or if you want you can associate it with a specific image name as:

version: '3'
services:
  some-service:
    build: .
    image: image_name:image_version
    env_file:
      - .env

Hope this will help someone who run into the same issue

Upvotes: 2

BMitch
BMitch

Reputation: 265130

ERROR: In file './docker-compose.yml', service 'env_file' must be a mapping not an array.

The error says that docker-compose believes env_file is the name of your service rather than a setting on a service. This indicates you have not correctly indented the env_file section to be within a service. YML syntax is white space sensitive, so double check the indentation on your lines. If you can't find the error with your indentation, please include the full docker-compose.yml file in your question, indented exactly as it is in your file, for us to assist further.

Upvotes: 0

flogy
flogy

Reputation: 937

Looks like this is a problem with your YAML structure. It might be a missing space before your - .env or the missing service name. Try this:

services:
  some-service:
    env_file:
      - .env

If you only use one environment file, you can also write:

services:
  some-service:
    env_file: .env

See: https://docs.docker.com/compose/compose-file/#env_file

Upvotes: 3

Related Questions