brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker compose cp copies files in and out of a service container

Docker
~/tips/tip-docker-compose-cp

docker compose cp web:/etc/nginx/nginx.conf ./nginx.conf

Copies a file between your host and a Compose service, without looking up the container name.

With plain docker cp you first need the full container name, which in a Compose project is something like myproject-web-1. Compose has its own version that takes the service name instead.

docker compose cp web:/etc/nginx/nginx.conf ./nginx.conf

That pulls the config file out of the web service into the current folder. It works the other way round too, so you can push a file into the container:

docker compose cp ./robots.txt web:/usr/share/nginx/html/robots.txt

I use this most for looking at the default config an image ships with before I mount my own version over it. Copy it out, edit it on the host, then mount the edited file in your compose file.

A few options from the docs are worth knowing. --index picks a container when the service runs several replicas. -a (--archive) keeps the uid and gid information of the files. -L follows a symlink in the source path, and --all also includes containers that were started with docker compose run.

Caveat: a file you copy into a container lives in that container’s writable layer. Recreate the container, for example after docker compose pull and up -d, and the change is gone. For anything you want to keep, use a volume or a bind mount and treat cp as a way to grab or test files.

Further reading: docker compose cp reference

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link