Junior — Middle
How to connect an external directory or file to a Docker container during its startup?
sobes.tech AI
Answer from AI
To connect an external directory or file to a Docker container at startup, the -v (volume) option is used in the docker run command.
Syntax:
docker run -v /path/on/host:/path/in/container image_name
Example of connecting a directory:
docker run -v /home/user/data:/app/data myapp
This mounts the local folder /home/user/data into the container at /app/data.
To connect a single file:
docker run -v /home/user/config.yaml:/app/config.yaml myapp
Thus, the container gains access to the necessary files or folders from the host without needing to copy them into the image.