Tools and Configurations for an Effortless Workflow in Docker and Kubernetes
Tags: docker, kubernetes, development, skaffold
Published: 2024-01-17 20:34:52

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](https://docs.docker.com/compose/file-watch/#sync) - synchronizes changes in the file with the file in the container.

[Rebuild](https://docs.docker.com/compose/file-watch/#rebuild) - initiates the image-building process and replaces the already running container with a new one.

[Sync + Restart](https://docs.docker.com/compose/file-watch/#sync--restart) - synchronizes changes and restarts the container.

**Example**:

![code example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0ap0eveo5r3le69t6iz.png)

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
```

![Docker watch in action](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExbjNqZXlxemVldHBmanphOGtoMmlsaXNjOWg4MGMxNGhtZTBqODFyeiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QHlJoslE2n3mfQ6bvH/source.gif)

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**:

![Code example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4al9bwmwck9aa0tfdglv.png)

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
```

![skaffold in action](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNXBqcndic3dlaGowbWZrNGE5cGNvZXB0bnIxenl1OWU5ZGN2YTF6MCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ZjuXEKg8RQFd8xoc3O/source.gif)

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**:

![title code example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3faayyks052om3i4nuca.png)

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:

1. _k8s_yaml_- specify the path to the k8s manifest.
2. _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.
3. _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
```

![title in action](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmx3cTNhejBkNmZncTN0dGVmNXlyMWIya3Rxem93eHhwcWNzeTdqMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/VpRJgyYdL82gJfu0T7/source.gif)

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.