37

I have the following environment variable configured in a docker-compose.yml file:

version: '3'
services:
  server:
    ports:
     - 13045:3000
    environment:
     - NODE_CONFIG: '{"DATABASE_URL":"http://db:5984"}'

When trying to run docker-compose up, I'm getting this error:

services.server.environment contains {"NODE_CONFIG": "{\"DATABASE_URL\":\"http://db:5984\"}"}, which is an invalid type, it should be a string

I need the environment variable to be set to a JSON string (see https://github.com/lorenwest/node-config/wiki/Environment-Variables#node_config)

Am I doing something wrong here? Can I get this to work somehow?

2

5 Answers 5

17

The Docker Compose file reference states, that environment variables are defined as VARIABLE=value array elements. For your case, the docker-compose.yml file would need to be changed to this:

version: '3'
services:
  server:
    ports:
     - 13045:3000
    environment:
     - NODE_CONFIG='{"DATABASE_URL":"http://db:5984"}'
3
  • Nope. I tried that first, it doesn't work.
    – Tri Nguyen
    Jun 23, 2017 at 13:18
  • @TriNguyen Strange, this works for me
    – Hexaholic
    Jun 23, 2017 at 13:24
  • Well, the build would go through, but the node process doesn't pick it up.
    – Tri Nguyen
    Jun 23, 2017 at 13:27
15

You need to remove dash in front of variable. Use syntax like that:

   environment:
     NODE_CONFIG: '{"DATABASE_URL":"http://db:5984"}'
8

The args elements themselves need to be a string

  args:
    - 'MAVEN_USER=$MAVEN_USER'
    - 'MAVEN_PASSWORD=$MAVEN_PASSWORD'
1
  • yes, this worked for me today. Oct 25, 2018 at 3:19
2
version: '3'
services:
  server:
    ports:
     - 13045:3000
    environment:
     NODE_CONFIG: '{"DATABASE_URL":"http://db:5984"}'

Just take the dash before NODE_CONFIG out. For some weird reason this dash looks to not be accepted under environment.

The following example is from official postgres docker hub https://hub.docker.com/_/postgres

# Use postgres/example user/password credentials
version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

QQ edit:

Basically, dashes are lists and no dashes are mappings, so that is why environment variable must not have dashes

References: https://stackoverflow.com/questions/64275361/docker-compose-yml-dash-syntax-in-yaml https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

0

You have to use = sign instead of :

example:

     - NODE_CONFIG= '{"DATABASE_URL":"http://db:5984"}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.