- Yair Mark
Today I had to containerize a number of API and UI applications. They were basically the same with some variations to cater for different business use cases. I started by trying to containerize one of them. After getting it working I had about a 40 line dockerfile. The problem was that about 30 of those lines would be identical for the other apps too.
I resolved this by creating a base/parent dockerfile and used that as the child's base image to remove the redundant parts. The process I used to do this was:
- Create a dockerfile with the common pieces in
- Build and push this to your container registry
- In the child dockerfile set your
FROMto what you tagged the parent file
- Put the child specific details in the rest of the child dockerfile
This approach is also really cool as you can even have the parent dockerfile and its dependencies live in a separate repository as it gets built and pushed to your container registry which is accessible from the child file.
The exact steps I used are:
- Build and tag the parent dockerfile
docker build -t yourContainerRegistryDomain/containerRegUserName/some-container-parent -f Dockerfile.api.parent .
- Push the built container to your container registry
docker push yourContainerRegistryDomain/containerRegUserName/some-container-parent:latest
- Use the parent image as your
FROMin the child
FROM yourContainerRegistryDomain/containerRegUserName/some-container-parent:latest ...
This pattern works really well but should only be used for the common boiler plate dependencies and setup. The child should be very focused on what the child container does.