Containers are temporary by nature. Delete a container, and the data inside its filesystem can vanish along with it. If you want to keep database data, uploaded files, config files, or development source code outside the container, Volumes or bind mounts are what you use.
dockerfile
# Create a named volume
docker volume create app-data
# Mount the named volume into a container
docker run --name data-demo -v app-data:/app/data alpine sh -c "echo saved > /app/data/message.txt"
# Read the saved file with a new container
docker run --rm -v app-data:/app/data alpine cat /app/data/message.txtapp-data named volume is created and mounted at /app/data inside the container. When you read it from a new container, you'll see the file is still there.
You should see
You'll see 'saved' printed in the terminal. Even after deleting the container and reading from a new one, the data will still be there — proving it survived.Info
If you want live updates to your source code during development, bind mount -v ./src:/app/src is commonly used. If you don't want to lose database data, a named volume is the better choice.