Stop Generating Multiple Docker Images After Code Updates

Hello,

I’m currently managing a Dockerized application on my VPS, where the source code is hosted on my VPS. Whenever I make changes to the source code, I find myself constantly rebuilding the Docker image using the Dockerfile. This process, however, results in multiple Docker images being generated, which isn’t optimal for my development workflow.

I’m seeking advice on how to streamline this process to avoid creating unnecessary Docker images.

Thank you.

Instead of copying the changed source files into a new Docker image (via Dockerfile), you can usually just “bind mount” the source folder into the runtime container.

Your mileage may vary, depending on the language you use.

how to “bind mount” the source folder into the runtime container.

please update.

By using a docker-compose.yml file, you have a property called volumes (Volumes top-level elements | Docker Docs). You just need to mount your current folder to where it should in the container.

Like that, changes done locally will be synchronized with the container.

Please review the Dockerfile.

The Dockerfile is located in the project’s root directory, and I use the following command to build the Docker image:

./gradlew build # With JDK 17 installed
docker build .

After a successful build of the Docker image from the source code, I use the following command to tag the Docker image:

docker tag cda504be51ab s-pdf:latest

after give tag I run the Docker image container with this command:

docker run -d -p 8200:8080 --name s-pdf s-pdf:latest

The Docker image is now operational on my server’s IP and port. However, I encounter the issue that whenever I make new changes to the source code, I have to rebuild the Docker image again using the Dockerfile located in the project root directory.

I manually delete the previously running container and image using the following command:
docker rmi 2159208c0484

how can I make changes to the already built Docker image without having to create and build a new Docker image everytime.

Thank you.

There is a contradiction here. You basically want to create a new image without creating a new image as there is no way to change an existing image. All you can do is replace some layers, but that means you will have a new image and a “dangling” image from the old layers. If you want to clean up the filesystem, you can run

docker image prune

which deletes dangling images.

What @cavo789 suggested is a good way for development without changing the image, as you don’t care about the image, only the container. There is also the compose watch feature which can automatically rebuild and synchronize without bind mounts.