haskell / docker-haskell

MIT License
63 stars 36 forks source link

The PATH environment variable does not contain the path to ghc when using docker-compose #63

Closed mrcjkb closed 2 years ago

mrcjkb commented 2 years ago

How to reproduce

Given the following compose file

version: '2.3'

services:
  build:
    image: haskell:9.2.1
    volumes:
      - .:/usr/app
    command: bash -lc "printenv && cd /usr/app && cabal init"

When I run

COMPOSE_FILE=<name-of-compose-file>.yml docker-compose run --rm build

Epected

Then cabal initialises a project and /opt/ghc/9.2.1/bin is in the PATH environment variable that is printed

Actual

Then cabal fails with The program 'ghc' version >=7.0.1 is required but it could not be found. And the PATH is printed as PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

mrcjkb commented 2 years ago

This could probably be solved by adding the PATH to ~/.bashrc:

RUN echo "export PATH=${PATH}" >> /root/.bashrc

I will test this later.

AlistairB commented 2 years ago

Thanks for the issue!

I'm interested to know the motivation for using a login shell? (the -l flag to bash).

https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html suggests different files that could be used to add GHC to the PATH.

I've done some testing on other official images and about 50% will work with a login shell. They seem to work because they happen to copy executables into /usr/local/bin (like we do with cabal + stack) rather than doing anything with .bash_profile etc.

I've been using docker run haskell:9.2.1 bash -lc "ghc --version" as a minimal repro BTW.

mrcjkb commented 2 years ago

Thanks for the hint. I ended up not needing the login shell for my project (I had copied over and modified the compose file from another project that was running end-to-end tests).

I'll close this issue, as I don't think there is a use case for building Haskell projects in a login shell.