Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #12: A Much Better Development Experience with Volumes

blog/cards/docker-tips-and-tricks.jpg

If you're creating apps written in dynamic languages such as Ruby, Python or Node then you should be using volumes in development.

The idea of having to build a new Docker image every time you make a change to your app’s code base doesn’t seem very productive. Even with Docker’s superb ability to cache image layers so that it only updates what changed, it would still be annoying to re-build constantly.

Lucky for us, we can use volumes to mount in our app code directly into a running container. This way if we update our code base, then the changes will take effect immediately.

This is what will give you real-time updates and create a development experience that rivals the speed of what you would get without Docker (with the added win of using Docker).

You can also get the best of both worlds by using COPY in a Dockerfile like usual, but then when it comes time to running your containers in development, you simply add the volume flag to match COPY’s path which will override whatever you copied in.

For example, here’s a trimmed down snippet that shows only what we talked about:

# Dockerfile

# Business like usual, copying in files to the Docker image.
COPY . /app
# docker-compose.yml

services:
  app:
    # Mount the current directory into `/app` inside the running container.
    volumes:
      - '.:/app'

Now you get the portability benefits of using COPY (which creates self contained Docker images that you can distribute to other machines) in production, and at the same time, you get a speedy development environment. It’s a win win.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments