Compose exec cannot process more than 32KB of data
Description
Cannot process more than 32KB of data using docker compose exec. The command has exit code 0 and no output.
If a smaller file is used, then the command works as expected.
Consider the following docker-compose.yml
version: "3"
services:
foo:
image: golang
entrypoint: ['sleep', '1200']
Steps to reproduce the issue:
- Create a 1MB file with random data
dd if=/dev/urandom of=sample.txt bs=1M count=1 - Start compose
docker compose up -d - Exec a command inside the container
cat sample.txt | docker compose exec -T foo wc -c
Describe the results you received: There is no output and exit code is zero.
Describe the results you expected:
The output should be the number of bytes of sample.txt
1048576
Additional information you deem important (e.g. issue happens only occasionally):
Note that a smaller than 32KB file works and the output is as expected.
$ dd if=/dev/urandom of=sample.txt bs=32K count=1
$ cat sample.txt | docker compose -p golang exec -T foo wc -c
32768
In addition, if using docker exec directly the command works as expected:
cat sample.txt | docker exec -i golang-foo-1 wc -c
1048576
When looking at the dockerd logs, there is an error when running the docker compose exec command:
time="2021-10-20T07:23:49.076829730Z" level=error msg="Error running exec 102649cf8af30af96614f9083ea597de800d8b4b251a04093558e281accc95c6 in container: exec attach failed: error attaching stdout stream: write unix /var/run/docker.sock->@: write: broken pipe"
I suspect the problem is somewhere here. I think the related implementation of docker exec is here and that may have the correct implementation.
Given that it works for 32KB, I suspect it's the stream decoding of the stdout or stderr may have the bug.
Output of docker compose version:
Docker Compose version 2.0.0-rc.2+azure
Output of docker info:
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Build with BuildKit (Docker Inc., 0.6.3+azure)
compose: Docker Compose (Docker Inc., 2.0.0-rc.2+azure)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 20.10.8+azure
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: false
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: e25210fe30a0a703442421b0f60afac609f950a3
runc version: 4144b63817ebcc5b358fc2c8ef95f7cddd709aa7
init version:
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.4.0-1059-azure
Operating System: Ubuntu 20.04.3 LTS (containerized)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.775GiB
Name: codespaces_80e910
ID: TY3U:PLBN:P3FO:QURI:S2K3:KBWZ:2Z27:RH3D:DXD7:R2B5:RGAA:65D3
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: codespacesdev
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Additional environment details:
I think I found the problem, namely the exec doesn't copy the output once the input is finished:
https://github.com/docker/compose/blob/244834ff12c10fc4d82879bf9d7559080f3dcd75/pkg/compose/exec.go#L135
The docker cli implementation does wait for it:
https://github.com/docker/cli/blob/e57b5f78de635e6e2b688686d10b830c4747c4dc/cli/command/container/hijack.go#L57
I'll try to make a patch to see if this fixes it
Actually figured out that the problem is because the random input contains the detach escape sequence which makes it abort. Looking at how the docker cli handles this, it seems it doesn't include the detach functionality unless the exec is a TTY which in this case it isn't (details here).
The fix should be in https://github.com/docker/compose/blob/244834ff12c10fc4d82879bf9d7559080f3dcd75/pkg/compose/run.go#L175 namely we should just return the input stream if no TTY is involved.