How do I copy files from the host to a Docker container using docker cp?

I’m working on a backup and restore solution involving Docker containers. I have a base image (ubuntu:base) and want to avoid rebuilding it every time I need to add files. Instead, I plan to create a script that starts a new container from this image and copies files from the host into the running container. What is the proper way to copy files from my host machine to a Docker container using the docker cp command?

I’ve been working with Docker for a few years now, especially when setting up backup workflows. One of the quickest tricks I picked up early on was using docker cp to move files into a running container. I usually go with something like:

docker cp /path/on/host container_name:/path/in/container  

It’s simple and saves time, no need to rebuild the image just to update a few files. Just ensure the container is up and running when you do this.

Yes, I do something similar! In fact, in my projects, I intentionally avoid baking files into the Docker image. Instead, after spinning up a container with:

docker run -d --name mycontainer ubuntu:base  

…I’ll use docker cp like this:

docker cp ./backup.sql mycontainer:/tmp/backup.sql  

This method helps a lot when your files, like backup scripts, are changing frequently. It just makes the whole workflow more flexible without dealing with image rebuilds every time.

Totally agree with both of you. I’ve faced the same situation while managing dynamic config files. When the container was up, I just ran:

docker cp ./config.json container_id:/app/config.json  

What I really like about docker cp is how cleanly it integrates into an existing container’s lifecycle. You can tweak or replace files as needed, no dockerfile changes, no image rebuilds, just a quick copy and you’re done. It’s especially useful in environments where you want minimal downtime or need to push quick fixes.