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 #57: Using Build and Image in the Same Docker Compose Service

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

You can build an image from a Dockerfile, and you can pull an image from a Docker Registry, but what happens when you supply both?

If you add build: "." to a service that will build a Docker image out of the Dockerfile that exists in the directory when you build or up your compose file.

If you add image: "postgres:10.3-alpine" to a service that will pull down that image from the Docker Hub when you pull or up your compose file.

But what happens if you define both a build and image property like this:
web:
  build: "."
  image: "nickjj/myimage:1.0"

The world won’t explode, so that’s good.

When the image gets built, instead of using your COMPOSE_PROJECT_NAME or folder name + service name as the name of the image it will use what you have in the image property.

In the above example you would end up with an image named and tagged as nickjj/myimage:1.0 which will be built out of the Dockerfile in the current directory.

This is really handy because it’s basically a shortcut to tag your images so that you can quickly push them to your Docker Registry of choice, such as the Docker Hub.

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