Felix Benning
Felix Benning

Reputation: 1192

Docker Image Reference Format

I am trying to write a docker-compose.yml which is then used in a continuous integration pipeline, but should also be possible to use locally.

I naively tried this:

services:
  app:
    build:
      ...
    image: ${IMAGE_SERVER_URL:-}/image_name:${IMAGE_TAG:-latest}

in my gitlab-ci.yml I log into the image server:

echo ${IMAGE_SERVER_PASSWORD} | docker login -u ${IMAGE_SERVER_USERNAME} --password-stdin ${IMAGE_SERVER_URL}

and then I can do

docker-compose build --pull
docker-compose push

So what is the issue?

when the .devcontainer from vscode attempts to build the app I get ERROR: Invalid Reference Format which is of course due to the fact that

/image-name:latest

is not a valid image name. So the issue is the slash.

docker-compose does not accept ${IMAGE_SERVER_URL+/} currently (cf. docker docs) so that is out of the question. I could of course include the slash in the environment variable but I have the feeling that this is going to cause similar problems in other places.

Are there any best practices when it comes to formatting dynamic image names?

Upvotes: 0

Views: 1122

Answers (2)

BMitch
BMitch

Reputation: 263846

You can default your server url to Docker Hub's common name docker.io. And if you are using an official image from the library, you can also include library as the repository name:

${IMAGE_SERVER_URL:-docker.io}/image_name:${IMAGE_TAG:-latest}

Upvotes: 1

Konrad Botor
Konrad Botor

Reputation: 5053

There are two solutions that come to mind that satisfy OP's requirements.

One is to set the default value of IMAGE_SERVER_URL in the docker-compose.yml to a non-empty string (like OP did with IMAGE_TAG).

The other is to set said default value in the .env file placed alongside the docker-compose.yml.

As long as developers' are not meant to push their locally built image to the repo, it shouldn't matter what that default value is as long as it's not empty.

In both cases, according to the documentation, variables set in the shell by the CI server should override default values.

Edit: One other solution, less elegant in my opinion, is to use ${IMAGE_SERVER_URL:?Variable IMAGE_SERVER_URL must be set to a non-empty string} in docker-compose.yml to generate more comprehensible error message.

Upvotes: 1

Related Questions