Somethingwhatever
Somethingwhatever

Reputation: 1348

How can I pass a config file to a docker container?

I am trying to setup mysql_exporter for prometheus using docker : https://github.com/free/sql_exporter.

I went to their official docker image here and ran the following command :-

docker pull githubfree/sql_exporter

When i do sudo docker images i see the following output :-

REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
githubfree/sql_exporter   latest    a1de4d8de942   19 months ago   23.6MB

When i run the following command :-

sudo docker run -it <image-id>

I am getting the following error :-

I0719 00:43:49.475797       1 config.go:18] Loading configuration from sql_exporter.yml
F0719 00:43:49.477036       1 main.go:56] Error creating exporter: open sql_exporter.yml: no such file or directory

So i created the required sql_exporter.yml config file. But my question is how do I run the docker container using that config file? Any help will be appreciated. My OS is ubuntu 18.04.

Upvotes: 0

Views: 4612

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25189

The default location for the config file in the container is /sql_exporter.yml. So if you map your config file there, it can find it. Something like

docker run --rm -v $(pwd)/sql_exporter.yml:/sql_exporter.yml:ro githubfree/sql_exporter

Please note that the host path to the file has to be absolute. If it isn't, Docker maps it to a directory inside the container rather than a file and it doesn't work.

The image seems very rudimentary and if you need to map collector files, you'll need to map them individually which is going to be a hassle. So if you need to use this a lot, you could create your own image that's a bit easier to use. The main drawback of the image is that it uses the root directory as its working directory.

To create an image with a different working directory you could create a Dockerfile like this

FROM githubfree/sql_exporter
WORKDIR /config_files

Then you could have your config file and collector files in your current host directory and map them all in one go with

docker build -t my_sql_exporter .
docker run --rm -v $(pwd):/config_files:ro my_sql_exporter

Upvotes: 2

Related Questions