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.

  • Deleting docker volumes on Dokku
  • Run AspNet Core app in docker using GMSA
  • Automatically run command inside docker container after starting up + volume mount
  • how to choose pxe setup in deploying OPNFV ARNO via Fuel (master node)?
  • docker PHP-APACHE "You don't have permission to access / on this server”
  • Docker service process zombie holding a Device Mapper managed device
  • Building dockerfile:

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

    Running dockerfile:

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

  • Run docker after building own image
  • Connection reset by peer @ io_fillbuf - fd:8 (HTTPClient::KeepAliveDisconnected)
  • Using REST API to deploy Liberty Docker containers
  • How I can inject artifact from AWS S3 inside Docker image?
  • Start a service inside docker CentOS 7 container
  • docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts
  • 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.