Why is git clone failing when I build an image from a dockerfile?

FROM ansible/ansible:ubuntu1604

MAINTAINER myname


RUN git clone http://github.com/ansible/ansible.git /tmp/ansible
RUN git clone http://github.com/othertest.git /tmp/othertest
WORKDIR /tmp/ansible
ENV PATH /tmp/ansible/bin:/sbin:/usr/sbin:/usr/bin:bin
ENV PYTHONPATH /tmp/ansible/lib:$PYTHON_PATH
ADD inventory /etc/ansible/hosts
WORKDIR /tmp/
EXPOSE 8888

When I build from this dockerfile, I get Cloning into /tmp/ansible and othertest in red text (I assume is an error). When I then run the container and peruse around, I see that all my steps from the dockerfile built correctly other than the git repositories which are missing.

I can’t figure out what I’m doing wrong, I’m assuming its a simple mistake.

  • Creating docker images with java application [closed]
  • Running Maven inside a Cloud9 Docker container
  • Docker containers work on port 80 only
  • Jenkins: DirectoryNotEmptyException: …/lastSuccessful
  • Configuring the network interface's index of a Docker container
  • Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer
  • Building dockerfile:

    sudo docker build --no-cache -f Dockerfile .
    

    Running dockerfile:

    sudo docker run -I -t de32490234 /bin/bash
    

  • Unable to see images built through docker remote api's in docker cli result
  • Setting up Ruby along with android in docker
  • How to get the URL of Swarm agent load balancer in Azure container service
  • Neo4j 2.3.2 to cloud foundry
  • Cross container communication with Docker
  • Docker-compose running container
  • One Solution collect form web for “Why is git clone failing when I build an image from a dockerfile?”

    The short answer:

    Put your files anywhere other than in /tmp and things should work fine.

    The longer answer:

    You’re basing your image on the ansible/ansible:ubuntu1604 image. If you inspect this image via docker inspect ansible/ansible:ubuntu1604 or look at the Dockerfile from which it was built, you will find that it contains a number of volume mounts. The relevant line from the Dockerfile is:

    VOLUME /sys/fs/cgroup /run/lock /run /tmp
    

    That means that all of those directories are volume mount points, which means any data placed into them will not be committed as part of the image build process.

    Looking at your Dockerfile, I have two comments unrelated to the above:

    • You’re explicitly setting the PATH environment variable, but you’re neglecting to include /bin, which will cause all kinds of problems, such as:

      $ docker run -it --rm bash
      docker: Error response from daemon: oci runtime error: exec: "bash": executable file not found in $PATH.
      
    • You’re using WORKDIR twice, but the first time (WORKDIR /tmp/ansible) you’re not actually doing anything that cares what directory you’re in (you’re just setting some environment variables and copying a file into /etc/ansible).

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.