Updated April 1, 2023
Introduction to Docker Sync
Docker sync is a very useful tool to improve the performance of your containerize application about 60 times on Mac or Windows. We are facing performance issues while working on development projects on Mac or Windows because containers run inside a VM on these systems and it degrades the file read/write performance drastically as the file system used by Mac or Windows is different from Linux so containers can not read or write directly to disk instead it depends upon the Mac or Windows kernel.
Syntax:
docker sync [COMMAND]
Commands:
- –version, -v: Used to print out the version of docker-sync.
- clean: Used to stop and clean up all sync endpoints.
- help [COMMAND]: Used to know more about any command.
- list: Used to list all sync points of the project configuration path.
- logs: Prints the last 100 lines of daemon log, It only works with docker-sync running in the background.
- start: Used to start all sync configuration in the project.
- stop: Used to stop the docker-sync daemon.
- sync: Used to the only sync without starting the watcher.
To know more about any of the above command, we can use the help command as below:
docker-sync help start
Explanation:
- In the above snapshot, we can see that it shows all options available for the ‘docker-sync start’ command.
How Sync Command Works in Docker?
Docker-sync creates a volume and saves all our application source files in that volume that makes it available to our application running as a container which means we need to mount that volume to the container. It uses native mount to access volumes and volumes are not bound to our host (i.e. Windows or Mac) filesystem that is handled by Linux kernel and that allows containers to access our application source code faster.
It uses different sync strategies based on the operating system by default, however, we can mention specific sync strategies in the docker-sync.yml file.
Examples
Let’s understand with examples.
Installation on a Linux machine (Ubuntu):
1. First of all update your system.
apt-get udpate
2. Install ruby and ruby-dev.
apt-get install ruby ruby-dev -y
3. Install gem now.
apt-get install gem
4. Now, install and the below command will work on any platform after installing gem.
gem install docker-sync
5. We will run the below command to check, docker-sync was installed successfully or not.
docker-sync -v
6. Once it is installed successfully. Create a docker-sync.yml file with the below config and it should be in the main directory.
docker-sync.yml
7. Let’s assume, we have a docker-compose.yml file with the below configuration.
docker-compose.yml
8. Now, create a docker-compose-dev.yml file and populate it with the below configuration.
docker-compose-dev.yml
9. Let’s start using the below command.
docker-sync start
Note: When we run this command, it creates a volume with the name mentioned in the ‘docker-sync.yml’ file, we can check it by running ‘docker volume ls’ as shown below.
docker volume ls
10. Now, start the containers using Docker compose as shown below.
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up –d
11. If we want to stop the sync, we can use the stop keyword by replacing start as shown below:.
docker-sync stop
12. We can use clean to clear the logs and remove the unused volumes.
Explanation:
- In the above example, we can see a Docker volume is created and when we run the clean command it gives us an error that the volume is still in use because those containers are still running so we need to remove the containers first in order to remove those volumes. Mostly, it is used to clear logs, remove stopped containers, or unused volumes created by docker-sync.
13. We can use the ‘list’ command as below to list all sync-points of the project configuration path.
docker-sync list
Advantages of Docker Sync
Given below are the advantages mentioned:
- It is supported for almost all platforms OSX, Windows, Linux, and FreeBSD.
- It improves the code execution a lot if running on Docker for Mac and Windows.
- It does not require any dependencies on OSX if we use native_osx.
- It supports more than one sync-end point and project at the same time.
- It also does not bound us to use a specific docker solution as it is for docker-for-mac, docker toolbox, VirtualBox, VMware Fusion, Parallels, or whatever.
Rules and Regulations for Using Sync
- The ‘docker-compose’ must be installed on the system to use docker-sync.
- The ‘docker-sync clean’ command only removed unused volumes and if the volumes are in use, it throws the error.
- It is recommended to update the system before installing the gem.
- We should only sync the files that are really needed on the host as well as the client-side.
Conclusion
It is the best tool to avoid slowness issues if running multiple containers on Mac OS or Windows and syncing files from host machines. There are options available while creating docker-sync.yml to fulfill your requirement.
Recommended Articles
This is a guide to Docker Sync. Here we discuss the introduction, how sync command works in docker, examples, and advantages. You may also refer to our other related articles –