0

I've created a docker image from Dockerfile, and now I want to add another application to it. My question is, which is the best practice for updating the docker image:

  1. Run a container from the docker image, install the package inside the container, and then get an image from that container.
  2. Update Dockerfile with the corresponding command and rebuild the image. Which of these is the best practice?
Kamran Hosseini
  • 478
  • 5
  • 25

3 Answers3

1

I would not do any of the two points you mentioned. The reason is because of the Single Responsiblity Principle. I know this is not programming, though it is nice to have the different responsiblities (different programs and their images) separated in this case.

I would extend the image you already created by using as a base it to create a new image for the other program which will give you a new image that has the program you want to have (keeping the old DockerImage untouched).

Please see my answer about combining DockerImages here.

At the end, I cannot really tell what is the most proper solution for you, because it depends on your use case.

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • No. I mean when you're in the middle of building an application and you don't know which package exactly you are going to install on your container. After installing some packages, should I get an image from the container or go and update the docker file properly and get the image from docker file? – Kamran Hosseini Aug 22 '20 at 09:40
  • 1
    @K47, it depends if you will be updating your Docker container (starting from the image) frequently. For example let us say that you are developing a .Net Core application, and to update the software on the server you deploy a new Image with different source code every time you. What I want you to know is just the answer in this case depends on your case. But though, I still suggest combining Docker images to install multiple application in the container. Hope I could help. – Mohammed Noureldin Aug 22 '20 at 20:42
0

The idea behind containers was to have an immutable state of the container. The reason for that is so you could keep your containers (and Docker images for that matter) small and portable. If you were to install anything additional to the container, that would increase its size, which is not impossible but is a bad practice. If you would like to have some of the changes reflected in the container immediately, try with Docker volumes. If however, the package is needed for application to work, the best practice is to update the Dockerfile and rebuild the image. More about Dockerfile best practices can be found here.

Marko E
  • 13,362
  • 2
  • 19
  • 28
  • So, if I update the docker file it will increase the size of image and so the size of container too. won't it? – Kamran Hosseini Aug 20 '20 at 08:14
  • Yes, but then the image can be reused everywhere. If you update only a package inside a container, then you will go back to the good old `it works on my machine`. :) – Marko E Aug 20 '20 at 08:35
  • If I take a snapshot from the container after installing some packages on it. Won't the output image be able to run by docker command in any other Linux machine? – Kamran Hosseini Aug 20 '20 at 09:16
  • 1
    @K47 Of course, but it's much easier to distribute a Dockerfile as opposed to a Docker image – Paolo Aug 20 '20 at 09:18
0

I would suggest modifying the Dockerfile directly, this way you can keep track of all changes since hopefully it's under version control.

Paolo
  • 21,270
  • 6
  • 38
  • 69