Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing files from dockerfile

I need to add several lines to /etc/sysctl.conf in a Docker image.

Is there an idempotent way to do this via a Dockerfile rather than editing manually and using the docker commit approach?

like image 246
Myles McDonnell Avatar asked Dec 30 '14 22:12

Myles McDonnell


People also ask

Can docker edit images?

In many scenarios, users need to edit these images to suit their needs. For customizing or tweaking a docker image to specific requirements, we edit this docker image. But Docker has a drawback that an image cannot be directly edited or modified.


2 Answers

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.

like image 125
wassgren Avatar answered Sep 27 '22 22:09

wassgren


sed work pretty well to replace stuff, if you need to append, you can user double redirect

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).

The s is the substitute command of sed for find and replace

The g means global replace i.e. find all occurrences of origin text and replace with new text using sed

like image 38
creack Avatar answered Sep 27 '22 21:09

creack