Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose: command not found #363

Closed
affektde opened this issue Mar 17, 2022 · 13 comments · Fixed by #364
Closed

docker-compose: command not found #363

affektde opened this issue Mar 17, 2022 · 13 comments · Fixed by #364
Labels

Comments

@affektde
Copy link
Contributor

affektde commented Mar 17, 2022

  • Sail Version: 1.13.17
  • Laravel Version: 9.5.1
  • PHP Version: 8.1.3
  • OS: Linux 5.13.0-35-generic Ubuntu 21.10

Description:

Laravel Sail was upgraded via composer update today.
We use docker compose instead of docker-compose on our system.
Before Version 1.13.17 we had now issues with Sail.

This commit introduced this new line here: DOCKER_COMPOSE=(docker-compose) : 2092e1c

Following errors now occur:

$ sail
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 82: docker-compose: command not found
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 88: docker-compose: command not found
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 369: docker-compose: command not found

Steps To Reproduce:

Linux with "Oh My Zsh"
and following line in .zshrc: alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'

If I echo out

# Define Docker Compose command prefix...
DOCKER_COMPOSE=(docker-compose)
type -a docker-compose

It says:

$ sail
docker-compose is aliased to `docker compose'
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 82: docker-compose: command not found
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 88: docker-compose: command not found
/home/user/Code/project/vendor/laravel/sail/bin/sail: line 369: docker-compose: command not found

I try to find out, what is special on my environment, that docker-compose: command not found is happening.

The change docker-compose to "${DOCKER_COMPOSE[@]}", seems to have a side-effect.

@driesvints
Copy link
Member

Ping @ribeirobreno ^

@driesvints driesvints added the bug label Mar 17, 2022
@affektde
Copy link
Contributor Author

eval "${DOCKER_COMPOSE[@]}"

If I prefix all "${DOCKER_COMPOSE[@]}" with eval, it works. But I am not a bash-script expert. Maybe this is a Linux thing and we have to do this for Linux users only.

@ribeirobreno
Copy link
Contributor

I've installed ZSH (sudo apt install zsh) Oh My Zsh under WSL and sail still works under zsh.

The actual bug is probably not related to "Oh My Zsh" but running sail under any non-bash terminal while not having a Compose v1 executable. I'm setting up a VM with Ubuntu 21.10 and no docker-compose executable to test this hipotesis.
If this is correct, the workaround for now should be installing the latest stable docker-compose.

This is what I have right now:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
Codename:       focal

$ zsh --version
zsh 5.8 (x86_64-ubuntu-linux-gnu)

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

$ docker compose version
Docker Compose version v2.2.3

$ docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

$ docker version
Client: Docker Engine - Community
 Cloud integration: v1.0.22
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:48 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:43:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

@affektde
Copy link
Contributor Author

Yes, I am researching for command substitution under zsh.

Laravel Sail supported docker compose V2 with aliasing docker-compose LINE 3-6.

if ! [ -x "$(command -v docker-compose)" ]; then
    shopt -s expand_aliases
    alias docker-compose='docker compose'
fi

But somehow this alias is not working anymore.

For now I've added eval as workaround.

@affektde
Copy link
Contributor Author

affektde commented Mar 17, 2022

if ! [ -x "$(command -v docker-compose)" ]; then
    function docker-compose () {
        docker compose "$@"
    }
fi

This is also working for me. I have read some articles right now and command substitution and using an alias in a bash script is not recommended. It worked without "${DOCKER_COMPOSE[@]}".

But maybe we have to change first lines 3-6 to a shell function now.

https://www.gnu.org/software/bash/manual/html_node/Aliases.html

There is no mechanism for using arguments in the replacement text, as in csh. If arguments are needed, a shell function should be used (see Shell Functions).

@Farid-NL
Copy link

But if you create the alias and use it as the composer v1 it works as expected:

$  alias docker-compose='docker compose'
$  docker-compose up -d
...
$  docker-compose down

The script for some reason store the docker-compose string in a variable called DOCKER_COMPOSE, and this variable is an array?: DOCKER_COMPOSE=(docker-compose)

So when this variable is called like this "${DOCKER_COMPOSE[@]}" the alias is not expanded, since the docker-compose string is used instead of the alias... I think?.

Here's some little test I did:

#!/usr/bin/env bash

shopt -s expand_aliases
alias e='echo'

ECHOING=(e)
ECHOING2=e

e 'WORK'

"${ECHOING[@]}" 'DOES NOT WORK'

"${ECHOING2[@]}" 'DOES NOT WORK'

And runs...

WORK
./script: line 11: e: command not found
./script: line 13: e: command not found

@affektde
Copy link
Contributor Author

If I read the bash reference correctly, we have to overwrite the docker-compose function with a shell function. With the new command variable DOCKER_COMPOSE an alias will no longer work.

https://www.gnu.org/software/bash/manual/bash.html#Aliases

To be safe, always put alias definitions on a separate line, and do not use alias in compound commands.
For almost every purpose, shell functions are preferred over aliases.

Thank you @FaruNL for this nice demo.

@ribeirobreno
Copy link
Contributor

ribeirobreno commented Mar 18, 2022

This fixes it and still works under my other envs (Mac and WSL2):
image

Requirements:

A minimal environment to test the issue: https://gist.github.com/ribeirobreno/e37f817e7cfa60f4e20021707feeaf8c

To test:

  • Setup a new laravel install
    curl -s https://laravel.build/example-app | bash
    cd example-app
  • Apply the changes
  • Run zsh
  • Run sail commands

@driesvints
Copy link
Member

@affektde can you try out @ribeirobreno's solution?

@affektde
Copy link
Contributor Author

if ! [ -x "$(command -v docker-compose)" ]; then
    DOCKER_COMPOSE=(docker compose)
else
    DOCKER_COMPOSE=(docker-compose)
fi

Testest, but we have to switch if-else I think. If command -v docker-compose return nothing, we have docker compose.

Or just remove ! :-)

@ribeirobreno
Copy link
Contributor

I've went ahead and created a PR to speed up things in case this works. :)

@ribeirobreno
Copy link
Contributor

Yes! You are correct @affektde ! I did remove the ! locally but submitted it with it in the PR.
cc @driesvints

@ribeirobreno
Copy link
Contributor

Updated the PR, should be good to go now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants