Modify a JSON Configuration File with jq

Joel Lord
InstructorJoel Lord
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

This lesson uses some advanced concepts and some bash scripting to build a better Dockerfile. By using the concepts showed here, you will be able to build more future-proof and production-ready containers. If you prefer to focus on the content about containers, feel free to ignore this lesson and come back to it later on if needed.

Currently, we use sed to overwrite the hard-coded path to the API. This works well when there is a single value to replace. Now if one of your team members replaced the value of localhost:3000, your build will break.

We'll use jq to fix this issue. Jq is a command-line tool that makes it easy to change values in a JSON file. The base image that we used for building the application does not contain this tool, but that is not an issue. To install jq, you will need to download the binary, we'll need that to execute jq commands in our Dockerfile. This will allow us to update specific properties i

Instructor: [0:00] You saw how to use sed to overwrite the hard coded path to the API. This works well when there's a single value to replace. Now, if one of your team members replaced the value of localhost:3000, your build will break.

[0:14] There is a better solution to this, but it involves adding a new software into that first container. To change this value, we will use jq. jq is a command-line tool that makes it easy to change values in a JSON file.

[0:31] The base image that we used for building the application does not contain this tool. That is not an issue, because a container is essentially just like a Linux machine. You can install software in your image and use this tool to perform various operations.

[0:48] To install jq, you will need to download the binary. To make it easier to eventually upgrade if there is a security vulnerability of some sort, you can add the version number as an environment variable.

[1:02] You can specify this variable with the E and V keyboard, jq version equals 1.5. Next, you can download jq from the GitHub repository as shown below. Note how the jq version environment variable is used in the URL.

[1:23] Jq will be downloaded using a Wget which is already installed in the base image. You will also need to specify Wget to ignore certificates, add the URL to the GitHub repository, and then you need to tell Wget to output this URL into a file in this /temp folder. Note that this is a capital O.

[1:55] Next, you will copy this binary file into the /USR/bin folder. Finally, you'll need to change the permissions on this file to make it executable. Now that jq is installed, you will be able it to change the value of the base URL property in the config.json file.

[2:18] You can do so with another run statement. First, we will execute the following command. We will tell jq to change the property-based URL in our config JSON file to the value dollar-based URL.

[2:37] This is done in the config.json file. We will then store the output of this command into the contents variable, and we will echo that contents into the config.json file to overwrite the config.json with new configuration. Finally, you can remove that sed command, as we don't need it anymore.

[3:03] The next thing with jq is that you only change the value of one of the JSON properties. If your developer team added more configurations to this file, the script in the Dockerfile won't change the rest of it. This makes it a safer way to change only the values that need to change in the configuration.

[3:22] You could also add more jq commands to overwrite other values in the config.json file, should you need to overwrite them, with environment variables. This makes your Dockerfile a little bit more robust, more future-proof, and more ready to be used in production.

Jose Hernandez
Jose Hernandez
~ 4 years ago

For it to work, I had to remove the spaces around the first assignment operator in the statement RUN contents = $(jq '.BASE_URL = "$BASE_URL"' config.json) && echo ${contents} > config.json.

Markdown supported.
Become a member to join the discussionEnroll Today