Better Programming

Advice for programmers.

Follow publication

Improve the Performance of Docker on MacOS With Docker-Sync

Docker on Mac can be a resource hog but it doesn’t have to be

Rohit Lingayat
Better Programming
Published in
2 min readMay 11, 2020

--

Photo by Md Mahdi on Unsplash

If you’ve moved your development environment to Docker and you’re on a Mac, you might have noticed your web application stacks are slower than another native environment you’ve been used to. The fact of the matter is, Docker for Mac has terribly slow IO performance compared to on Linux machines.

Developing with docker under OSX/Windows is a huge pain, since sharing your code into containers slows down the code-execution. To solve the performance issue we can use docker-sync. To show how to set up the docker-sync with our application let’s take an example of a Rails application.

In my rails application I have a docker-compose.yml of the following configuration:

First, you need to install the docker-sync gem:

sudo gem install docker-sync

Then you need to create docker-sync.yml. This will have the following configuration, with the src entry point which is going to be used as an external volume in the docker-compose.yml:

Once you’ve created docker-sync.yml you need to update the docker-compose.yml file:

So, the whole docker-compose.yml file will look like this:

To start the container you can use docker-sync-stack startthis command will start your sync process as well as the server.

If you want to start the sync process first and then start the containers separately you can do it with the following commands:

docker-sync clean
docker-sync start
docker-compose up

After setting up docker-sync you may face the issue that your main application container tries to get up and running before the database container spins up. If this happens then your main application container will not get started. To prevent this we can make the following change to your docker-compose.yml file.

command: bash -c "while !</dev/tcp/db/5432; do sleep 1; done; rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"

This configuration is optional — it all depends on how you set up your dependent containers in your docker-compose.yml file. By using this configuration the main application container won’t start unless the database container has started.

This setup is working pretty well for us and enables us to run Docker on a Mac like a charm!

--

--

Rohit Lingayat
Rohit Lingayat

Written by Rohit Lingayat

Ruby on Rails | React | AWS | Solr | JQuery | Nodejs | Typescript

No responses yet

Write a response