Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"key cannot contain a space" error while running docker compose

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:
like image 257
xis10z Avatar asked Oct 10 '21 05:10

xis10z


Video Answer


3 Answers

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.

like image 139
Drum Avatar answered Oct 19 '22 20:10

Drum


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!

like image 26
xis10z Avatar answered Oct 19 '22 20:10

xis10z


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/
like image 2
W1M0R Avatar answered Oct 19 '22 20:10

W1M0R