TypeError: environment can only contain strings when using run with .env file
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
- Create docker-compose.yml file:
version: '2.1'
services:
ubuntu:
image: ubuntu:bionic
- Create .env file:
A=a
(I made sure that it contains only these three characters, i.e. 41 3D 61)
3. 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
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:
-
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
-
Rename docker-compose-Windows-x86_64.exe to docker-compose.exe
-
Place docker-compose.exe anywhere in your working environement e.g.
E:\dev\docker-compose\docker-compose.exe -
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:\dev\docker-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 issue TypeError: 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.
- 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:\dev\docker-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\bin\docker-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
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 437beforedocker-composeon 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