When running containers with Docker Compose, I sometimes want to access an unpublished port from a dependency container. For example, I sometimes need to connect directly to a database running inside a compose stack.
The usual solution is to edit (docker-)compose.yml and recreate the container with a published port. This works, but it is disruptive when the service is already running.
After experimenting a bit, I discovered a simple trick to expose container ports without recreating the containers. I'll document that in this post.
Docker makes it easy to run arbitrary processes inside an existing container by using commands like docker exec or docker compose exec.