Docker Compose throws invalid type error
Asked Answered
E

3

11

I'm having an issue running docker compose. Specifically i'm getting this error:

ERROR: The Compose file './docker-compose.yml' is invalid because:
services.login-service.environment contains {"REDIS_HOST": "redis-server"}, which is an invalid type, it should be a string

And here's my yml file:

version: '3'

services:
  redis:
    image: redis
    ports:
      - 6379:6379
    networks:
      - my-network

  login-service:
    tty: true
    build: .
    volumes:
      - ./:/usr/src/app
    ports:
      - 3001:3001
    depends_on:
      - redis
    networks:
      - my-network
    environment:
      - REDIS_HOST: redis
    command: bash -c "./wait-for-it.sh redis:6379 -- npm install && npm run dev"

networks:
  my-network:

Clearly the issue is where I set my environment variable even though i've seen multiple tutorials that use the same syntax. The purpose of it is to set REDIS_HOST to whatever ip address docker assigns to Redis when building the image. Any insights what I may need to change to get this working?

Endostosis answered 5/5, 2018 at 0:13 Comment(2)
Take out the leading - to fix the syntax error. That is, environment either expects a list of key=value, or is a dict of key: value.Pibroch
you dont need to use environment here at all, if you are inside login-service container and you will do ping redis it will redirect to current IP of the redis, how is the REDIS_HOST variable handled inside the login-service container?Fetterlock
E
30

There are two different ways of implementing it. One with = sign and other with : sign. Check the following examples for more information.

Docker compose environments with = sign.

version: '3'

services:
  webserver:
    environment:
      - USER=john
      - [email protected]

Docker compose environments with : sign

version: '3'

services:
  webserver:
    environment:
      USER:john
      EMAIL:[email protected]
Equi answered 13/5, 2020 at 15:53 Comment(0)
G
9

It happens because the leading dash. You can try without it like below:

environment:
  REDIS_HOST: redis
Geber answered 17/8, 2019 at 14:55 Comment(0)
H
0

For me, when I had two environment variables for a service in my docker-compose.yml file, I was getting the error services.web.environment.1 must be a string because one of the environment variables was

- REDIS_DATABASE_PASSWORD: ${REDIS_DATABASE_PASSWORD}

This syntax means that the REDIS_DATABASE_PASSWORD variable is not set probably because for docker compose version 3.9 setting an enviornment variable with this syntax is not allowed and does not work.

When using REDIS_DATABASE_PASSWORD: ${REDIS_DATABASE_PASSWORD}, your IDE's syntax highlighting will show REDIS_DATABASE_PASSWORD in blue and ${REDIS_DATABASE_PASSWORD} in red. This means that REDIS_DATABASE_PASSWORD: ${REDIS_DATABASE_PASSWORD} is not a string, and when setting an environment variable it must be a string. The way to solve this problem is to set the environment variable in your docker-compose.yml file using the syntax:

REDIS_DATABASE_PASSWORD=${REDIS_DATABASE_PASSWORD}

Now your IDE should show REDIS_DATABASE_PASSWORD=${REDIS_DATABASE_PASSWORD} in red, meaning it is a string.

Howlond answered 6/8, 2022 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.