[Docker] Modify a JSON Configuration File with jq
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
Let's say the config.json file contains the endpoint frontend needs, and we change it to:
{ "BASE_URL": "$BASE_URL" }
Dockerfile:
FROM node:14 AS builder COPY . /app WORKDIR /app ## add jq ENV JQ_VERSION=1.5 RUN wget --no-check-certificate https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64 RUN cp /tmp/jq-linux64 /usr/bin/jq RUN chmod +x /usr/bin/jq RUN contents="$(jq '.BASE_URL = "$BASE_URL"' config.json)" && echo ${contents} > config.json RUN npm install RUN npm run build FROM nginx:1.17 WORKDIR /usr/share/nginx/html ## Copy the file from first step into current working dir COPY --from=builder /app/dist .
Run:
docker run -d --rm --name front -p 8888:80 -e BASE_URL=http://localhost:3000 k8scourse-front
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-02-01 [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT
2018-02-01 [MST] Create Dynamic Types and use Type Composition to Extract Common Functionality
2017-02-01 [TypeScript] Use the never type to avoid code with dead ends using TypeScript
2016-02-01 [Regex Expression] Use Shorthand to Find Common Sets of Characters
2016-02-01 [Regex Expression] Find Sets of Characters
2016-02-01 [Regular Expressions] Find Repeated Patterns
2016-02-01 [Regular Expressions] Find Plain Text Patterns