How to copy file from host to container using Dockerfile

前端 未结 7 1199
Happy的楠姐
Happy的楠姐 2020-12-13 03:11

I have written a Dockerfile which looks like this

FROM ubuntu:12.04

RUN apt-get update
RUN apt-get install -y wget

Now I\'m having a file

相关标签:
7条回答
  • 2020-12-13 03:47

    If you want to copy the current dir's contents, you can run:

    docker build  -t <imagename:tag> -f- ./ < Dockerfile
    
    0 讨论(0)
  • 2020-12-13 03:48

    you can use either the ADD command https://docs.docker.com/engine/reference/builder/#/add or the COPY command https://docs.docker.com/engine/reference/builder/#/copy

    0 讨论(0)
  • 2020-12-13 04:06

    I got the following error using Docker 19.03.8 on CentOS 7:

    COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/abc.txt: no such file or directory
    

    The solution for me was to build from Docker file with docker build . instead of docker build - < Dockerfile.

    0 讨论(0)
  • 2020-12-13 04:07

    I was able to copy a file from my host to the container within a dockerfile as such:

    1. Created a folder on my c driver --> c:\docker
    2. Create a test file in the same folder --> c:\docker\test.txt
    3. Create a docker file in the same folder --> c:\docker\dockerfile
    4. The contents of the docker file as follows,to copy a file from local host to the root of the container: FROM ubuntu:16.04

      COPY test.txt /

    5. Pull a copy of ubuntu from docker hub --> docker pull ubuntu:16.04
    6. Build the image from the dockerfile --> docker build -t myubuntu c:\docker\
    7. Build the container from your new image myubuntu --> docker run -d -it --name myubuntucontainer myubuntu "/sbin/init"
    8. Connect to the newly created container -->docker exec -it myubuntucontainer bash
    9. Check if the text.txt file is in the root --> ls

    You should see the file.

    0 讨论(0)
  • 2020-12-13 04:09

    For those who get this (terribly unclear) error:

    COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/abc.txt: no such file or directory

    There could be loads of reasons, including:

    • For docker-compose users, remember that the docker-compose.yml context overwrites the context of the Dockerfile. Your COPY statements now need to navigate a path relative to what is defined in docker-compose.yml instead of relative to your Dockerfile.
    • Trailing comments or a semicolon on the COPY line: COPY abc.txt /app #This won't work
    • The file is in a directory ignored by .dockerignore or .gitignore files (be wary of wildcards)
    • You made a typo

    Sometimes WORKDIR /abc followed by COPY . xyz/ works where COPY /abc xyz/ fails, but it's a bit ugly.

    0 讨论(0)
  • 2020-12-13 04:12

    Use COPY command like this:

    COPY foo.txt /data/foo.txt
    # where foo.txt is the relative path on host
    # and /data/foo.txt is the absolute path in the image
    

    read more details for COPY in the official documentation

    An alternative would be to use ADD but this is not the best practise if you dont want to use some advanced features of ADD like decompression of tar.gz files.If you still want to use ADD command, do it like this:

    ADD abc.txt /data/abc.txt
    # where abc.txt is the relative path on host
    # and /data/abc.txt is the absolute path in the image
    

    read more details for ADD in the official documentation

    0 讨论(0)
提交回复
热议问题