docker / cli

The Docker CLI
Apache License 2.0
4.88k stars 1.92k forks source link

"docker run" does not allow command arguments containing spaces #5050

Closed MaxEliaserAWS closed 5 months ago

MaxEliaserAWS commented 5 months ago

Description

If you wish to invoke an executable with an argument containing spaces, shell interpreters allow you to use quotes and-or escaping to prevent the argument being split into multiple arguments, e.g. this always works: bash -c "echo hi"

However, the Docker client appears to do a second pass on its arguments, splitting arguments containing spaces. Although I can use quotes to prevent my shell from splitting the argument, once my shell has invoked the Docker client with my unitary argument, Docker seems to have a mind of its own and splits it in two anyway.

Reproduce

docker run --rm centos:7 bash -c "echo hi"

I have also tried all the following variants:

docker run --rm centos:7 bash -c echo\ hi
docker run --rm centos:7 bash -c echo\\\ hi
docker run --rm centos:7 bash -c "echo\ hi"
docker run --rm centos:7 bash -c "'echo hi'"
docker run --rm centos:7 bash -c '"echo hi"'
docker run --rm --entrypoint bash centos:7 -c "echo hi"

and many other variations tried besides.

Expected behavior

Text "hi" should be printed to stdout

docker version

Client:
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.3
 Git commit:        24.0.5-0ubuntu1~20.04.1
 Built:             Mon Aug 21 19:50:14 2023
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.3
  Git commit:       24.0.5-0ubuntu1~20.04.1
  Built:            Mon Aug 21 19:50:14 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.2
  GitCommit:
 runc:
  Version:          1.1.7-0ubuntu1~20.04.2
  GitCommit:
 docker-init:
  Version:          0.19.0
  GitCommit:

also reproduces on this version:

Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:51:00 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:48:42 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

Client:
 Version:    24.0.5
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 55
 Server Version: 24.0.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  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 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version:
 runc version:
 init version:
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 5.15.146.1-microsoft-standard-WSL2
 Operating System: Ubuntu 20.04.6 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 15.62GiB
 Name: SEA-1801730572
 ID: 95fb2e65-b7b7-4da8-ab1c-7c820e4cc0ea
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support

Also reproduces with this version:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.16.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 25
 Server Version: 23.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  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: runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 6.2.2-1.el7.elrepo.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 15.55GiB
 Name: eliaserm-dev
 ID: D5M3:E3ZU:2RCG:N5EA:ZIP4:CQ77:Q3QT:QMTG:GNUL:ZZJ6:F3GM:T6K3
 Docker Root Dir: /home/elemental/.docker_data
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
  drydock.elementalad.com:5000
  127.0.0.0/8
 Live Restore Enabled: true

Additional Info

Just to explain the use case here. I have a container built that already has a PATH set, but I want to append another directory to the PATH on a one-off basis just for one command. Because the host environment does not have access to the container's default PATH value, using --env argument to Docker client is not a suitable solution. Instead, I am trying to do something like this: docker run --rm my-container /bin/bash -c 'PATH=$PATH:/new/directory comand'

However, there is no way to express this without including a space in my bash command.

MaxEliaserAWS commented 5 months ago

My bad folks. Forgot I had this in my bashrc:

docker()
{
    if [ "x$1" = "xkillall" ] ; then
        for i in `docker ps | grep " $2 " | sed 's/ .*//'` ; do
            `which docker` kill $i
        done
    else
        `which docker` $@
    fi
}

Changing to "$@" with quotes fixes my problem.