This is possible but only with volume
mounts: docker
and thus docker-compose
distinguishes between (named) volumes
and bind mounts
, most notably:
- New volumes can have their content pre-populated by a container.
With bind mounts
you specify an absolute path to a directory or file on the docker
host to be bind-mounted into the container.
Technically volumes
do the same with the difference that the source directory/file on the host is completely managed by docker
, i.e. you can create/list/delete them with the docker volume
command. This allows docker
to provide additional features for those volumes
, e.g. to populate the volume with the contents of the container image at the target directory in the image when the volume is first created.
In contrast using a bind mount
will always override/hide the contents of the target path.
To use a named volume use this docker-compose.yml
version: "3"
services:
ltb:
image: danielemaddaluno/self-service-password
ports:
- 8080:80
volumes:
- conf:/var/www/html/conf/
volumes:
conf:
This will automatically create a new named volume conf
and populate it with the contents of /var/www/html/conf/
of the danielemaddaluno/self-service-password
when you first start it - i.e. subsequent runs will reuse the existing volume and not copy the contents again!
If using a volume
is not feasible for you but you must use a bind mount
you'll have to manage the initialization/population of the volume yourself, e.g. by using an entrypoint script to copy files from the container into an empty volume.
docker-compose up
on your suggesteddocker-compose.yml
(the one withvolumes: conf:
) but no folder it's created... Am I missing something? Where is the new named volume conf located? Can I locate it in the same folder of the yaml as I did with my yaml? – Troll