Autogenerate Dockerfile from ubuntu image

I was learning docker to use in one of my projects. I kept installing packages, finally created required Ubuntu image. Suddenly thought it would be cool if I can generate Dockerfile like pip freeze > requirements.txt.

sudo docker run ubuntu dpkg --get-selections | awk '{print $1}' >  base.list && sudo docker run pyextra dpkg --get-selections | awk '{print $1}'> pyextra.list && sort base.list pyextra.list | uniq -u > op.list && python -c "f = open('Dockerfile', 'w');f.write('from ubuntu\n');f.write('RUN apt-get update\n');for line in open('op.list').readlines():f.write('run apt-get install -y {0}'.format(line));"

Breakdown

Start the docker in daemon mode, then execute following commands.

sudo docker run ubuntu dpkg --get-selections | awk '{print $1}' > base.list

Displays all packages installed in ubuntu base, extract the package name and write to base.list file.

sudo docker run pyextra dpkg --get-selections | awk '{print $1}'> pyextra.list

pyextra is docker image name. Get all installed packages names and write to pyextra.list.

sort base.list pyextra.list | uniq -u > op.list

Find the packages not in base distro and add to op.list. Now freshly installed package names are available.

python -c "f = open('Dockerfile', 'w');
f.write('from ubuntu\n');f.write('RUN apt-get update\n');
for line in open('op.list').readlines():f.write('run apt-get install -y    {0}'.format(line));

Convert op.list file into Dockerfile.

Disadavantages

  • This method produces dependencies as packages. apt-get install vim will depend on vim-common, vim-common is specified as package.

Is it worth to create a library which will do this ?

See also

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Get new posts by email: