A volume is a specially-designated directory within one or more containers that bypasses the Union File System. Volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically deletes volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container. Also known as:
Volumes are often "bind-mounted", ie a specific directory on your local filesystem is mounted as a volume in a container.
To bind-mount a local directory as a volume, use the
docker run -v option:
docker run -d -v `pwd`:/var/www/html IMAGE
This will bind-mount the current local directory as
/var/www/html in the container.
Environment variables can be passed to the container at runtime. To be useful, the image must support these environment variables. Typically, the image documentation describes what environment variables it supports.
For example, the official
mysql image supports, among other things, the
MYSQL_ROOT_PASSWORD environment variable, which sets the mysql server's root password.
"Hello World!" via nginx with volume
Create a basic web page displaying "Hello World!". Run it as the default page for a nginx web server using:
- the latest official nginx image
- the default port (80)
- a volume bind-mounted to the local directory.
Run mysql server using the latest official mysql image on the default port (3306). Use environment variables to create:
- with password