Compose: TypeError: environment can only contain strings when using run with .env file

Created on 21 Jan 2020  Â·  32Comments  Â·  Source: docker/compose

Description of the issue

docker-compose run fails to run when non-empty ".env" file is present, printing TypeError: environment can only contain strings. docker-compose up works correctly.

This started happening when I upgraded today from older Docker Desktop release with docker-compose 1.24.0.

Context information (for bug reports)

Output of docker-compose version

docker-compose version 1.25.2, build 698e2846
docker-py version: 4.1.0
CPython version: 2.7.16
OpenSSL version: OpenSSL 1.0.2q  20 Nov 2018

Output of docker version

Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:22:37 2019
 OS/Arch:           windows/amd64
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:29:19 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker-compose config

services:
  ubuntu:
    image: ubuntu:bionic
version: '2.1'

Steps to reproduce the issue

  1. Create _docker-compose.yml_ file:
version: '2.1'
services:
  ubuntu:
    image: ubuntu:bionic
  1. Create _.env_ file:
A=a

(I made sure that it contains only these three characters, i.e. 41 3D 61)

  1. Run docker-compose run ubuntu - fails with "TypeError: environment can only contain strings". Removing/emptying this file letsdocker-compose to run correctly.

Observed result

Fails to start

Expected result

Runs container using environment variables from _.env_

Stacktrace / full error message

Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 72, in main
  File "compose\cli\main.py", line 128, in perform_command
  File "compose\cli\main.py", line 865, in run
  File "compose\cli\main.py", line 1349, in run_one_off_container
  File "compose\cli\main.py", line 1469, in call_docker
  File "subprocess.py", line 172, in call
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child
TypeError: environment can only contain strings
[19220] Failed to execute script docker-compose

Additional information

OS: Windows 10 Pro 1909 (18363.592) - up to date
docker-compose from Docker Desktop (stable, 2.2.0.0 42247).

Result is the same regardless of used shell: Power Shell / bash from Git for Windows / cmd

kinbug

Most helpful comment

OK, I got workaround using earlier docker-compose v1.24.1 while awaiting a fix for Docker Desktop

Steps:

  1. Download docker-compose v1.24.1
    please looking for the file docker-compose-Windows-x86_64.exe from this tag
    https://github.com/docker/compose/releases/tag/1.24.1

  2. Rename docker-compose-Windows-x86_64.exe to docker-compose.exe

  3. Place docker-compose.exe anywhere in your working environement e.g.
    E:\dev\docker-compose\docker-compose.exe

  4. Override docker-compose v1.25.2 shipped with Docker Desktop using older version of docker-compose (v1.24.1) by going to Windows Environment Variables / System variables / Path as screenshot

image

please ensure e:devdocker-compose comes before C:\Program Files\Docker\Docker\resources\bin

Go to cmd and test if docker-compose v1.24.1 is literally active

$ docker-compose -v
docker-compose version 1.24.1, build 4667896b

Phew! the issueTypeError: environment can only contain strings is gone

All 32 comments

Oh, it seems that while I was trying to work around this and preparing my bug report somebody already did that in https://github.com/docker/for-win/issues/5517

My case, it happens on Windows CMD after upgraded the Docker desktop to v2.2.0.0 (42247) with docker compose v1.25.2 this morning

docker-compose exec container bash

Here is stack trace

Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 72, in main
  File "compose\cli\main.py", line 128, in perform_command
  File "compose\cli\main.py", line 491, in exec_command
  File "compose\cli\main.py", line 1469, in call_docker
  File "subprocess.py", line 172, in call
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child
TypeError: environment can only contain strings
[6136] Failed to execute script docker-compose

However, when use docker-compose v1.24.1 in WSL against docker desktop v2.2.0.0 (42247), it runs succesfully.

OK, I got workaround using earlier docker-compose v1.24.1 while awaiting a fix for Docker Desktop

Steps:

  1. Download docker-compose v1.24.1
    please looking for the file docker-compose-Windows-x86_64.exe from this tag
    https://github.com/docker/compose/releases/tag/1.24.1

  2. Rename docker-compose-Windows-x86_64.exe to docker-compose.exe

  3. Place docker-compose.exe anywhere in your working environement e.g.
    E:\dev\docker-compose\docker-compose.exe

  4. Override docker-compose v1.25.2 shipped with Docker Desktop using older version of docker-compose (v1.24.1) by going to Windows Environment Variables / System variables / Path as screenshot

image

please ensure e:devdocker-compose comes before C:\Program Files\Docker\Docker\resources\bin

Go to cmd and test if docker-compose v1.24.1 is literally active

$ docker-compose -v
docker-compose version 1.24.1, build 4667896b

Phew! the issueTypeError: environment can only contain strings is gone

I confirm that it is also happening to me since I upgrade to latest version of docker desktop.

$ docker-compose exec web bash
Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 72, in main
  File "compose\cli\main.py", line 128, in perform_command
  File "compose\cli\main.py", line 491, in exec_command   
  File "compose\cli\main.py", line 1469, in call_docker   
  File "subprocess.py", line 172, in call
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child       
TypeError: environment can only contain strings
[6188] Failed to execute script docker-compose

$ docker-compose --version

docker-compose version 1.25.2, build 698e2846

$ docker --version

Docker version 19.03.5, build 633a0ea

I am experiencing the same issue when running docker-compose exec after upgrading to 1.25.2. Uninstalling and reinstalling docker-compose 1.24 restored the previous functionality.

Confirm same issue.

Same issue.

Same issue.

  1. Override docker-compose v1.25.2 shipped with Docker Desktop using older version of docker-compose (v1.24.1) by going to Windows Environment Variables / System variables / Path as screenshot

please ensure e:devdocker-compose comes before C:\Program Files\Docker\Docker\resources\bin

In my case in my PATH I didn't have C:\Program Files\Docker\Docker\resources\bin, so I did the old hack of renaming new docker-compose.exe to docker-compose-1.25.exe and paste the docker-compose-Windows-x86_64.exe as docker-compose.exe. Everything works like before :-)

Thanks for the tip @atthakorn !

Can confirm this issue too. The workaround from @atthakorn worked!

Same issue. Thanks @atthakorn for the workaround, been trying to find a way to get a previous version up and running again so I will try that.

Update:
The fix worked by replacing docker-compose.exe in the installation directory (default C:\Program Files\Docker\Docker\resources\bindocker-compose.exe). Unfortunately I have no full control over environment variables on my machine.

The workaround does work, but I now get the next error if I want to connect into the container:

winpty docker-compose exec app bash --login

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Confirmed after upgrade to Docker Desktop for Windows 2.2.0.0.

Workaround worked: Replaced docker-compose.exe v 1.25.2 with v 1.24.1 in C:\Program Files\Docker\Docker\resources\bin

The same issue here, v1.25.2 threw the error, swapped to v1.24.1 and it worked. Docker Desktop for Windows 2.2.0.0.

Same here. I upgraded to 2.2.0.0 last night and now I get this error every time I try to open a bash in the container.

Same issue typing on console "winpty docker-compose exec --user=laradock workspace bash" after update docker on windows 10

Traceback (most recent call last):
File "docker-compose", line 6, in
File "compose\cli\main.py", line 72, in main
File "compose\cli\main.py", line 128, in perform_command
File "compose\cli\main.py", line 491, in exec_command
File "compose\cli\main.py", line 1469, in call_docker
File "subprocess.py", line 172, in call
File "subprocess.py", line 394, in __init__
File "subprocess.py", line 644, in _execute_child
TypeError: environment can only contain strings

Also confirming this error for any exec interaction with containers, replacing the docker-compose with 1.24.1 in C:\Program Files\Docker\Docker\resources\bin as sugested by @atthakorn and @mstellingwerff fixed the issue for the time being.

In another issue #7170 following fix was mentioned and works for me:

my workaround is chcp 437 before docker-compose on 2.2.0.

Likewise, I worked around the issue by installing docker-compose via python 3.8 and pip
https://github.com/docker/compose/issues/7170#issuecomment-577416345

This issue also happened on my side, the workaround i use is by SSH into the containers directly using docker command, not to be confused with docker-compose.

Example:
Usually i use command

docker-compose exec [service-name] bash # SSH to container

The workaround i use is

docker ps # to find out the container id
docker exec -it [container-id] bash # SSH to container

Got it to work for me by following steps 1 & 2 from the quoted answer below.
then for step 3 what I did was to go to C:\Program Files\Docker\Docker\resources\bin, made a backup copy of existing docker-compose.exe, then pasted the file from step 2 (the one that's v1.24.1)

after that, docker-compose works again:

docker-compose -v
docker-compose version 1.24.1, build 4667896b

OK, I got workaround using earlier docker-compose v1.24.1 while awaiting a fix for Docker Desktop

Steps:

  1. Download docker-compose v1.24.1
    please looking for the file docker-compose-Windows-x86_64.exe from this tag
    https://github.com/docker/compose/releases/tag/1.24.1
  2. Rename docker-compose-Windows-x86_64.exe to docker-compose.exe
  3. Place docker-compose.exe anywhere in your working environement e.g.
    E:\dev\docker-compose\docker-compose.exe
  4. Override docker-compose v1.25.2 shipped with Docker Desktop using older version of docker-compose (v1.24.1) by going to Windows Environment Variables / System variables / Path as screenshot

image

please ensure e:devdocker-compose comes before C:\Program Files\Docker\Docker\resources\bin

Go to cmd and test if docker-compose v1.24.1 is literally active

$ docker-compose -v
docker-compose version 1.24.1, build 4667896b

Phew! the issueTypeError: environment can only contain strings is gone

The same problem on MAC

Same issue for me when using docker-compose via pycharm. Windows docker 19.03.5 Compose v1.25.2.

Same issue for me when using docker-compose via pycharm. Windows docker 19.03.5 Compose v1.25.2.

Check the last comment in this topic https://stackoverflow.com/questions/59011504/failed-to-execute-script-docker-compose-laravel/59873283#59873283

I override the docker-compose.exe with this release in this folder (Win10), and it's working:
C:\Program Files\Docker\Docker\resources\bin
Last release: https://github.com/docker/compose/releases/tag/1.25.3

My case, it happens on Windows CMD after upgraded the Docker desktop to v2.2.0.0 (42247) with docker compose v1.25.2 this morning

docker-compose exec container bash

Here is stack trace

Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 72, in main
  File "compose\cli\main.py", line 128, in perform_command
  File "compose\cli\main.py", line 491, in exec_command
  File "compose\cli\main.py", line 1469, in call_docker
  File "subprocess.py", line 172, in call
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child
TypeError: environment can only contain strings
[6136] Failed to execute script docker-compose

However, when use docker-compose v1.24.1 in WSL against docker desktop v2.2.0.0 (42247), it runs succesfully.

sir. how do you downgrade docker-compose to v1.24.1

My case, it happens on Windows CMD after upgraded the Docker desktop to v2.2.0.0 (42247) with docker compose v1.25.2 this morning
docker-compose exec container bash
Here is stack trace

Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 72, in main
  File "compose\cli\main.py", line 128, in perform_command
  File "compose\cli\main.py", line 491, in exec_command
  File "compose\cli\main.py", line 1469, in call_docker
  File "subprocess.py", line 172, in call
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child
TypeError: environment can only contain strings
[6136] Failed to execute script docker-compose

However, when use docker-compose v1.24.1 in WSL against docker desktop v2.2.0.0 (42247), it runs succesfully.

sir. how do you downgrade docker-compose to v1.24.1

@gamenic-ramesh try this, or download the 1.24.1 tag...
download---> Last release: https://github.com/docker/compose/releases/tag/1.25.3
overwrite---> C:\Program Files\Docker\Docker\resources\bin

image

I still have the problem even if the docker-compose version = 1.25.3 (Override the docker-compose from Docker Desktop)

PS : Override with the 1.24.1 work perfectly !

Hello! Check your .env file.

пт, 7 февр. 2020 г. в 04:33, March Kasama Kaewsainguan <
[email protected]>:

[image: image]
https://user-images.githubusercontent.com/16873595/73993327-6e1b1200-4984-11ea-8efa-abcfe77803da.png

I still have the problem even if the docker-compose version = 1.25.3

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/docker/compose/issues/7169?email_source=notifications&email_token=ADHBB5U2VIX2YXR5R3Z5FBTRBS277A5CNFSM4KJ22DWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELBNEPI#issuecomment-583193149,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ADHBB5XLT6XC2LYJDLJZ27DRBS277ANCNFSM4KJ22DWA
.

@z3niths Could you please open a new issue including your docker-compose.yml and all the environment files, including the .env?
From the feedback here, the issue is solved by 1.25.3. Note that a even newer release (1.25.4) is available with a fix on environment variables with no value. Maybe that's your issue.

@z3niths Could you please open a new issue including your docker-compose.yml and all the environment files, including the .env?
From the feedback here, the issue is solved by 1.25.3. Note that a even newer release (1.25.4) is available with a fix on environment variables with no value. Maybe that's your issue.

Thanks, I've found a root problem already.
Seem like my .env contain some environment key without a value.

Was this page helpful?
0 / 5 - 0 ratings