How to copy file from host to container using Dockerfile
Asked Answered
I

8

131

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 called abc.txt in my host machine. How can I copy it to this container. Is there any step that I can add in Dockerfile which copy from Host to Container.

Issue answered 26/5, 2015 at 9:46 Comment(3)
by the way, wget is already included in Ubuntu 12.04, if memory serves meLeastways
Your update and install should probably be placed on one line, see docs.docker.com/articles/dockerfile_best-practices/#runAccomplice
do you want the file in a docker image or just in a docker container?Shugart
F
178

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

Farewell answered 29/5, 2015 at 9:34 Comment(4)
Is there a possibility of doing this using the absolute path of the host machine?Turenne
Unfortunately no, you can only add files from the folder of your Dockerfile or subfolders of it. Also, soft links will not work either. This is due to the docker build process. Building starts by compressing the directory where Dockerfile resides to an archive and uses this archive to retrieve files used in ADD/COPY commands.Farewell
thanks, I wrote a shell script to automate that part. It would have been better if I could handle that through Dockerfile.Turenne
This works perfectly except when you change your Docker build context and forget about it. If you're running docker build in a script check through your script to see if you changed your build context. Once you get the source file to the right context it just works. /facepalmEdict
O
57

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.

Opener answered 24/10, 2018 at 8:3 Comment(4)
Adding to this -- if you move a Dockerfile in pycharm, it can alter the context automatically.Vorous
Ahh thanks! Found a pesky .gitignore that was preventing ADD foo.zip . in my Dockerfile.Mickens
Thanks! For me the issues was .gitignoreJanot
For me, the WORKDIR trick did the job.Betulaceous
L
9

I faced this issue, I was not able to copy zeppelin [1GB] directory into docker container and was getting issue

COPY failed: stat /var/lib/docker/tmp/docker-builder977188321/zeppelin-0.7.2-bin-all: no such file or directory

I am using docker Version: 17.09.0-ce and resolved the issue with the following steps.

Step 1: copy zeppelin directory [which i want to copy into docker package]into directory contain "Dockfile"

Step 2: edit Dockfile and add command [location where we want to copy] ADD ./zeppelin-0.7.2-bin-all /usr/local/

Step 3: go to directory which contain DockFile and run command [alternatives also available] docker build

Step 4: docker image created Successfully with logs

Step 5/9 : ADD ./zeppelin-0.7.2-bin-all /usr/local/ ---> 3691c902d9fe

Step 6/9 : WORKDIR $ZEPPELIN_HOME ---> 3adacfb024d8 .... Successfully built b67b9ea09f02

Lozar answered 7/12, 2017 at 12:55 Comment(1)
The important step here is the fact the file must be in the same folder as the DockerfileDeserving
O
4

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.

Oulu answered 16/6, 2020 at 9:24 Comment(0)
L
3

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

Leastways answered 26/5, 2015 at 11:2 Comment(0)
N
2

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.

Negligence answered 10/6, 2020 at 16:1 Comment(0)
M
0

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

docker build  -t <imagename:tag> -f- ./ < Dockerfile
Maris answered 25/6, 2020 at 6:0 Comment(0)
C
0

This is what I had to do to copy an external nginx.conf file into the container's /etc/nginx directory on Linux.

In docker-compose.yml

 services:
 
   xhgui:
     build:
       # This directory is at docker/xhgui
       context: ./xhgui
       args:
         # This file is at docker/xhgui/config
         NGINX_CONFIG_FILE: ./config/nginx.conf
     image: xhgui/xhgui:latest
     restart: always
     # Not sure if this volume declaration is necessary
     volumes:
       - ../:/xhgui

Then in docker/xhgui/Dockerfile

ARG NGINX_CONFIG_FILE
COPY ${NGINX_CONFIG_FILE} /etc/nginx/nginx.conf
Cermet answered 30/8, 2023 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.