我需要添加几行到一个 Docker 图像的 /etc/sysctl.conf。
/etc/sysctl.conf
有没有一种幂等的方法可以通过 Dockerfile而不是手动编辑和使用 docker commit方法来实现这一点?
Dockerfile
docker commit
sed work pretty well to replace stuff, if you need to append, you can user double redirect
sed
sed -i 's/origin text/new text/g' /etc/sysctl.conf bash -c 'echo hello world' >> /etc/sysctl.conf
-i is a non-standard option of GNU sed for inline editing (alleviating the need for dealing with temporary files).
-i
The s is the substitute command of sed for find and replace
s
The g means global replace i.e. find all occurrences of origin text and replace with new text using sed
g
origin text
new text
I would use the following approach in the Dockerfile
RUN echo "Some line to add to a file" >> /etc/sysctl.conf
That should do the trick. If you wish to replace some characters or similar you can work this out with sed by using e.g. the following:
RUN sed -i "s|some-original-string|the-new-string |g" /etc/sysctl.conf
However, if your problem lies in simply getting the settings to "bite" this question might be of help.