Anil
Anil

Reputation: 1765

Docker How to modify an existing image

I created an image in docker. Installed Ubuntu, JDK in it and couple of other libraries. Then I exited the container and did a commit like this

docker commit a7b95082f6ea anil-ubuntu

I started the container again

docker run -p 5901:5901 -t -i anil-ubuntu

This time I installed gradle and couple of other libraries. And then exited the container and did a commit again.

 docker commit a7b95082f6ea anil-ubuntu

enter image description here

Every time I do a commit a new image is created and older images with repository and tag remain. Very soon by following this workflow, I'll run out of space. Is this the right way of using docker ? How do I ensure that all these images go away.

I am using docker version 18.09.1 desktop on windows 10

Upvotes: 3

Views: 12986

Answers (1)

David Maze
David Maze

Reputation: 158847

Docker images are immutable; once you create an image you can never change it again.

The correct way to create an image is using docker build. Docker has a pretty good official tutorial on creating and running custom images; while it's Python-oriented, the basic techniques here are applicable to any language.

Using docker commit is pretty much always wrong. A Dockerfile is just a basic listing of the steps you used to build up the image – start FROM some base image, COPY in some files, RUN some commands – and it's usually just as easy to write a Dockerfile as to write out the steps involved in building up an image in text. You'll also need to work with other people on working on your image who will need this description, and for that matter when the base image you started from has a critical security fix in six months, you'll need to remember for yourself how you built it.

The workflow I generally find works is:

  1. Build and test my application, locally, without Docker involved at all.
  2. Write a Dockerfile that builds my application, without any development or test tools in it. Check that it docker runs locally and hand-test it. Add the Dockerfile to my source control repository.
  3. (optional but recommended) Set up a continuous integration server and Docker registry, so that on every commit, after the local unit tests pass, a new image is built and docker pushed.

Yes, this causes old images to back up, but this isn't really harmful. You can use docker system prune to clean them up.

Upvotes: 8

Related Questions