For Docker Compose V2.0, please read the comment below.
Since GCE's Container-Optimized OS do not include docker-compose
by default.
So, we need to use the official docker/compose image as a workaround.
However, docker/compose
cannot access GCR or GAR to pull private images.
Therefore we have to create a custom docker-compose image which is authenticated to GCR / GAR.
mkdir docker-compose-gar && cd docker-compose-gar
FROM docker/compose:alpine-1.27.4
ENV VERSION=2.0.4
ENV OS=linux
ENV ARCH=amd64
# Install docker-credential-gcr
RUN wget -O - "https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/v${VERSION}/docker-credential-gcr_${OS}_${ARCH}-${VERSION}.tar.gz" \
| tar xz --to-stdout ./docker-credential-gcr > /usr/bin/docker-credential-gcr && chmod +x /usr/bin/docker-credential-gcr
RUN docker-credential-gcr version
RUN docker-credential-gcr configure-docker --include-artifact-registry
CMD docker-compose
docker build -t docker-compose-gar .
echo alias docker-compose="'"'docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PWD:$PWD" \
-w="$PWD" \
docker-compose-gar'"'" >> ~/.bashrc
Now you can pull the private images from GCR / GAR
Try:
docker-compose pull
or
docker-compose up
Update: Docker Compose V2.0+
Since docker/compose image stopped updating since v2.0, therefore we need to install docker/compose-bin plugin instead.
Run this once only, and you can then run compose with docker compose (no dash):
Ref
GCE startup script
You have to run the script every time you restart the instance, thus it is better to add it to the startup script of your instance.
Using startup scripts