2

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

Anil
  • 1,585
  • 2
  • 16
  • 25
  • You can delete the old images yourself with _docker rmi_ . Why don't you use a Dockerfile to install all at once? – Zak Feb 10 '19 at 13:57
  • @Zak, if I try deleting older images, I get the error Error response from daemon: conflict: unable to delete 4708b27e7571 (cannot be forced) - image has dependent child images – Anil Feb 10 '19 at 14:04
  • Maybe you should do some cleaning before. Delete all your containers, than execute `docker system prune` to remove dangling images and volumes. If that does not do the trick, take look at this https://stackoverflow.com/questions/38118791/can-t-delete-image-with-children – Zak Feb 10 '19 at 14:23

1 Answers1

3

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.

David Maze
  • 67,477
  • 12
  • 66
  • 91