- Section 1: Prerequisites
- Section 2: Core Workflow
- Section 3: Advanced Operations
- Section 4: Volume Management
- Section 5: Cleanup
Author: Md Toriqul Islam
Description: Reference commands for sharing log files between Docker containers using bind mounts and volumes.
Learning Focus: Advanced Docker volume concepts, multi-container data sharing, and volume management.
Note: This is a reference guide. Exercise caution and understand the implications before executing any commands.
- Docker 20.10.17 or higher
- Alpine Linux 3.16 or compatible
# Build the custom Docker image
docker build -t my_writer .
# Create a directory on the host to store logs
LOG_SRC=./web-logs-example
mkdir -p ${LOG_SRC}
# Run a container that writes logs to the bind-mounted directory
docker run --name plath -d \
--mount type=bind,src=${LOG_SRC},dst=/data \
my_writer
# Verify the container is running
docker ps -f name=plath
# Run a container that reads logs from the bind-mounted directory
docker run --rm \
--mount type=bind,src=${LOG_SRC},dst=/data \
alpine:latest \
head /data/logA
# Display the contents of the log file on the host
cat ${LOG_SRC}/logA
# Stop and remove the log-writing container
docker rm -f plath
# Create a named Docker volume for log sharing
docker volume create --driver local logging-example
# Verify the volume creation
docker volume ls
# Run a container that writes logs to the Docker volume
docker run --name plath -d \
--mount type=volume,src=logging-example,dst=/data \
my_writer
# Verify the container is running
docker ps -f name=plath
# Run a container that reads logs from the Docker volume
docker run --rm \
--mount type=volume,src=logging-example,dst=/data \
alpine:latest \
head /data/logA
# Display the contents of the log file in the Docker volume
docker exec plath cat /data/logA
# Stop the log-writing container
docker stop plath
# Create a container with anonymous volumes
docker run --name fowler \
--mount type=volume,dst=/library/PoEAA \
--mount type=bind,src=/tmp,dst=/library/DSL \
alpine:latest \
echo "Fowler collection created."
# Create another container with anonymous volumes
docker run --name knuth \
--mount type=volume,dst=/library/TAoCP.vol1 \
--mount type=volume,dst=/library/TAoCP.vol2 \
--mount type=volume,dst=/library/TAoCP.vol3 \
--mount type=volume,dst=/library/TAoCP.vol4.a \
alpine:latest \
echo "Knuth collection created."
# Create a container that uses volumes from other containers
docker run --name reader \
--volumes-from fowler \
--volumes-from knuth \
alpine:latest ls -l /library/
# Display detailed information about the container's volumes
docker inspect --format '{{json .Mounts}}' reader | jq
# List all volumes
docker volume ls
# Remove a specific volume by name
docker volume rm <volume_name>
# Remove all unused volumes
docker volume prune
# Remove all volumes forcefully
docker volume rm $(docker volume ls -q)
# Stop and remove all containers
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
# Remove all volumes
docker volume rm $(docker volume ls -q)
💡 Best Practice: Use meaningful names for containers and volumes to facilitate management and organization.
⚠️ Warning: Be cautious when forcefully removing all volumes, as it can lead to data loss.
📝 Note: Always verify the contents of a volume before removing it to avoid unintended data loss.