Docker Community Forums

Share and learn in the Docker community.

Rename a file on an Docker Image

Hi,

I have created an docker image of a website from a build. This image is a website with a nodejs webapp and a .net core website. Both have configs (config.json and web.config respectively).

I have packaged up the image to include other environment configs so I can use an image for a particular environment (QA, Stage, Production, etc) but with a rename of web.config.ENVIRONMENT to web.config and Config.JSON.ENVIRONMENT to Config.JSON.

How can I rename a file on a docker image or rename both of these files? A rename works or a copy config.json.QA to config.json works as well.

Do I need to mount the image, make the config file changes, then start the image in a container?

thanks in advance.

AFAIK the best and most standard way to modify a Docker image is to use a Dockerfile and “docker build”.
Can’t you write “RUN mv …” line in a Dockerfile, or don’t you have a mv command in the image?

Thanks for the response. I didnt want to have to rebuild the entire image just for a web.config or config.json change for a different environment.

at this point to change a file, i am doing a docker container stop, docker cp and docker container start.

I didnt know if there is a better way.

Nice compilation here! Thnx for sharing.

Why not, containerised applications are meant to be immutable not patched in-situ, that’s an anti-patten. If you have different config per environment, mount the appropriate config using a volume.

Yeah i agree Goffinf. Hopefully a quick question on this.

Do I mount a folder that would have the file or do I mount just the file?

Being a little new on this… are there any examples.

I want to take a web.config.QA and make it web.config for the application container when run in QA.

Just a single file is sufficient. You can mount the file with a different name inside the container.

can you give me a example of running the image while mounting the file?
I have a image/ container that has a site running in the /app folder

so there is an /app/web.config
I want to use that same image but place web.config.qa as web.config
this would allow me to reuse the same image for different environments with just this file changing.

docker run -v /local/web.config.qa:/app/web.config -it -p 8080:8080 myimage

is that correct?

If the file web.config.qa is located in /local on your host and the target file inside the container should be located in /app/web.config, then -v /local/web.config.qa:/app/web.config is correct.

thanks Meyay,

Does that leave an opening into the container for anybody that wants to change that web.config can simply change the /local/web.config.qa?

Just curious

Earlier I was doing a docker create container from image
docker cp /local/web.config.qa container:/app/web.config
docker start

What do you mean by “Does that leave an on opening into the container for anybody that wants to change that web.config”?

Does it leave a security hole where somebody could change /local/web.config.qa or even delete it and it has an impact on the container that has a volume bind there?

If you don’t protect the file accordingly, then yes.

How would you protect that file in that situation other than security to the file system /local/web.config.qa

I just tried this and I must be doing something wrong

I have my configs in a folder named e:\configs

docker run -d -p 9090:80 --name QAAAuthService -v E:/configs/appsettings.QAA.json:/app/appsettings.json qaaauthservice

docker: Error response from daemon: invalid volume specification: ‘E:/configs/appsettings.QAA.json:/app/appsettings.json’.

I also tried

docker run -d -p 9090:80 --name QAAAuthService -v /configs/appsettings.QAA.json:/app/appsettings.json qaaauthservice

same error

I am using Docker Desktop for WIndows

I wonder if this is a new feature for docker for windows. I am using and older version 2.0.0.3

I tried this as well.

docker run -d -p 9090:80 --name QAAAuthService -v e:\configs\appsettings.QAA.json:c:/app/appsettings.json qaaauthservice

docker: Error response from daemon: invalid volume specification: ‘e:\configs\appsettings.QAA.json:c:/app/appsettings.json’: invalid mount config for type “bind”: source path must be a directory.

Can you only mount directories with Docker Desktop for Windows?

On Linux, it works like described.
On Windows: I have no idea

I have also tried this approach and on the latest version of docker desktop for windows.

docker run -d -p 9090:80 --name QAAAuthService --volume type=bind,src=e:\configs\appsettings.QAA.json,dst=/app/appsettings.json qaaauthservice

here is my docker version details

C:>docker version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:17:08 2019
OS/Arch: windows/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.1
API version: 1.40 (minimum version 1.24)
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:25:07 2019
OS/Arch: windows/amd64
Experimental: false

Ok it looks like a file to file mapping is not supported in Docker Desktop for windows the docker forums on github. The folder to folder volume mapping is supported.

so this is supported where c:\configs is a folder on the windows host and /app/config is a folder on the container image

docker run -v c:/configs:c:/app/config -d -p 9090:80 qaaauthservice

this is NOT supported on windows docker

docker run -v c:/configs/appsettings.QAA.json:c:/app/config/appsettings.json -d -p 9090:80 qaaauthservice