Docker Compose can use volumes
keywords to mount directories and map host directories or files to containers.
grammar:
volumes: - <host directory>:<container directory>
Among them, <宿主机目录>
the host directory path to be mapped can be a relative path or an absolute path; the <容器目录>
target path mounted in the container can also be a relative path or an absolute path.
Example:
Mount the host's current directory to the container's /app
directory:
version: "3" services: web: image: nginx volumes: - .:/app
In the above example, web
the service uses the Nginx image, and maps the current directory (that is, the directory where docker-compose.yml is located) to the /app
directory in the container. At this point, /app
the directory can read and write files in the current directory of the host.
Note that in the Windows system, due to the different file systems, absolute paths need to be used for mounting. For example:
version: "3" services: web: image: nginx volumes: - C:\Users\myuser\project:/app
The above example maps a local C:\Users\myuser\project
directory into the container's /app
directory.