Docker sync to decrease start times
Install docker-sync to decrease start times and increase “responsiveness” when developing in a containerised environment on macOS.
tl;dr:
I met a brick wall about 1 month ago, thrown in a dev environment which was dockerised to the bone. Me, with almost no experience too, got the feeling I was 5 years old and handed “A Practical Guide to the UNIX System” and expecting a coloring book.
The monkey in me, shouted “-this is even better! More buttons to push”. Now in the present, with some time spent in the project. Not so great.
It takes about 10 minutes to start a clean project. What? 10 minutes. That cant be right. Well, I usually never question why its “seems” broke or borken. Little did I know then, but the solution I found was Docker sync. (Maybe I found this too late, as of the time of writing this article, I do not know. It could have dire consequences)
Problem:
So what was the problem I was facing? Well, if I understood it correctly. On macOS and Windows, docker run in a virtualized environment. This have its drawbacks specially when you are not on a Linux machine. I am still reading up about this.
When restarting fresh or updating containers with rewriting code, i.e add new packages, implementing bugs/code =). It could take up to 10 minutes, really tedious.
My solution: (1 of them, for me, atleast)
Structure
vanilla/ # → Root of example
├── docker/ # → Data for containers
│ └── strapi
├── src/ # → Source files
│ └── Source files
├── .env # → Environment files
├── .docker-compose-dev.yml # → Setup documents/files for editor
├── .docker-sync.yml # → Basic sync file
├── .docker-compose.yml # → Services used in project
└── README.md # → Webpack configuration
In this example I will only be syncing webserver and cms. I have not figured out cached yet. So db is for another article.
Install
Install “Docker sync”
gem install --user-install docker-sync
Create a “docker-sync.yml” file, example
version: "2" syncs: vanilla: # <--- Sync notify_terminal: true src: "./" sync_excludes: [".git", ".idea", "node_modules"]
Create a “docker-compose-dev.yml” where you add the volumes in your project.
version: "2" services: nginx: volumes: - vanilla:/app:nocopy # <--- Where to, if I understood it correctly strapi: volumes: - vanilla:/app:nocopy # <--- Where to, if I understood it correctly volumes: vanilla: # <--- This points to what I want to sync in the sync file external: true
Running the containers sync
docker-sync-stack start
Run in background
docker-sync start
Wait until sync command is finished, then start your project with the compose files.
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up
Thats it, and in my case the speeds went from 4 minutes down to 3-4 seconds. Not much 4 minutes. But when you need to restart and test from a clean “git clone”. Those times add up, and become alot of wait time.
Thanks to freepik for awesome icons!