bashdockerebcli

No such file or directory when executing command via docker run -it


I have this Dockerfile (steps based on installation guide from AWS)

FROM amazon/aws-cli:latest

RUN yum install python37 -y

RUN curl -O https://bootstrap.pypa.io/get-pip.py

RUN python3 get-pip.py --user

RUN pip3 install awsebcli --upgrade --user

RUN echo 'export PATH=~/.local/bin:$PATH' >> ~/.bashrc

RUN source ~/.bashrc

ENTRYPOINT ["/bin/bash"]

When I build the image with docker build -t eb-cli . and then run eb --version inside container docker run -it eb-cli, everything works

bash-4.2# eb --version
EB CLI 3.20.3 (Python 3.7.1)

But, when I run the command directly as docker run -it eb-cli eb --version, it gives me this error

/bin/bash: eb: No such file or directory

I think that is problem with bash profiles, but I can't figure it out.


Solution

  • Your sourced .bashrc would stay in the layer it was sourced, but won't apply to the resulting container. This is actually more thoroughly explained in this answer:

    Each command runs a separate sub-shell, so the environment variables are not preserved and .bashrc is not sourced

    Source: https://stackoverflow.com/a/55213158/2123530

    A solution for you would be to set the PATH in an environment variable of the container, rather, and blank the ENTRYPOINT as set by your base image.

    So you could end with an image as simple as:

    FROM amazon/aws-cli:latest
    
    ENV PATH="/root/.local/bin:${PATH}"
    
    RUN yum install python37 -y \
        && pip3 install awsebcli
    
    ENTRYPOINT []
    

    With this Dockerfile, here is the resulting build and run:

    $ docker build . -t eb-cli -q
    sha256:49c376d98fc2b35cf121b43dbaa96caf9e775b0cd236c1b76932e25c60b231bc
    $ docker run eb-cli eb --version
    EB CLI 3.20.3 (Python 3.7.1)
    

    Notes: