"key cannot contain a space" error while running docker compose
Asked Answered
W

5

22

I am trying to deploy my django app to app engine using dockerfile and for that after following a few blogs such as these, I created a docker-compose.yml file but when I run the docker compose up command or docker-compose -f docker-compose-deploy.yml run --rm gcloud sh -c "gcloud app deploy" I get an error key cannot contain a space. See below:

For example:

$ docker compose up

key cannot contain a space
$ cat docker-compose.yml 

version: '3.7'

services:
  app:
    build:
      context: .
    ports: ['8000:8000']
    volumes: ['./app:/app']

Can someone please help me to fix this error? I have tried yamllint to validate the yaml file for any space/indentation type of error and it doesn't show any error to me.

EDIT: Here is the content for file in the longer command:

version: '3.7'

services:
  gcloud:
    image: google/cloud-sdk:338.0.0
    volumes:
      - gcp-creds:/creds
      - .:/app
    working_dir: /app
    environment:
      - CLOUDSDK_CONFIG=/creds

volumes:
  gcp-creds:
Wayland answered 10/10, 2021 at 5:26 Comment(5)
Can someone please help?Wayland
The longer docker-compose run --rm cloud ... command you show doesn't seem to match this Compose file; is there a more complete file that demonstrates the error? The file you show seems syntactically fine.Dwight
Can you show the complete error message, exactly as it is displayed? Also make sure that the YAML you posted is the exact YAML you're using, especially concerning tabs (which are not indentation in YAML).Prickle
@Prickle The error message is complete error message :( That's all I get and I don't find any issues with the yaml file. Also, the yaml file is syntactically the same as I have posted here.Wayland
@David, the longer command is something I was trying as per what's mentioned in the blog link I shared and that is the final command to deploy the app, however, that command shows the same error too. Is it because I am running these commands on macos and something is not setup right? I will post the longer command file "docker-compose-deploy.yml" content as well if that helps.Wayland
W
32

Ok this is resolved finally! After beating my head around, I was able to finally resolve this issue by doing the following things:

  1. Unchecked the option to use "Docker Compose v2" from my docker desktop settings. Here is the setting in Docker Desktop

  2. Closed the docker desktop app and restarted it.

Please try these steps in case you face the issue. Thanks!

Wayland answered 10/10, 2021 at 19:51 Comment(6)
Didn't work for me, despite multiple repeated attempts. Currently rolling-back to version 4.0.1Spry
did you try "docker-compose up" with a dash? Read the update here about the two versions of docker-compose: docs.docker.com/compose/cli-commandWayland
It worked for mePlacate
@suman thanks for letting me know. My first solution on SO :)Wayland
@Wayland - yes I tried that and read the release notes 🤷‍♂️ I always dread updates from Docker Desktop! I'll give it another try though. ThanksSpry
@Spry Same here. Nothing worked and there were no spaces anywhere in the env file. I re-installed 4.0.1 and all is good.Monolith
S
31

Just adding another alt answer here that I confirmed worked for me when following the steps above did not. My case is slightly different, but as Google brought me here first I thought I'd leave a note.

Check your env var values for spaces!

This may only be applicable if you are using env_var files (appreciate that OP is not in the minimal example, hence saying this is different). Unescaped spaces in variables will cause this cryptic error message.

So, given a compose file like this:

version: '3.7'

services:
  gcloud:
    image: google/cloud-sdk:338.0.0
    volumes:
      - gcp-creds:/creds
      - .:/app
    working_dir: /app
    env_file:
      - some_env_file.env

If some_env_file.env looks like this:

MY_VAR=some string with spaces

then we get the cryptic key cannot contain a space.

If instead we change some_env_file.env to be like this:

MY_VAR="some string with spaces"

then all is well.

The issue has been reported to docker-compose.

Google brought me here first, and when your suggestion sadly didn't work for me, it then took me to this reddit thread, where I found out the above.

Seer answered 19/10, 2021 at 14:9 Comment(1)
this is the right answer! i guess between docker-compose v2 and v1, they started to check for values format in .env fileMarine
C
4

Docker Compose (at least since v2) automatically parses .env files before processing the docker-compose.yml file, regardless of any env_file setting within the yaml file. If any of the variables inside your .env file contains spaces, then you will get the error key cannot contain a space.

Two workarounds exist at this time:

  1. Rename your .env file to something else, or
  2. Create an alternate/empty .env file, e.g. .env.docker and then explicitly set the --env-file parameter, i.e. docker compose --env-file .env.docker config.

Track the related issues here:

  1. https://github.com/docker/compose/issues/6741
  2. https://github.com/docker/compose/issues/8736
  3. https://github.com/docker/compose/issues/6951
  4. https://github.com/docker/compose/issues/4642
  5. https://github.com/docker/compose/commit/ed18cefc040f66bb7f5f5c9f7b141cbd3afbbc89
  6. https://docs.docker.com/compose/env-file/
Controversy answered 6/12, 2021 at 8:55 Comment(0)
C
1

One more thing to be care about - since Compose V2, this error may be raise in case you have inline comments in the env file used by Compose. For example

---
version: "3.7"

services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    env_file: .app.env

and that .app.env is like this

RABBIT_USER=user  # RabbitMQ user

the same error may occur. To fix just move comment to its own line

# RabbitMQ user
RABBIT_USER=user
Chalice answered 26/11, 2021 at 10:38 Comment(0)
C
1

Another excellent /s way to get dumped into this cauldron of confusion is to have an empty .env file in your directory. As in :

l .env
-rw-r--r--  1 redacted  staff  0 Mar  2 17:34 .env

Once I rm .env’d, the devilish key cannot contain a space demon left my ‘pooter.

Curson answered 2/3, 2023 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.