How can I copy a directory or file to a docker image from localhost on ubuntu14.04?

when i add a file or directory to docker image by a Dockerfile, I use ADD or COPY , but the file path i want to add or copy must a relative path to the Dockerfile’s directory. Is there a method for adding a file or directory from localhost to docker image by using absolute path? by the way, why the “docker cp” can only support copying file from docker image to localhost? on the contrary , it doesn’t work ?

  • Rails server is still running in a new opened docker container
  • Docker-Compose build failing on Google App Engine
  • How can I see a dynamically assigned port from within a docker container?
  • Understanding Docker best practices
  • Dockerhub Create Automated Build step stuck at Creating
  • What is the proper way to update the Ruby bin folder for a docker container?
  • Error with Go Get for Protobuf in Dockerfile
  • Deploy docker-compose to swarm
  • Docker Elasticsearch Bulk index timeout
  • How do I use docker from local to a remote machine?
  • Docker syslog driver with multline parsing in logstash
  • FATA[0000] Cannot connect to the Docker daemon. Is 'docker -d' running on this host?
  • 4 Solutions collect form web for “How can I copy a directory or file to a docker image from localhost on ubuntu14.04?”

    The short answer is that it is not supported.

    From the Docker docs:

    The <src> path must be inside the context of the build; you cannot ADD ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

    Furthermore, symbolic links are not supported so it is not possible to trick the build by linking to a different location on the localhost.

    However, URL:s are supported so one way to get around the problem is to to serve the files over HTTP if you enable a web server on your localhost (or in a docker container).

    The command docker cp (unfortunately) is only supported from the container to the host, not vice versa. This is described in the docs. The way to go around this is to use docker volumes where the data can be shared.

    The solution for those who use composer is to use a volume pointing to the folder:

    #docker-composer.yml
    
    foo:
      build: foo
      volumes:
        - /local/path/:/path/:ro
    

    But I’m pretty sure the can be done playing with volumes in Dockerfile.

    There is a workaround when you use docker-compose:

    api1:
      build: .
      dockerfile: cluster/manifests/Dockerfile-NodeAPI
      ports:
        - 8080
      links:
        - mongo
        - redis
      command: "nodemon lib/app.js -e js,json,scss,css,html"
    

    This still won’t allow you to do relatives paths, however basepath in the Dockerfile will be ./ instead of cluster/manifests/

    To copy a file, simply use docker cp:

    docker cp file.txt CONTAINER:/path/to/dest
    

    To copy a directory, use a tar pipe:

    tar -c "dir/" | docker run -i -a stdin CONTAINER tar -xC /path/to/dest
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.