How to copy files from docker container
How to copy files from docker container
How to copy files from dockerfile to host?
I want to get some files when dockerfile built successfully, but it doesn’t copy files from container to host.
That means when I have built dockerfile, the files already be host.
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
This is now possible since Docker 19.03.0 in July 2019 introduced «custom build outputs». See the official docs about custom build outputs.
To enable custom build outputs from the build image into the host during the build process, you need to activate the BuildKit which is a newer recommended back-compatible way for the engine to do the build phase. See the official docs for enabling BuildKit.
This can be done in 2 ways:
From the official docs about custom build outputs:
custom exporters allow you to export the build artifacts as files on the local filesystem instead of a Docker image, which can be useful for generating local binaries, code generation etc.
The local exporter writes the resulting build files to a directory on the client side. The tar exporter is similar but writes the files as a single tarball (.tar).
If no type is specified, the value defaults to the output directory of the local exporter.
e.g. an example Dockerfile
The tail of the output is:
This produces a local file out/output.txt that was created by the RUN command.
All files are output from the target stage
Copy file from remote docker container
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If you want to download files from a docker container to your local machine, you can do it with Docker itself (no need for SSH):
Update: I just read that you are connected to a remote container. Then you can use SCP for that:
sounds like a great application for docker context.
This works in reverse, too, to get files from the remote docker container to your local file system. Best to have key-based access defined for your remote server.
Another solution is to create a volume, start a docker container temporarily that exports this volume to the world (ftp, smb, nfs, you name it). Once that is done, you can then mount that volume to your target container and have access to the files. This one, too, obviously works just fine in reverse.
Let’s assume that when you connect to a docker container you must first SSH to the remote host using public key, sudo to root with password, and use docker exec to get a shell in the container. This is inconvenient but it is the sort of thing we get handed and we can work around the constraints.
When you are connecting you need the following and so any command doing the copy will require this information:
First, let’s test the authentication from the localhost. The following command should return the hostname of the docker container.
One problem with this approach is that the root password will show in the process list on the docker host. We can work around this if we have SSH forward an environment variable with the password.
For example we can choose an environment variable that we know gets forwarded based on the configuration in /etc/ssh/sshd_config and set this in
We can then modify the previous command to use the new variable.
Copy Files From Docker Container to Host
Before Docker 1.8, we could only copy files from the container to the host. However, as containers became even more popular, copying files to and from containers has become necessary.
This article will discuss how to copy files from a Docker container utilizing a DockerFile, the docker cp command, and mounting directories to the host machine as volumes.
This requires installing and configuring Docker and a Docker container correctly. This will work in both Linux and WSL environments.
Use docker cp to Copy Files From Docker Container to Host
The docker cp command is one of the easiest ways that we can use to copy files and directories as well from our host machine to a docker container. We will be using Ubuntu 20.4.5 and the latest version of Docker at the time, which is 19.03.
Please enable JavaScript
We will start by first creating our target container; therefore, we also need an image. We’ll be using the official Ubuntu image available in the Docker registry.
We’ll then create a docker container in detached and interactive mode. This allows our container to run in the background.
To create a simple text file, we will navigate to the /usr/share folder.
We will start by entering into the container using the respective container id.
We will navigate the folder /usr/share/ within the container to create our text file.
We will create a file named new_file.txt using the touch command. We will shortly copy this file from this container to our host.
Therefore it is good to take note of the directory where the file is stored since we will use this location when copying it to the host.
To verify that we have created the file, run the ls command to list all files in this directory, as shown below.
We have successfully copied the new._file.txt to the host machine by executing that command. We can verify this by running the ls command in that particular folder.
Use Docker Mounts to Copy Files From Docker Container to Host
We can also use docker mounts to copy files from the docker container to the host.
Bind mounts are one of the earliest solutions that allow us to persist data through mounting our docker containers to directories on our host system.
Bind mounts allow us to reference an exact directory by referring to the absolute file path of the target directory. Besides copying files from the container to the host, we can also create services that depend on directories on the host system.
This method is often preferred because we can create multiple mounts inside a docker container. We can also create directories to transfer files from several containers.
In the example below, we’re using the Ruby official images to create a container and mount a local directory to a directory on the container. Once this is done, we can easily copy files from the container to the local directory and vice versa.
We can easily copy a file between our docker container and the host directory. Also, note that files created in the host directory will automatically be available in the docker container directory mounted to the host.
Use COPY to Copy Files From Docker Container to Host
When creating a docker image using a docker file, we can also copy files between the docker host and the container using the COPY command.
This DockerFile creates an image of a simple Flask application that prints a message on the console.
How to write data to host file system from Docker container
I have a Docker container which is running some code and creating some HTML reports. I want these reports to be published into a specific directory on the host machine, i.e. at /usr/share/nginx/reports
Am I doing something wrong?
The host machine is an Ubuntu server, and the Docker container is also Ubuntu, no boot2docker weirdness going on here.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
From «Managing data in containers», mounting a host folder to a container would be:
This is one of the type of mounts available:
The answer to this question is problematic because it varies depending on your operating system and your full requirements. The answer by VonC makes some assumptions that should be addressed and is therefore only correct in some contexts. Other answers on this topic generally ignore the fact that some people are running linux, others windows, and still others are on OSX or other weird OS’s.
One of the biggest problems (in 2020) is the use of the Windows Subsystem for Linux (WSL), where bind-mounting a host volume is fraught with error and may or may not work as expected depending on whether the path mounted is in the linux filesystem or the windows filesystem. VonC’s answer was written before WSL became a big problem, but it still makes assumptions about the local filesystem being real rather than mounted into a virtual-machine of some kind.
I have found that a lot of engineers prefer to bypass this unnecessary confusion through the use of docker volumes. A docker volume can be created with the command:
and removed with
This should list the files in that directory. If you have a file called ‘nginx.log’ for example, you could view it like this:
And the contents would be paged to your terminal.
You can bind this volume to multiple containers simultaneously if needed. This is useful if, for example, you’re writing to your logs with one container, and paging them to a console with another.
If you want to copy the example log file from above into a tmp directory on your local filesystem you can achieve that with:
Copying data between Docker containers
When running docker there are use-cases when you need to copy files and folders into the container or between containers.
There is a `docker cp` command available since the Docker 1.0 that allows you to copy files and folders out of the container. However, if you need to copy from the host to a container or between containers you’re out of luck now. At least, `docker cp` doesn’t support that.
One could create a new image each time or mount a data volume, but it is much faster to copy a bunch of files to the running container.
Docker 1.7.0 should have an extended `docker cp` command to support copying data to containers. Until that, you can use one of the alternative solutions.
In this article, I present you a workaround that relies solely on `docker cp` and `docker exec` to partially fill-in the feature we’re missing.
We consider three file copy scenarios:
You can skip the implementation details below and get the source code at the bottom of the article.
Implementation details
To copy files or folders from a container to the host we have a native `docker cp` command available since the Docker 1.0.
For example, if we have a container named kickass_yonath and wish to copy /home/data.txt from this container to the host’s current directory, it can be done the following way:
To copy files from the host to a container one can use the `docker exec` command available since Docker 1.3.0.
If have a test.txt file and wish to copy it to /home/e1/e2 in kickass_yonath container, first of all, we create a directory structure in the container with mkdir:
Next, copy the file by redirecting input and output:
When executed, the file is transferred from the current directory to the container.
CONTAINER1:PATH CONTAINER2:PATH
This use-case can be implemented by combining the both cases we listed above and using the host as the intermediate storage.
First of all, copy file to some temporary directory on the host:
Next, transfer it from the temp directory to the other container:
Source Code
The script `dcp.sh` for copying files and folders between docker containers can be downloaded from the github repo. See the included readme file for the details of usage.
NOTE: This shell script doesn’t intend to replace the extended `cp` command in the upcoming Docker 1.7, it just implements some of the functionality we’re missing right now. It is encouraged to use the extended `docker cp` in the upcoming Docker instead of this script.
Источники информации:
- http://stackoverflow.com/questions/50217304/copy-file-from-remote-docker-container
- http://www.delftstack.com/howto/docker/copy-files-from-docker-container-to-the-host/
- http://stackoverflow.com/questions/31448821/how-to-write-data-to-host-file-system-from-docker-container
- http://medium.com/@gchudnov/copying-data-between-docker-containers-26890935da3f