Development Made Easier: Docker Init & Watch Mode

Raju Dawadi
3 min readJul 31, 2023
docker init & compose, src: docker.com

We hear much about the ease and parity docker brings in whole build-deployment flow as well as some of the downsides. But when it comes to real implementation or Dockerizing existing project, we feel like a learning curve to write few lines in “Dockerfile”. After getting that done, another problem lies in local environment is live reloading of code or rebuilding artifact because we might not be using standalone image in development environment.

Both of these issues are solved by the recent release of docker and compose. Here is “docker init” in action:

docker init outputs

Once your application(in above example, it’s nodejs project) is ready, initialize the project with docker init command. This lets us enter a multiple options related with the project like,

  1. Application platform(Go, Python, Node, Rust and Other)
  2. Version of docker image to use as base image
  3. Package manager if any(npm, yarn, pnpm)
  4. Port on which the application listens on
  5. Command to run for starting the application(CMD)
  6. Relative directory(in case of Go)

After passing these customizable variable, it creates few new files:

  1. Dockerfile
  2. .dockerignore
  3. compose.yaml

The generated Dockerfile is well suited, minimal and ready to ship. Though it doesn’t have multi-stage build steps but customized to use build cache based on the platform that is chosen above.

Also, the compose.yaml is ready to run with docker-compose up but it has some additional lines commented out if that is required for future, like, a database/db(postgres) definition block with port exposure and all.

With the above minimal docker specific files generation, if the project doesn’t have any external dependency or requirement, it should be enough. In case of the dependencies, we need to add the steps on Dockerfile and compose.yaml as required.

Next feature, which is in alpha release, is Docker Compose Watch Mode.

One of the major issue in using docker or compose is live loading the startup command or restarting the app when application code changes or dependency is added/removed. Some people do docker-compose up -d --build --force or some script hacks which watches all files in the directory. But gone are those days.

Watch mode is there to solve all those hassle. After changing a line of code in any file inside the application directory, compose itself hot-reloads the application startup script which is specified in compose.yaml file. An example of such structure looks like this:

services:
server:
build:
context: .
environment:
NODE_ENV: production
ports:
- 3000:3000
command: npm start
x-develop:
watch:
- action: rebuild
path: ./
target: /usr/src/app/
ignore:
- node_modules/
- action: rebuild
path: package.json

Here, the npm start script is executed for any change of file inside current directory as well as package.json .

But if you have other application which reloads the code(like Nodemon) on file change but only need is to sync the changed file inside docker. In such case the action could be:

services:
server:
build: .
x-develop:
watch:
- action: sync
path: ./
target: /usr/src/app/
ignore:
- node_modules/
- action: rebuild
path: package.json

Won’t this make your development workflow much easier while using docker and compose?

That’s it for this post. If you are interested on getting my interesting updates, connect with me on Linkedin, Twitter.

--

--