kshnkvn
kshnkvn

Reputation: 966

How to edit yaml file in Dockerfile or in shell?

In Dockerfile, I upload one configuration file from curl, which I then need to edit a little, here is an example structure:

client:
  ...
server:
  applicationConnectors:
  - type: http
    port: 8989
    # for security reasons bind to localhost
    bindHost: localhost
  requestLog:
      appenders: []
  adminConnectors:
  - type: http
    port: 8990
    bindHost: localhost

I need to edit both bindHost parameters, it would be desirable if I could pass their values as a parameter during docker build, but I didn’t seem to find anything on the Internet or in the documentation.

Upvotes: 0

Views: 6055

Answers (2)

Exadra37
Exadra37

Reputation: 13104

I need to edit both bindHost parameters, it would be desirable if I could pass their values as a parameter during docker build, but I didn’t seem to find anything on the Internet or in the documentation.

You can use the ARG directive in the Dockerfile and pass a value to it with --build-arg.

Dockerfile ARG:

ARG <name>[=<default value>]

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg = flag. If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning.

[Warning] One or more build-args [foo] were not consumed.

A Dockerfile may include one or more ARG instructions. For example, the following is a valid Dockerfile:

FROM busybox
ARG user1
ARG buildno

Docker Build Args:

Set build-time variables (--build-arg)

You can use ENV instructions in a Dockerfile to define variable values. These values persist in the built image. However, often persistence is not what you want. Users want to specify variables differently depending on which host they build an image on.

A good example is http_proxy or source versions for pulling intermediate files. The ARG instruction lets Dockerfile authors define values that users can set at build-time using the --build-arg flag:

$ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 --build-arg FTP_PROXY=http://40.50.60.5:4567 .

This flag allows you to pass the build-time variables that are accessed like regular environment variables in the RUN instruction of the Dockerfile. Also, these values don’t persist in the intermediate or final images like ENV values do. You must add --build-arg for each build argument.

Using this flag will not alter the output you see when the ARG lines from the Dockerfile are echoed during the build process.

For detailed information on using ARG and ENV instructions, see the Dockerfile reference.

You may also use the --build-arg flag without a value, in which case the value from the local environment will be propagated into the Docker container being built:

$ export HTTP_PROXY=http://10.20.30.2:1234
$ docker build --build-arg HTTP_PROXY .

Upvotes: 0

Jan Held
Jan Held

Reputation: 654

You can create an .env file which then contains the things you like to have as variables, like this:

BINDHOST=localhost

And in your docker-compose.yml you write:

client:
  ...
server:
  applicationConnectors:
  - type: http
    port: 8989
    # for security reasons bind to localhost
    bindHost: ${BINDHOST}
  requestLog:
      appenders: []
  adminConnectors:
  - type: http
    port: 8990
    bindHost: ${BINDHOST}

Find out more about it here: https://docs.docker.com/compose/environment-variables/

Hope this helps.

Upvotes: 1

Related Questions