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.
docker sync [COMMAND]
- –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
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.
Let’s understand with examples, so first, we need to install in the system.
Installation on a Linux machine (Ubuntu):
1. First of all update your system
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: –
6. Once it is installed successfully. Create a docker-sync.yml file with the below config and it should be in the main directory: –
7. Let’s assume, we have a docker-compose.yml file with the below configuration: –
8. Now, create a docker-compose-dev.yml file and populate it with the below configuration:
9. Let’s start using the below command:
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: –
12. We can use clean to clear the logs and remove the unused volumes.
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:
1. It is supported for almost all platforms OSX, Windows, Linux, and FreeBSD.
2. It improves the code execution a lot if running on Docker for Mac and Windows.
3. It does not require any dependencies on OSX if we use native_osx.
4. It supports more than one sync-end point and project at the same time.
5. 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:
1. The ‘docker-compose’ must be installed on the system to use docker-sync
2. The ‘docker-sync clean’ command only removed unused volumes and if the volumes are in use, it throws the error.
3. It is recommended to update the system before installing the gem.
4. We should only sync the files that are really needed on the host as well as the client-side.
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.
This is a guide to Docker Sync. Here we also discuss how sync command works in docker along with its advantages and a few examples. You may also refer to our other related articles.
- Docker run Command
- Difference between OpenShift vs Docker
- Docker Stop Container
- Docker Container Linking