How to copy files from host to Docker container?

0 votes

I have Docker base image that I have created, ubuntu:base, and do not want 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?

Can anyone help me with this query?

Thanks

Aug 24, 2018 in DevOps & Agile by Damon Salvatore
• 5,510 points
1,229 views

2 answers to this question.

0 votes

Here is what you can do. The cp command can be used to copy files. One specific file can be copied like:

docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt

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

docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

Reference: Docker CLI docs for cp

I hope it would help you resolve your query.

answered Aug 24, 2018 by Damon Salvatore
• 5,510 points
0 votes

You could also try 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
answered Aug 9 by Sirajul
• 36,820 points

Related Questions In DevOps & Agile

+1 vote
2 answers

How do I run Apache server from Docker without mapping to a location?

If you're seeing a 500 error, that's ...READ MORE

answered Oct 18, 2018 in DevOps & Agile by lina
• 8,110 points
82 views
0 votes
1 answer

How do I get a Docker container's IP address from the host?

Save your docker image as a tar ...READ MORE

answered Aug 29, 2018 in DevOps & Agile by shubham
• 6,890 points
54 views
0 votes
1 answer
+5 votes
7 answers

Docker swarm vs kubernetes

Swarm is easy handling while kn8 is ...READ MORE

answered Aug 27, 2018 in Docker by Mahesh Ajmeria
1,032 views
0 votes
1 answer
0 votes
1 answer

How to edit file after I shell to a docker container?

Since docker images are trimmed to bare minimum ...READ MORE

answered Sep 11, 2018 in DevOps & Agile by Damon Salvatore
• 5,510 points
79 views