Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Docker Volumes & Data Management
40%

Bind Mounts

A bind mount is a file or directory located anywhere on the host filesystem that is mounted into a container. It's defined by its source path and the container path to which it is mounted. However, bind mounts are not the ideal solution for creating volumes because they are not portable. This lack of portability arises from their reliance on the host filesystem. Additionally, bind mounts are not managed by Docker itself, but rather by the user.

This is an example showing how to use a bind mount with a PostgreSQL container:

# Create a directory on the host
mkdir -p /data/my_bind_mount

# Run a container with the bind mount
docker run --name postgres-with-bind-mount \
    -v /data/my_bind_mount:/var/lib/postgresql/data \
    -e POSTGRES_PASSWORD=mysecretpassword -d \
    postgres:16

# List the contents of the bind mount directory on the host
ls -l /data/my_bind_mount

# Create a table in the database
docker exec -it postgres-with-bind-mount psql -U postgres \
    -c "CREATE TABLE test (id SERIAL PRIMARY KEY, name VARCHAR(50));"

# Insert data into the table

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More