Pull service images while ignoring buildable services
In this step, we will explore how to use the --ignore-buildable flag with docker compose pull. This flag is useful when your compose.yaml file includes services that are built from a Dockerfile (buildable services) in addition to services that use pre-built images. The --ignore-buildable flag tells Docker Compose to only pull images for services that specify an image key, and to skip services that specify a build key.
First, let's modify our compose.yaml file to include a buildable service. We will add a simple service that builds a basic image.
Ensure you are in the ~/project/my-compose-app directory.
cd ~/project/my-compose-app
Now, create a simple Dockerfile for our buildable service.
nano Dockerfile
Paste the following content into the Dockerfile:
FROM alpine:latest
CMD ["echo", "Hello from buildable service!"]
Save the file and exit the nano editor.
Next, modify the compose.yaml file to include a new service that uses this Dockerfile.
nano compose.yaml
Add the following service definition to your compose.yaml file, below the db service:
builder:
build: .
Your complete compose.yaml file should now look like this:
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: redis:latest
builder:
build: .
Save the file and exit the nano editor.
Now, let's try to pull images using docker compose pull --ignore-buildable.
docker compose pull --ignore-buildable
You will observe that Docker Compose checks the web and db services and pulls their images if necessary (they should already be pulled from the previous steps). However, it will ignore the builder service because it uses the build key instead of the image key. You will not see any output related to building or pulling an image for the builder service.
To confirm that no image was built or pulled for the builder service, you can list your Docker images.
docker images
You should still only see the nginx and redis images listed. There will not be a new image created from the Dockerfile by this command.
This demonstrates how the --ignore-buildable flag allows you to selectively pull only pre-built images, which can be useful in various development and deployment scenarios.