Skip to content

Instantly share code, notes, and snippets.

@dmgig
Last active August 19, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmgig/1504f394abfd52bebb6e652f929f02da to your computer and use it in GitHub Desktop.
Save dmgig/1504f394abfd52bebb6e652f929f02da to your computer and use it in GitHub Desktop.
Getting DDEV and docker-sync to work together

Getting DDEV and docker-sync to work together

Here is a basic set up for running DDEV and docker-sync on a Mac.

Previously slow performance is dramatically improved.

I struggled with the documentation a bit, it seemed to show more of the complexities than the basics.

To explain it as I now understand it, with docker-sync we are setting up a new docker volume (our docker-sync volume) which acts as an intermediary between our Mac and the docker container.

This fixes the slow speed and high CPU usage which is caused by the Mac's filesystem interacting directly with the Docker container.

docker-sync configuration

A docker-sync.yml file should be created inside of the .ddev folder.

docker-sync.yml

version: '2'
options:
  verbose: true
  compose-file-path: 'docker-compose.yml'
  compose-dev-file-path: 'docker-compose.override.yml'
  project_root: '../'
syncs:
  my_project_name-sync:
    notify_terminal: true
    src: '../'
    sync_excludes: ['.git', '.idea']

Properties options.project_root and syncs.my_project_name-sync.src both point to the project root (the parent directory of the .ddev directory).

  • If these are set incorrectly, you'll see your files have been sync'd to the wrong directory inside the container.
  • Directly under syncs, we are naming our volume, which should be any name you like, only unique. docker-sync suggests that you end your container name with -sync just for clarity's sake.

Excluded directories will not be sync'd of course.

docker-sync is highly configurable as shown it its documentation, but this minimal set up worked.

DDEV Docker Composer

docker-compose.override.yaml

version: '3.6'
services:
    web:
      volumes:
        - teamleaders-sync:/var/www/html:nocopy
        - ".:/mnt/ddev_config:ro"
        - ddev-global-cache:/mnt/ddev-global-cache
        - ddev-ssh-agent_socket_dir:/home/.ssh-agent

volumes:
  my_project_name-sync:
    external: true

As shown above, we add a docker-compose.override.yaml file to the .ddev directory.

Dealing with the lower volumes section first - we have added in a definition for the docker-sync volume. The my_project_name-sync key matches with the key under syncs in the docker-sync.yml file described above.

In the upper services section, we override the services.web.volumes from DDEV's docker-compose.yml file.

Here we have replaced the first volume under DDEV's default web service. The old default volume connected the Docker container to our Mac's filesystem.

Our replacement volume instead joins the Docker container's filesystem to our docker-sync volume, instead of directly to our Mac's filesystem.

Start up

First, go to your project root and stop any running DDEV containers with ddev stop.

Next we navigate to the .ddev directory (where the docker-sync.yml file is) and start up docker-sync with docker-sync start.

  • All docker-sync commands have to be run from the directory where the docker-sync.yml file is.
  • Other useful commands are docker-sync stop and docker-sync clean, the latter of which removes the docker-sync container and it's volumes. Refer to docker-sync's documentation for more on those.

The first time around, it will take a bit to get through it's precopy phase as it creates the volume. It will then begin the Looking for changes process, and this could take several minutes.

It will then show the long list of directories as it is syncing.

When complete, the terminal will display the following success messages:

          ok  starting initial sync of teamleaders-sync
     success  Sync container started
     success  Starting Docker-Sync in the background

We can now navigate back to the project root directory and start up DDEV with ddev start.

Expected results

Aside from Drupal's usual slowness while caching pages, the site should now be performing with the quick speed which a local development enviroment should.

Simple page navigation which used to peg my Mac's CPU no longer does so. Also gone is the memory swapping which had previously plauged me when running the Docker container without docker-sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment