Docker Tip #57: Using Build and Image in the Same Docker Compose Service
![blog/cards/docker-tips-and-tricks.jpg](/assets/blog/cards/docker-tips-and-tricks-92efa4f3cccffb29a7369417a3f9e884a77a918d152a0d2c1636c064a4dd2169.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.