16

I'm moving most of my development processes into docker to ensure a parallel development environment between computers so there are no odd bugs or issues due to version mis-match, etc.

All of this is going great, except that when running webpack-dev-server inside of docker, the build process is substantially slower than when I run it just locally on my computer. (Like 3-5 minutes in docker vs 30 seconds to 1 min locally). Is there any way to speed this up? Is it just an issue with docker/webpack interacting with a lot of files on my hard drive through a mounted volume?

If it matters, my host system is a Mac running High Sierra on an i7 with 16bg of ram.

I'm running docker for mac, docker -v returns: Docker version 17.12.0-ce, build c97c6d6

I hope all of this is clear enough, let me know if I can add any information!

Griffin Meyer
  • 684
  • 6
  • 19

4 Answers4

12

For those in a similar spot, as Matt suggested, the issues were coming from having a mounted volume. I sped the build up significantly by using docker's volume cache mode. The docs on it are here.

The command looks something like this:

docker run -v \local\director:docker\directory:cached dockerImage

Community
  • 1
  • 1
Griffin Meyer
  • 684
  • 6
  • 19
  • 2
    Thank you! Spent so long on figuring this out and this answer was the key, it reduced my build time from 30s to <1s. For anyone using a docker-compose.yml file, you can use the same syntax and add `:cached` to the end of your volume. – Ryo C. Feb 21 '19 at 23:46
5

I would recommend using delegated instead of cached as per the documentation:

Cached: The host is authoritative in this case. There may be delays before writes on a host are available to the container.

Delegated: The container is authoritative. There may be delays until updates within the container appear on the host.

So the docker-compose file would be as following:

version: '3'
services:
  front:
    container_name: my-front-dev
    image: my-front-dev-image
    build:
      context: .
      dockerfile: front/Dockerfile.dev
    ports:
      - 5002:80
    volumes:
      - ./front/:/app/:rw:delegated
Nicolas
  • 1,193
  • 1
  • 10
  • 25
2

Apart from adding cached volume flag to your docker-compose.yaml file:

version: '3'
services:
  front:
    container_name: my-front-dev
    image: my-front-dev-image
    build:
      context: .
      dockerfile: front/Dockerfile.dev
    ports:
      - 5002:80
    volumes:
      - ./front/:/app/:rw,cached

I also recommend you to upgrade docker-for-mac to newest version (bump to version 2.0.0 had significant performance improvement on my mac).

Last recommendation is to raise default CPU/Memory limits in docker-for-mac settings:

docker cpu/memory limits

lukyer
  • 7,595
  • 3
  • 37
  • 31
  • 3
    You have this in your volume `./front/:/app/:rw,cached`. What does the last part `:rw` do? – Carven Mar 25 '19 at 05:22
1

Windows

A lot of these answers apply mostly to Mac users. If you're on Windows you should run your Docker commands in your Linux distro. It was a night and day difference for me. No messing with caching or anything. If you already have Docker Desktop installed you just have to make sure you already have a Linux distro installed. It's easy to setup if you don't.

Basically, any read/write process between Windows and Linux takes a long time. If you run your Docker container inside Windows Subsystem for Linux the file read/write is near instantaneous because the file are going from Linux to Linux. You will have to move your files from whatever directory in Windows to a directory in your Linux distro, but that shouldn't be a problem assuming you're using git.

Resources:

Article that kind of explains it

Docker documentation on setting up WSL 2 and a Linux Distro

VSCode working with WSL 2

Michael Cox
  • 1,116
  • 2
  • 11
  • 22