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 create plus docker cp pulls a file out of an image without running it

Docker
~/tips/tip-docker-create-cp-extract-file

docker create --name tmp nginx:alpine

Copy a default config file out of an image without starting the app.

You want the default config file of an image to start your own from. Running the container would start the app, open ports and maybe write data. A created but never started container is enough to copy from.

docker create --name tmp nginx:alpine
docker cp tmp:/etc/nginx/nginx.conf ./nginx.conf
docker rm tmp

docker create sets up the container’s filesystem from the image and stops there. No process runs, no entrypoint script touches anything. docker cp works on that stopped container like on any other, and the last line cleans it up.

I like this most for images without a shell. Distroless and scratch based images have no cat or sh, so docker run --rm image cat file fails, while copying out still works.

Copying a whole folder works too; docker cp tmp:/etc/nginx ./nginx-conf brings the directory with everything in it.

You get the file as the image ships it, which is not always what the running app uses. Many images generate their real config at startup from environment variables, so the file in the image can be a template or missing entirely. If that is the case, copy from a running container instead, after the entrypoint has done its work. And remember the docker rm, or created containers pile up in docker ps -a.

Further reading: docker container create, docker container cp

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