Skip to main content

Docker Engine (Part 9)

Copying Files and Publishing Ports in Docker

Updated 6 min readMarkdown
Part of seriesDemystifying Docker

Once your containers are up and running, two practical questions immediately come up in day-to-day workflows: how do you move files in and out of a container without rebuilding the image, and how do you expose a containerized web app or database so external users can actually reach it?

In this guide, we will cover copying files between your Docker host and containers using docker container cp, every way to publish and bind ports (-p, -P, interface binding, and random ports), and how Docker uses Linux iptables under the hood to route traffic.

1. Copying Files Between Host and Container (docker container cp)

Even though containers are isolated from the host filesystem, Docker provides the docker container cp command to transfer files and directories in either direction—without needing SSH or attaching an interactive shell.

Copy From Host to Container

Specify the local host path first, followed by <container_name>:<destination_path>:

# Copy a single file to an exact file path inside the container
docker container cp /tmp/web.conf webapp:/etc/nginx/conf.d/web.conf

# Copy a file into an existing directory (keeps original filename)
docker container cp /var/tmp/app.conf webapp:/root/

Copy From Container to Host

Swap the source and destination arguments to extract logs, configs, or generated artifacts back to your host:

docker container cp webapp:/var/log/nginx/access.log /tmp/access.log

Copying Entire Directories & Important Rules

You can also copy entire directories recursively (for example, docker container cp ./static-site webapp:/var/www/html). Keep three rules in mind:

  • Destination directory must exist: If you copy a file into a container directory path (ending in /), that directory must already exist inside the container, or the command will fail.
  • Silent overwrites: If a file with the same name already exists at the destination path, docker cp overwrites it without prompting.
  • Works on stopped containers: Unlike docker exec, docker container cp works even when a container is in a Created or Exited state—making it ideal for recovering files from crashed containers.

2. Port Mapping & Publishing Ports (-p vs. -P)

By default, every Docker container receives an internal bridge IP address (such as 172.17.0.2). While you can reach that IP directly from the Docker host itself (e.g., curl http://172.17.0.2:5000), external machines on your network or the internet cannot route to internal container IPs.

To make a container accessible externally, you must publish (map) a port on the Docker host to the port your application listens on inside the container.

  1. Static Port Mapping (-p <host_port>:<container_port>) Use lowercase -p (--publish) to bind a specific host port to a container port:
docker container run -d --name webapp -p 80:5000 my-python-app
  • Traffic hitting port 80 on the Docker host ([http://192.168.1.5:80](http://192.168.1.5:80)) is forwarded to port 5000 inside the container.
  • Run multiple instances: Because each container gets its own isolated network namespace, you can run multiple instances of the same service (like two MySQL databases both listening internally on 3306) by mapping them to different host ports (-p 3306:3306 and -p 8306:3306).
  • Port conflicts: Two containers cannot bind to the same host port on the same IP interface—the second container will fail to start with a port is already allocated error.
  • Persists across restarts: Because the host port is statically defined, restarting the container keeps the exact same host port.

2. Binding to a Specific Network Interface (-p <ip>:<host_port>:<container_port>)

If your Linux server has multiple network cards (NICs)—say, a public internet IP (10.2.4.10) and a private internal LAN IP (192.168.1.1)—running -p 8000:5000 binds to 0.0.0.0, exposing your app on every network interface.

To restrict access to a single interface, prefix the mapping with that interface's IP address:

# Accessible ONLY on the internal 192.168.1.1 network
docker container run -d -p 192.168.1.1:8000:5000 my-python-app

# Accessible ONLY from the Docker host itself (loopback interface)
docker container run -d -p 127.0.0.1:8000:5000 my-python-app

3. Dynamic Host Port Mapping (-p <container_port>)

If you omit the host port and specify only the container port:

docker container run -d -p 5000 my-python-app

Docker maps container port 5000 to a random ephemeral port on the host (e.g., 41232) chosen from the Linux kernel's local port range defined in /proc/sys/net/ipv4/ip_local_port_range (typically 32768–60999).

4. Auto-Publish All Exposed Ports (-P)

Uppercase -P (--publish-all) automatically maps every port declared with the EXPOSE instruction in the image's Dockerfile (plus any ports added at runtime via --expose) to random ephemeral ports on the host:

docker container run -d --name web-random -P httpd

Production Warning on -P: Whenever a container created with -P (or -p <container_port>) is stopped and restarted, Docker assigns a brand-new random host port. Always use static lowercase -p host:container mappings in production so ports remain predictable.

(Tip: To check which ports an image exposes before running it, inspect the image metadata: docker image inspect -f '{{.Config.ExposedPorts}}' httpd.)

3. Under the Hood: How Docker Routes Ports with iptables

Docker does not run a heavy application proxy for every packet; instead, it programs packet-filtering and Network Address Translation (NAT) rules directly into the Linux kernel firewall using iptables.

When you publish a port with -p 41232:5000:

  1. Docker creates two custom chains in iptables: DOCKER (managed automatically by the Docker daemon) and DOCKER-USER (reserved for administrators to add custom firewall rules without Docker overwriting them).
  2. In the nat table's DOCKER chain, Docker inserts a DNAT (Destination NAT) rule that intercepts incoming traffic destined for host port 41232 and rewrites its destination IP and port to the container's internal bridge IP (172.17.0.2:5000).

You can inspect these live NAT rules on your Linux host anytime:

sudo iptables -t nat -L DOCKER -n -v

Quick Reference Cheat Sheet

Task

Command

Notes

Copy host → container

docker container cp ./file.conf webapp:/etc/

Target directory must exist inside container

Copy container → host

docker container cp webapp:/var/log/app.log ./

Works even on stopped containers

Static port mapping

docker container run -d -p 8080:80 nginx

Recommended for production; port stays fixed on restart

Bind to private IP / localhost

docker container run -d -p 127.0.0.1:8080:80 nginx

Blocks access from external network interfaces

Publish all EXPOSE ports

docker container run -d -P nginx

Assigns random ports (32768–60999); changes on restart

Inspect kernel port rules

sudo iptables -t nat -L DOCKER -n -v

Shows live DNAT forwarding rules created by Docker

No comments yet. Be the first to comment.