How to copy files from a running Docker container to your local file system?
September 22, 2019    |    Docker    |    How To    |    Tutorial

If you have ever needed to copy files from a running Docker container to your local file system, you’re in the right place. Here is the command:

Quick Example

docker container cp <container-name>:/path/to/file-in-container /local/path/to-save-file

Note: To find the container name run:

docker ps -a

Full Example

Recently, I was working on a tutorial called Building a PDF Generator on AWS Lambda with Python3 and wkhtmltopdf. This tutorial utilized a binary from the popular software WKHTMLtoPDF and required a very specific version to work properly.

Not being able to find a build of the binary for the specific version I needed from the WKHTMLtoPDF website, I decided to use Docker to install the software via a Linux container and then copy the binary from there. The other option was to manually build one from the source. The following are the steps I took to get the binary file I needed for the Serverless PDF generator service.

Note: I’m working on a Linux OS with Docker version 19.03.2, build 6a30dfc installed.

Step 1: Dockerfile Setup

The first thing we need to do is set up a directory to store a new Dockerfile. This Dockerfile will set up a Linux system and install the software we need.

Create a new directory called docker-htmltopdf

$ mkidr docker-htmltopdf

Go into the directory you created:

$ cd docker-htmltopdf

Create a docker file:

$ touch Dockerfile

Add the following to the docker file:

FROM ubuntu:latest
RUN apt-get update && apt-get -y install wget
RUN wget
RUN apt update -y && apt install -y ./wkhtmltox_0.12.5-1.bionic_amd64.deb

Step 2: Build the Docker Image

Now that we have the Dockerfile ready to build, let’s build it with the name htmltopdf. The following command will build our Docker image with the correct version of WKHTMLtoPDF that we need.

$ docker build -t htmltopdf .

After this command is completed, you’ll have a Docker container on your computer called htmltodpdf that you can run locally.

Step 3: Run the Docker container in the background

Now let’s run the container in the background so that we can copy files from it. In this example, we start the container with the name temp-container.

In addition, to keep the container running, we add tail -f /dev/null to the command. This prevents the Docker container from shutting down immediately.

$ docker run -d --name temp-container htmltopdf tail -f /dev/null

Step 4: Copy Binary from Running Image

Now that we have a container running, let’s copy the binary we need from the running Docker container to the current folder we are in:

$ docker container cp temp-container:/usr/local/bin/wkhtmltopdf .

And for good measure let’s kill the container on our system:

$ docker kill temp-container
$ docker rm temp-container


And there we have it! We should have the exact pre-built binary we need on our system. By utilizing Docker to build the binary file we needed for our application we were able to get the software we needed without installing any extra dependencies on our system. I bet you can think of many more use cases for this technique!

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *