简体   繁体   中英

How to specify a docker image in docker-compose file using regex

I have a docker image which I build using an automated pipeline job.

It is called:

REPOSITORY                      TAG                IMAGE ID        
test-001-com:3000/img           1.23-SNAPSHOT      2f83de9h895e

NOTE: The TAG changes daily so tomorrow it will be 1.24-SNAPSHOT etc...

My Question is:

How do I use the "image" argument in docker-compose to pass in a regex or something that I don't have to update it everytime to match the TAG.

docker-compose.yml:

services:
  test-001:
    hostname: "test-001"
    container_name: "test-001"
    image: "test-001-com:3000/img:1.23-SNAPSHOT"
    ports:
    - "8000:8000"
version: "2.1"

The above docker-compose.yml works but can I replace:

image: "test-001-com:3000/img: 1.23-SNAPSHOT "

WITH

image: "test-001-com:3000/img: *-SNAPSHOT " or something?! <- Doesn't work.

You can't do that. The image: must name an exact version tag, or not have a tag and use the implied ...:latest version. In general Docker just doesn't support this; there is no easy way to search available images by tag and match with a regex or shell glob, you have to know what you're looking for. Even if you did have a list of tags, there's no universal definition of what's "newest".

You tagged this as "kubernetes". If this is actually a Kubernetes question, the best way to do this is to set up your continuous deployment system to update the Kubernetes Deployment for you. Kubernetes will try to start new pods with the new version before deleting old pods, so you should get a zero-downtime upgrade. ( Helm is a common tool to inject parameters like this; if an upgrade goes wrong it also readily supports rolling back an update.)

Your example uses Docker Compose. The image: field is one of the places variable substitution works, so you can set

image: "test-001-com:3000/img:${IMAGE_VERSION:-latest}"

and then set IMAGE_VERSION as an environment variable or in a .env file in the same directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM