user3001829
user3001829

Reputation: 26022

How to copy files from host to Docker container?

I am trying to build a backup and restore solution for the Docker containers that we work with.

I have Docker base image that I have created, ubuntu:base, and do not want have to rebuild it each time with a Docker file to add files to it.

I want to create a script that runs from the host machine and creates a new container using the ubuntu:base Docker image and then copies files into that container.

How can I copy files from the host to the container?

Upvotes: 2561

Views: 2526637

Answers (30)

Fayaz Khan
Fayaz Khan

Reputation: 19

You can use the Docker CP command to copy the files from the host to an existing Container

docker cp /path/on/host <container_name_or_id>:/path/in/container

I will tell you how its works 

#!/usr/bin/env bash

CONTAINER_NAME="my_temp_container"
IMAGE_NAME="ubuntu:base"
HOST_PATH="/path/on/host"
CONTAINER_PATH="/path/in/container"

# Create the container (won't start it)
docker create --name "$CONTAINER_NAME" "$IMAGE_NAME"

# Copy files from the host to the container
docker cp "$HOST_PATH" "$CONTAINER_NAME":"$CONTAINER_PATH"

# Start the container
docker start "$CONTAINER_NAME"

# Optional: run a command in the container
docker exec -it "$CONTAINER_NAME" bash -c "ls -lah $CONTAINER_PATH"

Upvotes: 0

Chammaar
Chammaar

Reputation: 61

There overly complicated answers, this worked for me:

To copy from host to container:

docker cp HOSTPATH CONTAINER:PATH

Upvotes: 5

Henry Le
Henry Le

Reputation: 1411

As of now (2024), Docker Desktop provides a clear UI application to make things like file changes much easier and intuitive.

Simply open the Container details page, go to the 'Files' tab and 'right-click' on a particular folder/file to save or import.

Hope it helps!

enter image description here

Upvotes: 5

Ali Hallaji
Ali Hallaji

Reputation: 4392

Typically there are three types:

  1. From a container to the host

    docker cp container_id:./bar/foo.txt .
    

Also docker cp command works both ways too.

dev1

  1. From the host to a container

    docker exec -i container_id sh -c 'cat > ./bar/foo.txt' < ./foo.txt
    
  2. Second approach to copy from host to container:

    docker cp foo.txt mycontainer:/foo.txt
    

dev2

  1. From a container to a container mixes 1 and 2

    docker cp container_id1:./bar/foo.txt .
    
    docker exec -i container_id2 sh -c 'cat > ./bar/foo.txt' < ./foo.txt
    

dev3

Upvotes: 218

billsecond
billsecond

Reputation: 612

I would highly recommend that you do not copy any files to any container, instead use a docker volume. This will allow you to persist files on your host. If you do not do it this way, then when its time to upgrade your docker image/container (you should do this on any release that contains any CVE's) then you would be removing the files you just uploaded.

I SFTP to my Docker Host (Linux Server usually), uploading the file there.

Upvotes: 0

Chiranjeevi Kandel
Chiranjeevi Kandel

Reputation: 1142

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH to copy from host machine to container.

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH to copy from the container to the host machine.

Upvotes: 5

Aditya Bhuyan
Aditya Bhuyan

Reputation: 458

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

The destination path must be pre-exist

Upvotes: 9

Sridhar Sarnobat
Sridhar Sarnobat

Reputation: 25286

Bring dotfile into shell session (using base64 escape)

There are times when executing a separate interactive command to the shell launch is cumbersome. Here's what I do when I want to copy a dotfile into my container so my shell will contain my customizations

docker exec -u root -it mycontainername bash -c "echo "$(cat ~/.bashrc | base64 -w 0)" | base64 --decode > /tmp/.bashrc_inside_container; bash --rcfile /tmp/.bashrc_inside_container"

Upvotes: 0

malthe
malthe

Reputation: 1449

If you're able to use a container registry, the appendlayer Python package does what you're asking for:

Assume that ubuntu:base is already available in the registry.

You can then add a new layer consisting of some local files on top of it using the script, saving the whole thing as a new image (i.e. ubuntu:test).

$ pip install appendlayer
$ tar cvf - test.txt | appendlayer <host> <repository> <old-tag> <new-tag>

All without having to rebuild or even download any of the image data to your local machine.

Upvotes: 2

Pankaj Agrawal
Pankaj Agrawal

Reputation: 203

You can use below commands

  1. Copy file from host machine to docker container

    docker cp /hostfile container_id:/to_the_place_you_want_the_file_to_be

  2. Copy file from docker container to host machine

    docker cp container_id:src_path to_the_place_you_want_the_file_to_be

Upvotes: 3

Sayed Uz Zaman
Sayed Uz Zaman

Reputation: 656

Using the following command from outside the container, I was able to copy file from my host machine to the container.

 487b00c94dd4 = Container ID
D:\demo\demo.tar= Source Url
/myfolder= Container Url

 
docker cp "D:\demo\demo.tar" 487b00c94dd4:/myfolder

Upvotes: 1

Henrik Sachse
Henrik Sachse

Reputation: 54342

The cp command can be used to copy files.

One specific file can be copied TO the container like:

docker cp foo.txt container_id:/foo.txt

One specific file can be copied FROM the container like:

docker cp container_id:/foo.txt foo.txt

For emphasis, container_id is a container ID, not an image ID. (Use docker ps to view listing which includes container_ids.)

Multiple files contained by the folder src can be copied into the target folder using:

docker cp src/. container_id:/target
docker cp container_id:/src/. target

Reference: Docker CLI docs for cp

In Docker versions prior to 1.8 it was only possible to copy files from a container to the host. Not from the host to a container.

Upvotes: 4165

Erasmus
Erasmus

Reputation: 664

I wanted to have a build process happen in the container without files like the .git folder, but I want those files when I run the container interactively to debug problems. Like Ben Davis's answer, this executes the file copy within the container. But I don't want to have to actually run that command myself when I enter the container. Thus the following script will do the trick:

# mount the current [project] directory as read only
docker run --name my_container -v $(pwd):/mnt/application:ro -itd my_image /bin/bash
# copy the missing project files from the mounted directory
docker exec -it my_container /bin/bash -c 'cp -rnT /mnt/application $HOME/application'
# interactively use the container
docker attach my_container

This leaves behind a my_container container instance. To run the command again, you would have to write docker rm my_container. Or instead, if you wanted to get into that container instance again, you could execute docker start my_container && docker attach my_container.

Note the cp -n flag, which prevents overwriting files if they already exist in the destination. The :ro portion does not allow the host files to be changed. And you could change $(pwd) to a specific directory, like /home/user/dev/application.

Upvotes: 2

jfk
jfk

Reputation: 5307

This is what worked for me

#Run the docker image in detached mode 
$docker run -it -d test:latest bash

$ docker ps
CONTAINER ID        IMAGE               COMMAND
a6c1ff6348f4        test:latest     "bash"

#Copy file from host to container
sudo docker cp file.txt a6c1ff6348f4:/tmp

#Copy the file from container to host
docker cp test:/tmp/file.txt /home

Upvotes: 4

real deal is:

docker volume create xdata
docker volume inspect xdata

so you see its mount poit dir.
just copy your stuff there
then

docker run  --name=example1 --mount source=xdata,destination=/xdata -it yessa bash

where yessa is image name

Upvotes: 1

Isura Nimalasiri
Isura Nimalasiri

Reputation: 310

Container Up Syntax:

docker run -v /HOST/folder:/Container/floder 

In docker File

COPY hom* /myFolder/        # adds all files starting with "hom"
COPY hom?.txt /myFolder/    # ? is replaced with any single character, e.g., "home.txt"

Upvotes: 9

Abhishek Patwa
Abhishek Patwa

Reputation: 177

Docker cp command is a handy utility that allows to copy files and folders between a container and the host system.

If you want to copy files from your host system to the container, you should use docker cp command like this:

docker cp host_source_path container:destination_path

List your running containers first using docker ps command:

abhishek@linuxhandbook:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
  PORTS               NAMES
  8353c6f43fba        775349758637        "bash"              8 seconds ago       Up 7 
  seconds                            ubu_container

You need to know either the container ID or the container name. In my case, the docker container name is ubu_container. and the container ID is 8353c6f43fba.

If you want to verify that the files have been copied successfully, you can enter your container in the following manner and then use regular Linux commands:

docker exec -it ubu_container bash

Copy files from host system to docker container Copying with docker cp is similar to the copy command in Linux.

I am going to copy a file named a.py to the home/dir1 directory in the container.

docker cp a.py ubu_container:/home/dir1

If the file is successfully copied, you won’t see any output on the screen. If the destination path doesn’t exist, you would see an error:

abhishek@linuxhandbook:~$ sudo docker cp a.txt ubu_container:/home/dir2/subsub
        Error: No such container:path: ubu_container:/home/dir2

If the destination file already exists, it will be overwritten without any warning.

You may also use container ID instead of the container name:

docker cp a.py 8353c6f43fba:/home/dir1

Upvotes: 7

Niyas Ali
Niyas Ali

Reputation: 273

Copy from container to host dir

docker cp [container-name/id]:./app/[index.js] index.js

(assume you have created a workdir /app in your dockerfile)

Copy from host to container

docker cp index.js [container-name/id]:./app/index.js

Upvotes: 0

Mourad MAMASSI
Mourad MAMASSI

Reputation: 885

docker cp SRC_PATH CONTAINER_ID:DEST_PATH

For example, I want to copy my file xxxx/download/jenkins to tomcat

I start to get the id of the container Tomcat

docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
63686740b488        tomcat              "catalina.sh run"   12 seconds ago      Up 11 seconds       0.0.0.0:8080->8080/tcp   peaceful_babbage

docker cp xxxx/download/jenkins.war  63686740b488:usr/local/tomcat/webapps/

Upvotes: 5

Soumyajit
Soumyajit

Reputation: 349

The simpliest way to achieve this is,

  1. Find the container id
  2. docker cp <filename> <container-id>:<path>

Upvotes: 2

RustyShackleford
RustyShackleford

Reputation: 27388

  1. Create a new dockerfile and use the existing image as your base.

    FROM myName/myImage:latest
    
    ADD myFile.py bin/myFile.py
    
  2. Then build the container:

    docker build .
    

Upvotes: 50

colin lamarre
colin lamarre

Reputation: 1785

I just started using docker to compile VLC, here's what you can do to copy files back and forth from containers:

su -
cd /var/lib/docker
ls -palR > /home/user/dockerfilelist.txt

Search for a familiar file in that txt and you'll have the folder, cd to it as root and voila! copy all you want.

There might be a path with "merged" in it, I guess you want the one with "diff" in it.

Also if you exit the container and want to be back where you left off:

docker ps -a
docker start -i containerid

I guess that's usefull when you didn't name anything with a command like

docker run -it registry.videolan.org:5000/vlc-debian-win64 /bin/bash

Sure the hacker method but so what!

Upvotes: 3

Maninder
Maninder

Reputation: 1919

This is the command to copy data from Docker to Host:

docker cp container_id:file path/filename /hostpath

docker cp a13fb9c9e674:/tmp/dgController.log /tmp/

Below is the command to copy data from host to docker:

docker cp a.txt ccfbeb35116b:/home/

Upvotes: 8

aerin
aerin

Reputation: 22694

I tried most of the (upvoted) solutions here but in docker 17.09 (in 2018) there is no longer /var/lib/docker/aufs folder.

This simple docker cp solved this task.

docker cp c:\path\to\local\file container_name:/path/to/target/dir/

How to get container_name?

 docker ps 

There is a NAMES section. Don't use aIMAGE.

Upvotes: 21

Ben Davis
Ben Davis

Reputation: 13830

The cleanest way is to mount a host directory on the container when starting the container:

{host} docker run -v /path/to/hostdir:/mnt --name my_container my_image
{host} docker exec -it my_container bash
{container} cp /mnt/sourcefile /path/to/destfile

Upvotes: 177

ishandutta2007
ishandutta2007

Reputation: 18284

In case it is not clear to someone like me what mycontainer in @h3nrik answer means, it is actually the container id. To copy a file WarpSquare.mp4 in /app/example_scenes/1440p60 from an exited docker container to current folder I used this.

docker cp `docker ps -q -l`:/app/example_scenes/1440p60/WarpSquare.mp4 .

where docker ps -q -l pulls up the container id of the last exited instance. In case it is not an exited container you can get it by docker container ls or docker ps

Upvotes: 5

h.aittamaa
h.aittamaa

Reputation: 359

In my opinion you have not to copy files inside image but you can use GIT or SVN for your files and then set a volume synchronized with a local folder. Use a script while runing container who can check if data already exist in local folder if not copy it from GIT repository. That make your image very lightweight.

Upvotes: 2

terminal
terminal

Reputation: 72

I usually create python server using this command

python -m SimpleHTTPServer

in the particular directory and then just use wget to transfer file in the desired location in docker. I know it is not the best way to do it but I find it much easier.

Upvotes: 0

karthik101
karthik101

Reputation: 1719

This is a onliner for copying a single file while running a tomcat container.

docker run -v /PATH_TO_WAR/sample.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat

This will copy the war file to webapps directory and get your app running in no time.

Upvotes: 4

prayagupadhyay
prayagupadhyay

Reputation: 31232

To copy files/folders between a container and the local filesystem, type the command:

docker cp {SOURCE_FILE} {DESTINATION_CONTAINER_ID}:/{DESTINATION_PATH}

For example,

docker cp /home/foo container-id:/home/dir

To get the contianer id, type the given command:

docker ps

The above content is taken from docker.com.

Upvotes: 15

Related Questions