0

In my case, I am creating a config.json that I need to copy from the host to my container.

I figured out there is some option that I can pass args to my dockerfile. so first step is :

1.create Dockerfile:

FROM golang
WORKDIR /go/src/app
COPY . .                     /* here we have /foo directory */
COPY  $CONFIG_PATH ./foo/
EXPOSE $PORT
CMD ["./foo/run", "-config", "./foo/config.json"]

as you can see, I have 2 variable [ "$CONFIG_PATH", "$PORT"].

so these to variables are dynamic and comes from my command in docker run. here I need to copy my config file from my host to my container, and I need to run my project with that config.json file.

after building image:

  1. second step:

get my config file from user and run the docker image with these variables.

let configFilePath = '/home/baazz/baaaf/config.json'
let port = "8080"
docker.run('my_image', null, process.stdout, { Env: [`$CONFIG_PATH=${configFilePath}`, `$PORT=${port}`] }).then(data => {

        }).catch(err => { console.log(err) })

I am getting this error message when I am trying to execute my code.

Error opening JSON configuration (./foo/config.json): open ./foo/config.json: no such file or directory . Terminating.

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • The vars in your Dockerfile as you show them are used during `build` not `run`. Since it is for `copy` and `expose` only, there is absolutely no need to pass them to your docker container. Did you inspect your image file system to check if the config file has been copied to the correct path ? I suspect it has been copied to a subdir (i.e. the dirname of `CONFIG_PATH`). – Zeitounator Dec 04 '19 at 17:13

1 Answers1

0

You generally don’t want to COPY configuration files like this in your Docker image. You should be able to docker run the same image in multiple environments without modification.

Instead, you can use the docker run -v option to inject the correct config file when you run the image:

docker run -v $PWD/config-dev.json:/go/src/app/foo/config.json my_image

(The Dockerode home page shows an equivalent Binds option. In Docker Compose, this goes into the per-container volumes:. There’s no requirement that the two paths or file names match.)

Since file paths like this become part of the external interface to how people run your container, you generally don’t want to make them configurable at build time. Pick a fixed path and document that that’s the place to mount your custom config file.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • tnx for answer I get the point here but there is an problem: in docker file: VOLUME /test and in my run command : -v `${configFilePath}:/test/config.json` but get this error:container_linux.go:346: starting container process caused "exec: \"-v\": executable file not found in $PATH": unknown – Babak Abadkheir Dec 05 '19 at 09:54
  • Don't put `VOLUME` in your Dockerfile; it mostly only has confusing side effects and is unnecessary. Put the `-v` option before the image name and not after it. – David Maze Dec 05 '19 at 11:08