Search code examples
dockersshansiblecontainersopenssh

I cannot run ssh command inside my container


I am new to docker, I have got dockerfile as below to create docker image. Then I build the image and run the following test commands:

docker run --rm --network host -d -v .:/ansible ansible:latest tail -f /dev/null
docker exec -it c0a5f9c07635 ansible --version
docker exec -it c0a5f9c07635 ansible-playbook test.yaml
docker exec -it c0a5f9c07635 bash

ansible command works fine. However, when I get in to the bash, I cannot run ssh command:

root@docker-desktop:/ansible# ssh 8.8.8.8
bash: ssh: command not found

Did I miss anything?

FROM ubuntu:22.04
WORKDIR /ansible
COPY requirements.txt /ansible
RUN apt update && apt install -y gcc python3 \
    python3-pip \
    iputils-ping \
    openssh-client

RUN apt-get clean all
RUN pip3 install --upgrade pip; \
    pip3 install -r requirements.txt

Solution

  • try without USER 0 and some minors for readability

    FROM ubuntu:20.04
    WORKDIR /ansible
    ENV ANSIBLE_VERSION 2.9.17
    
    RUN apt update && apt install -y gcc python3 \
        python3-pip \
        iputils-ping \
        openssh-client \
        && apt-get clean  && rm -Rf /var/lib/apt/lists/*  
    # ...
    

    Maybe you could use ubuntu 22.04 and requirements.txt

    FROM ubuntu:22.04
    
    #...
    
    ## pip Alternative using requirements.txt
    COPY requirements.txt .
    RUN pip3 install -r requirements.txt