Docker Volume配置文件创建及容器文件覆盖问题咨询
Hey there! Let's work through your two questions clearly, since you're just getting started with Docker and your AngularJS project.
Question 1: Create and populate appConfig.json in your volume path
First, a quick note: directly modifying paths under /var/lib/docker works but isn't the most portable approach (Docker manages this directory internally, and its structure can vary across environments). I'll share two methods—one direct host command, and a more Docker-native way.
Method 1: Direct host command (requires sudo)
Since your volume's data directory is /var/lib/docker/volumes/volumetest/_data, you can create the file and write valid JSON (note: your original snippet isn't valid JSON—keys and string values need double quotes) with these commands:
# Create the directory if it doesn't exist (Docker usually creates it when the volume is made, but this covers edge cases) sudo mkdir -p /var/lib/docker/volumes/volumetest/_data # Write valid JSON content to the file echo '{ "baseUrl": "http://localhost:8080/" }' | sudo tee /var/lib/docker/volumes/volumetest/_data/appConfig.json
Method 2: Docker-native approach (recommended)
This avoids messing with Docker's internal directories and works across all systems:
# Run a temporary Alpine container, mount your volume, and write the file docker run --rm -v volumetest:/volume alpine sh -c 'echo '\''{ "baseUrl": "http://localhost:8080/" }'\'' > /volume/appConfig.json'
The --rm flag cleans up the container immediately after it runs, so you don't leave unused containers cluttering your system.
Question 2: Will the Dockerfile COPY directive overwrite your volume file?
Short answer: No, and your current COPY path is incorrect—here's why:
- Dockerfile instructions run during the image build process, not when you start a container. The path
/var/lib/docker/volumes/volumetest/_datais a path on your host machine, not inside the Docker image you're building. So yourCOPYline would try to copy your localappConfig.jsoninto the image's/var/lib/docker/volumes/volumetest/_datadirectory (which doesn't exist by default), and this has no impact on the host's volume content.
What you should do instead
To use your volume's config file in the container, you need to:
- In your Dockerfile, copy the default config into the container's application path (not the host's volume path). For example, if your AngularJS app lives at
/appin the container:COPY src/assets/conf/appConfig.json /app/src/assets/conf/appConfig.json - When you run the container, mount your
volumetestvolume to the same config path inside the container. This will replace the image's default config with the one from your volume:docker run -p 8000:8080 -d --name cm -v volumetest:/app/src/assets/conf testApp:1.0
In this setup, the volume's appConfig.json will overwrite the container's config file at the mounted path—not the other way around.
内容的提问来源于stack exchange,提问作者user1999453

