4ybaka
4ybaka

Reputation: 3264

How to tag docker image with docker-compose

I want to build image via docker-compose and set specific tag to it. Documentation says:

Compose will build and tag it with a generated name, and use that image thereafter.

But I can't find a way to specify tag and for built images I always see 'latest' tag.

Upvotes: 253

Views: 323527

Answers (9)

Randy Larson
Randy Larson

Reputation: 7617

It seems the docs/tool have been updated and you can now add the image tag to your script. This was successful for me.

Example:

version: '2'
services:

  baggins.api.rest:
    image: my.image.name:rc2
    build:
      context: ../..
      dockerfile: app/Docker/Dockerfile.release
    ports:
      ...

https://docs.docker.com/compose/compose-file/build

There is also a separate key for Tags.

Upvotes: 337

YouJiacheng
YouJiacheng

Reputation: 687

https://docs.docker.com/compose/compose-file/#image

If the image does not exist on the platform, Compose implementations MUST attempt to pull it based on the pull_policy. Compose implementations with build support MAY offer alternative options for the end user to control precedence of pull over building the image from source, however pulling the image MUST be the default behavior.

https://docs.docker.com/compose/compose-file/build/#tags

tags defines a list of tag mappings that MUST be associated to the build image. This list comes in addition of the image property defined in the service section

If image is specified, pulling the image will be the default behavior, thus if you want to use local image only, you should not specify image.

tags in build section is the correct place to specify the tag of local image.

services:
  backend:
    build:
      context: backend
      dockerfile: ../backend.Dockerfile
      tags:
        - "myimage:mytag"

Upvotes: 5

Henry S.
Henry S.

Reputation: 548

According to official docs: https://docs.docker.com/compose/environment-variables/set-environment-variables/

You can set an environment variable from your .env file

$ cat .env
TAG=v1.5

Then, you can retrieve it and use it as a tag for your image.

$ cat docker-compose.yml
services:
  web:
    image: "webapp:${TAG}"

You can verify it via command docker compose convert

$ docker compose convert

services:
  web:
    image: 'webapp:v1.5'

Upvotes: 1

geekyouth
geekyouth

Reputation: 350

it works for me: https://docs.docker.com/build/customize/bake/compose-file/#specification

services:
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
      tags:
        - spring-postgres:1.0
        - spring-postgres:latest
    ports:
      - 8080:8080

then run docker compose build --no-cache output as follows:

> docker compose build --no-cache
[+] Building 192.9s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                              0.0s 
 => => transferring dockerfile: 457B                                                                                                                              0.0s 
 => [internal] load .dockerignore                                                                                                                                 0.0s 
 => => transferring context: 2B                                                                                                                                   0.0s 
 => [internal] load metadata for docker.io/library/eclipse-temurin:17-jre-focal                                                                                   0.0s 
 => [internal] load metadata for docker.io/library/maven:3.8.5-eclipse-temurin-17                                                                                 0.0s 
 => [builder 1/5] FROM docker.io/library/maven:3.8.5-eclipse-temurin-17                                                                                           0.0s 
 => CACHED [stage-1 1/3] FROM docker.io/library/eclipse-temurin:17-jre-focal                                                                                      0.0s 
 => [internal] load build context                                                                                                                                 0.0s 
 => => transferring context: 1.05kB                                                                                                                               0.0s 
 => CACHED [builder 2/5] WORKDIR /code                                                                                                                            0.0s 
 => [builder 3/5] COPY pom.xml pom.xml                                                                                                                            0.0s 
 => [builder 4/5] COPY src .                                                                                                                                      0.0s 
 => [builder 5/5] RUN mvn clean -DskipTests -T 2C -X -am install                                                                                                192.5s 
 => [stage-1 2/3] COPY --from=builder /code/target/*.jar app.jar                                                                                                  0.0s 
 => exporting to image                                                                                                                                            0.0s 
 => => exporting layers                                                                                                                                           0.0s 
 => => writing image sha256:a5827124abc899efcfbc8db150f72f5ff8034d1ab93418dee5c15525ae4e42a1                                                                      0.0s 
 => => naming to docker.io/library/spring-postgres-backend                                                                                                        0.0s 
 => => naming to docker.io/library/spring-postgres:1.0                                                                                                            0.0s 
 => => naming to docker.io/library/spring-postgres:latest                                                                                                         0.0s 

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

above console output spring-postgres:1.0 and spring-postgres:latest

Upvotes: 3

Ryan McGrath
Ryan McGrath

Reputation: 2678

I'd like to add that you can also manage your tag versions through environment variables or an .env file.

https://docs.docker.com/compose/environment-variables/#the-env-file

export TAG=1.11

Example:

version: '3.3'
services:

  baggins.api.rest:
    image: my.image.name:${TAG}
    build:
      context: ../..
      dockerfile: app/Docker/Dockerfile.release
    ports:
      ...

docker-compose config to validate

In my ci pipeline my first build is tagged with a throwaway value used for running tests. Then I change the tag to latest and rebuild again (nearly instant since it's all cached) before pushing to the registry.

Upvotes: 43

Doğuş
Doğuş

Reputation: 1957

If you have already built your image, you can re-tag it by using the docker tag command:

docker tag imagename imagename:v1.0

docker tag imagename:v1.0 imagename:v1.1

If you have multiple tags attached to your repository, and if you want to remove one of them, you can use the docker rmi command:

$ docker rmi imagename:v1.0
Untagged imagename:v1.0

Reference:

Upvotes: 14

paulspartan14
paulspartan14

Reputation: 101

you can try:

services:
  nameis:
    container_name: hi_my
    build: .
    image: hi_my_nameis:v1.0.0

Upvotes: 9

Javier Rojano
Javier Rojano

Reputation: 982

If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:

build: ./dir
image: webapp:tag

This results in an image named webapp and tagged tag, built from ./dir.

https://docs.docker.com/compose/compose-file/#build

Upvotes: 9

Rico
Rico

Reputation: 61669

Original answer Nov 20 '15:

No option for a specific tag as of Today. Docker compose just does its magic and assigns a tag like you are seeing. You can always have some script call docker tag <image> <tag> after you call docker-compose.

Now there's an option as described above or here

build: ./dir
image: webapp:tag

Upvotes: 46

Related Questions