Ports and Volumes
Publish a port
Containers have their own network namespace. -p maps a host port to a container port so browsers and tools on your machine can connect.
docker run -d --name web -p 8080:80 nginx
Why volumes matter
Files written only inside a container disappear when that container is removed. A volume or bind mount stores data on the host or in Docker-managed storage.
Bind-mount a folder
This example serves files from a local site directory. Paths must exist on the host.
mkdir -p site
echo '<h1>Hello from a volume</h1>' > site/index.html
docker run -d --name static -p 8081:80 \
-v "$(pwd)/site:/usr/share/nginx/html:ro" nginx
Name a volume
Named volumes are handy for databases. Docker creates and manages the storage location.
docker volume create app-data
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v app-data:/var/lib/postgresql/data \
postgres:16
Comments
One comment per signed-in account. Comments are saved with this page’s URL.