Modify a JSON Configuration File with jq

Joel Lord
InstructorJoel Lord

Share this video with your friends

Send Tweet

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

Jose Hernandez
Jose Hernandez
~ a month 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.