Volumes, ports and networks

Junior Frequently asked 5 min

On this page

The question

How do you persist data in Docker, given that containers are disposable?

30-second answer

Keep it out of the container's writable layer, which is deleted with the container. Mount storage instead. A named volume is managed by Docker, lives outside any container and survives removal and image upgrades, so it suits databases. A bind mount maps a host directory into the container, which suits local configuration and source code during development. A tmpfs mount keeps files in memory only. In production, managed databases and object storage often replace local volumes altogether.

Understand it

Host, bridge network and volumes

Tap the parts

Select a part to see what it does.

Tap a part of the Docker host. The top rows carry traffic, the middle row holds the two containers, and the rows below are where files end up.

View as text

A Docker host. At the top, a published port that maps host port 8080 to the orders container, and, apart, the default bridge network. Below them, a user-defined bridge network, app-net, joins two containers: orders, on 8080, and postgres, on 5432, which is not published. Under orders, its writable layer and a bind mount from a host folder; under postgres, a named volume.

Port: -p 8080:8080 publishes the container port 8080 on host port 8080, so a browser on the host reaches orders at localhost:8080.

Default net: A container started without --network joins the default bridge.

app-net bridge: A user-defined bridge, docker network create app-net, gives its containers a private subnet and an embedded DNS server, so orders finds the database as postgres:5432, whatever IP it gets.

orders: The Spring Boot service listens on 8080 in its own network namespace.

postgres: The database listens on 5432 for containers on app-net only.

Writable: Each container writes to a thin layer on top of its image.

Volume: A named volume, -v pgdata:/var/lib/postgresql/data, is storage Docker manages outside any container.

Bind mount: A bind mount, -v ./config:/config, maps a host directory into the container, both ways.

A Docker host. At the top, a published port that maps host port 8080 to the orders container, and, apart, the default bridge network. Below them, a user-defined bridge network, app-net, joins two containers: orders, on 8080, and postgres, on 5432, which is not published. Under orders, its writable layer and a bind mount from a host folder; under postgres, a named volume.

Port: -p 8080:8080 publishes the container port 8080 on host port 8080, so a browser on the host reaches orders at localhost:8080.

Default net: A container started without --network joins the default bridge.

app-net bridge: A user-defined bridge, docker network create app-net, gives its containers a private subnet and an embedded DNS server, so orders finds the database as postgres:5432, whatever IP it gets.

orders: The Spring Boot service listens on 8080 in its own network namespace.

postgres: The database listens on 5432 for containers on app-net only.

Writable: Each container writes to a thin layer on top of its image.

Volume: A named volume, -v pgdata:/var/lib/postgresql/data, is storage Docker manages outside any container.

Bind mount: A bind mount, -v ./config:/config, maps a host directory into the container, both ways.

Takeaway: Containers on a user-defined network reach each other by name and container port. Publishing a port opens one to the host. Data that must outlive a container goes to a volume, not its writable layer.

Where should this data live?If the data does not need to outlive the container, the writable layer or a tmpfs mount in memory is enough. If it must survive, a host folder you edit by hand, such as local configuration or code, is a bind mount, and data the application owns, such as a database, is a named volume managed by Docker.noyesyesnoMust it outlivethe container?Writable layer,or tmpfs in memoryA host folderyou edit?Bind mount:./config:/configNamed volume:pgdata:/var/lib/...
Where should this data live?If the data does not need to outlive the container, the writable layer or a tmpfs mount in memory is enough. If it must survive, a host folder you edit by hand, such as local configuration or code, is a bind mount, and data the application owns, such as a database, is a named volume managed by Docker.noyesyesnoMust it outlivethe container?Writable layer,or tmpfs in memoryA host folderyou edit?Bind mount:./config:/configNamed volume:pgdata:/var/lib/...
Where should this data live?

In code

docker network create app-net
docker volume create pgdata

docker run -d --name postgres --network app-net \
  -v pgdata:/var/lib/postgresql/data -e POSTGRES_DB=orders -e POSTGRES_PASSWORD=secret postgres:17

docker run -d --name orders --network app-net -p 8080:8080 \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/orders orders:1.4.2
A network and a volume, then a database and the service that uses it
docker run --rm --network app-net alpine:3.20 nslookup postgres
# Name: postgres  Address: 172.18.0.2        <- the embedded DNS answers with the container's IP

docker run --rm alpine:3.20 nslookup postgres
# ** server can't find postgres: NXDOMAIN   <- default bridge: no names

docker rm -f postgres                        # the container and its writable layer are gone
docker run -d --name postgres --network app-net \
  -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:17
# the tables are still there: they live in pgdata, not in the container

docker port orders                           # 8080/tcp -> 0.0.0.0:8080
Names resolve on your network, not on the default bridge; the volume outlives the container
# Works from the IDE, fails in a container: nothing listens on port 5432 of the orders container
 Wrong spring.datasource.url=jdbc:postgresql://localhost:5432/orders

# The database container's name and its container port, on the same user-defined network
 Right spring.datasource.url=jdbc:postgresql://postgres:5432/orders
Inside a container, localhost is the container itself
A browser request, and the service calling its databaseThe browser on the host calls localhost:8080. Docker forwards the published host port to port 8080 of the orders container. orders asks Docker's embedded DNS for the name postgres, gets the container's IP on app-net and connects to it on port 5432, which is not published to the host.postgres containerEmbedded DNSorders containerBrowser on the hostlocalhost:8080, forwarded to 8080who is postgres?172.18.0.2 on app-netconnect 172.18.0.2:5432connected200 OK
A browser request, and the service calling its databaseThe browser on the host calls localhost:8080. Docker forwards the published host port to port 8080 of the orders container. orders asks Docker's embedded DNS for the name postgres, gets the container's IP on app-net and connects to it on port 5432, which is not published to the host.postgres containerEmbedded DNSorders containerBrowser on the hostlocalhost:8080, forwarded to 8080who is postgres?172.18.0.2 on app-netconnect 172.18.0.2:5432connected200 OK
A browser request, and the service calling its database

Solve it

The service works from the IDE but cannot reach the database in a container

A developer runs PostgreSQL with docker run -p 5432:5432 postgres:17 and the Spring Boot service from the IDE, with spring.datasource.url pointing at localhost:5432. All good. Then they containerize the service and run it with docker run -p 8080:8080 orders: it fails at startup with “Connection refused”. Explain and fix it.

  1. Ask where localhost is. The service now has its own network namespace. localhost is the orders container itself, where nothing listens on 5432; the published port belongs to the host, not to the container.
  2. Give them a shared network. Create a user-defined network and start both containers on it. Docker’s embedded DNS then resolves each container by its name.
  3. Use the name and the container port. Point the service at postgres:5432. Container to container, the published host port does not matter; keep -p 127.0.0.1:5432:5432 only for a local SQL tool.
  4. Keep the code environment-free. Override the URL with the SPRING_DATASOURCE_URL variable, which Spring Boot binds to spring.datasource.url, instead of building a different image per environment.
  5. Check. docker exec orders getent hosts postgres prints the database’s IP, and the service’s health endpoint reports the database as UP.
docker network create app-net
docker run -d --name postgres --network app-net -p 127.0.0.1:5432:5432 \
  -v pgdata:/var/lib/postgresql/data -e POSTGRES_DB=orders -e POSTGRES_PASSWORD=secret postgres:17
docker run -d --name orders --network app-net -p 8080:8080 \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/orders \
  -e SPRING_DATASOURCE_USERNAME=postgres -e SPRING_DATASOURCE_PASSWORD=secret orders:1.4.2
curl -s localhost:8080/actuator/health        # {"status":"UP"} with the db component UP
One network, the database by name, the URL from the environment

How to answer

  • Writable layer for scratch files only; it dies with docker rm.
  • Named volumes for data the app owns, such as a database's files.
  • Bind mounts tie a container to the host's paths, fine on a laptop, fragile in production.
  • docker rm -v or docker volume prune deletes volumes; plain docker rm keeps named ones.

Common mistakes

  • Writing uploads or a database into the container's filesystem and losing them on the next deploy.
  • Assuming a volume is backed up or replicated because it survives a restart.

Follow-up questions

What does -p 8080:80 do, and how is it different from EXPOSE?

-p 8080:80 publishes the container's port 80 on port 8080 of the host: Docker sets up NAT (network address translation) rules so traffic to the host's port 8080 reaches the container. The first number is the host port, the second the container port. EXPOSE in a Dockerfile only documents which port the app listens on; it publishes nothing. Containers on the same network reach each other on container ports without any mapping.

  • -p 127.0.0.1:5432:5432 publishes only to the host itself, not the whole network.
  • -P publishes every exposed port on a random host port.
  • Two containers can both use port 80 inside; only host ports must be unique.

See it in the infographic: Host, bridge network and volumes

How do two containers talk to each other?

Put them on the same user-defined network, created with docker network create or by Compose. Docker gives that network an embedded DNS (Domain Name System) server, so each container is reachable by its name, or a network alias, on its container port: orders connects to postgres:5432. The default bridge has no name resolution, so containers there must use IP addresses, which change. Containers on different networks cannot reach each other at all unless one joins both.

  • Use the container port, not the published host port.
  • Networks isolate groups of containers, such as a frontend network and a backend network.
  • The host network mode shares the host's network stack; none disables networking.

See it in the infographic: Host, bridge network and volumes

A service works from your IDE but fails in a container with "Connection refused" to localhost:5432. Why?

Because a container has its own network namespace. From the IDE, localhost is your machine, where the database's published port 5432 listens. Inside the container, localhost is the container itself, and nothing listens on 5432 there. Run both containers on one user-defined network and point the service at the database container's name and container port, postgres:5432, passed through an environment variable such as SPRING_DATASOURCE_URL.

  • Published ports are for the host and outside callers, not for container-to-container traffic.
  • Keep connection details in configuration, so one image runs everywhere.
  • Check with getent hosts or nslookup from inside the container.

See it in the infographic: Host, bridge network and volumes

Volume or bind mount for PostgreSQL in local development?

A named volume. Docker manages its location and permissions, it performs well on Docker Desktop, where bind mounts cross from the host into the Linux VM (virtual machine), and it is not wiped by a clean checkout. A bind mount is the right choice for files you edit, such as an init script or application.properties.

Check yourself

1. postgres runs on network app-net with -p 15432:5432. orders runs on app-net too. Which URL should orders use?
Show answers
  • jdbc:postgresql://postgres:5432/orders: Correct. Container to container, use the name on the shared network and the container port.
  • jdbc:postgresql://localhost:15432/orders: Incorrect. localhost inside orders is orders itself; 15432 is a host port.
  • jdbc:postgresql://postgres:15432/orders: Incorrect. The published port exists on the host, not on the postgres container.
2. postgres runs with -v pgdata:/var/lib/postgresql/data. You run docker rm -f postgres and start a new container with the same -v option. What happens to the tables?
Show answers
  • They are still there: Correct. The named volume outlives the container, and the new one mounts the same data.
  • They are gone with the container: Incorrect. Only the writable layer is deleted; the volume is not.
  • They come back read-only: Incorrect. The volume mounts read-write unless you add :ro.