Source
Sometimes, you might want to add a simple command in the middle of a Dockerfile but don’t want to rebuild the whole image again. In case this command is self-contained (i.e. doesn’t introduce downstream dependencies to existing commands later in the Dockerfile), you can create an updated image using the following procedure:
- Run a temporary container from the existing image.
- Execute the command to be added in that container in an interactive shell session.
- Commit the changed container to an image (ideally using the same name and tag as the existing one to be a drop-in replacement for running containers already using that existing image, so that they can simply be recreated using the new image).
- Recreate and restart any affected containers using this image.
- Add the command to the Dockerfile at the desired location.
For example, given the first Dockerfile from Running systemd as PID 1 in a container with the image named ubuntu-systemd:latest
and the systemcl mask
command to be added using the described procedure, you’d need to do the following:
root@svr-vm-docker-01:/# docker run --name ubuntu-systemd-temp -d ubuntu-systemd:latest
root@svr-vm-docker-01:/# docker exec -ti ubuntu-systemd-temp bash
root@d1849fe4a768:/# systemctl mask systemd-logind.service getty.service getty.target
root@d1849fe4a768:/# exit
root@svr-vm-docker-01:/# docker commit ubuntu-systemd-temp ubuntu-systemd:latest