Tyler Dahle
Tyler Dahle

Reputation: 879

Can you copy files from Docker container to local host?

I'm pretty nooby to Docker, so trying to see if this scenario is possible, and if so how to achieve it.

We have two json files in one folder, and we have another file in another folder that we need for local development. We don't really want to add these to our repo, but without doing so, we have to constantly manually add them back every time we work on this project (copy/pasting them in). So, I thought Docker might be a good way to solve this.

My plan was to have these files be in the docker image that myself and the other devs can use. Then when they create and run a container from that image, the Dockerfile would copy these files in the image and add them locally where they need to go, and then they would be gone when the container is stopped.

So far, I believe I have the files in my image. I added them to my container then commited to my image. Where I am stuck now, is how to use them.

So I am wondering if it is possible to use these files in the image so that the container will copy them to the right place locally and then have them be gone when the container stops, so that we don't need to have these files locally at all and they are contained in the docker container completely? Essentially if we can copy files from container to local host, and if so, will they actually go away when the container stops?

Upvotes: 0

Views: 101

Answers (1)

David Maze
David Maze

Reputation: 158647

A Docker image is not a good way to distribute data files.

From a design point of view, a Docker container is a wrapper around a single process, and an image contains the code needed to run that process and all of its OS-level dependencies. A container runs in an isolated filesystem from the host, and you need to use special options to give access to the host filesystem. Since you can use that to access any part of the host filesystem with root-level permissions, you often need administrator-level permissions to run a container at all.

In theory this is possible, but to actually run this you'd need an invocation like

sudo docker run --rm -v $PWD:/host our-org/file-copier

You also still need to commit the source code for the image somewhere, including all of the files embedded in it, and setting up automation to rebuild it is good practice. (You should pretty much never use docker commit.) That's more setup than is necessary for a single JSON file.

Some more reasonable approaches would include a repository of development-only artifacts, or having some centralized server that contains these files. (If you're otherwise on AWS, an S3 bucket fits this need nicely.)

scp dev-server:development.json .
aws s3 cp s3://my-org-dev-files/development.json .

Upvotes: 1

Related Questions