I think it's a familiar situation when you decide to create a service, write the initial code, package it into a container, launch it in Docker or Kubernetes, and everything works. However, you need to change the code because you plan to actively develop, modify files, add new dependencies, and so on. Manually building or synchronizing files each time is time-consuming. Fortunately, the world is not standing still, and there are already several tools that can help you solve this task.
Below, I'll tell you about some of them with examples and code.
Skaffold/Tilt configurations are set up for Kubernetes. The simplest way to init a local cluster is using Docker Desktop. Go to settings and check the 'Enable Kubernetes' option, or you can use Minikube.
Docker compose watch
Perhaps it's worth starting with the simplest tool and configuration - Docker Compose Watch, which became available starting from 2.22v.
Watch can be used to track changes in files and perform one of three actions:
Sync - synchronizes changes in the file with the file in the container.
Rebuild - initiates the image-building process and replaces the already running container with a new one.
Sync + Restart - synchronizes changes and restarts the container.
Example:
In the example above, you can see a simple Docker Compose configuration, but we are interested in the watch key. Here, it is specified that if the web/package.json file changes, a rebuild should be triggered to update dependencies and restart the process. However, if something else in the web folder changes, the _sync_hronization process is initiated.
The npm start
command inside the container will detect these changes and update the service.
Running docker compose in watch mode is straightforward, you just need to execute the following command:
docker compose watch
This simple example demonstrates how easily you can launch a container and synchronize changes on the fly. For more detailed information about this feature, please refer to the official website https://docs.docker.com/compose/file-watch/
Skaffold
Next in line is Skaffold, an excellent tool that I've been using for the past two years. It works reliably, has quite extensive functionality, but for the purposes of this article, let's focus on the skaffold dev
command. This command automatically tracks changes and synchronizes them in a Kubernetes pod.
Example:
In the example above, you can see a simple configuration where the path to the Dockerfile and the path to the k8s manifest are specified. In the config, the sync type chosen is manual, indicating that we will specify the directory path to synchronize ourselves. In this case, we synchronize changes in the local web folder to the /app folder in the container.
To run in development mode (where changes are tracked and synchronized), execute the following command:
skaffold run -f deploy/skaffold.yaml --port-forward=pods
These are not all the capabilities of Skaffold; it's a very powerful tool that can be used for both local development and production. For more detailed information, you can read the official documentation at https://skaffold.dev/docs/
Tilt
The last but not least tool is Tilt, with which I have almost no experience, but it has more than 7k stars on github and they joined Docker, which sounds promising.
Example:
This tool has its own file format, and for syntax highlighting in IntelliJ IDEA, you can use this repository: https://github.com/tilt-dev/tiltfile.tmbundle, in the screenshot, you can see three functions:
- k8s_yaml- specify the path to the k8s manifest.
- docker_build - provide the data for the build, and an interesting part is live_update, where you specify what to synchronize and the command to run if a file changes.
- k8s_resource - here we specify k8s resource name and the port we want to expose.
Running it is quite simple:
tilt up -f deploy/Tiltfile
Like with other tools, this is just a simple example of using Tilt, which is capable of much more. You can read more details on the official website: https://docs.tilt.dev/
If you know of other tools that you use or have used, please share in the comments. I'll try to update the post with examples of other tools as much as possible.
You can find all the source code here: https://github.com/idsulik/local-dev
Russian version: https://habr.com/ru/articles/786282/
P.S. If you need a separate utility that tracks changes and synchronizes them into a container pod, I recommend skasync (https://github.com/konrin/skasync). It can be used in conjunction with Skaffold and more.
Top comments (1)