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

FROM ansible/ansible:ubuntu1604


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
ADD inventory /etc/ansible/hosts

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.

  • Etcd cluster setup failure
  • Chrome Node in Selenium/Docker keeps crashing
  • How to forward data from a host computer to a Docker container?
  • Docker image behaves different on different machines
  • Port mapping in Docker on Mac OSX installed with Docker Toolbox
  • Jenkins build inside a docker container with generated reports
  • Building dockerfile:

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

    Running dockerfile:

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

  • docker-compose and graceful Celery shutdown
  • Java Spring will not talk to Consul when run as a Docker container
  • Linux “Perf” tool cannot run inside docker images?
  • How to get two Docker containers talking to each other?
  • docker-machine boot2docker root password
  • How to create autostart services in Docker 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.