How to tag docker image with docker-compose

DockerDocker Compose

Docker Problem Overview


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.

Docker Solutions


Solution 1 - Docker

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

Solution 2 - Docker

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

Solution 3 - Docker

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.

Solution 4 - Docker

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

Solution 5 - Docker

you can try:

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

Solution 6 - Docker

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:

Solution 7 - Docker

Make sure your FROM statement in a Dockerfile contains the right image name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question4ybakaView Question on Stackoverflow
Solution 1 - DockerRandy LarsonView Answer on Stackoverflow
Solution 2 - DockerRicoView Answer on Stackoverflow
Solution 3 - DockerRyan McGrathView Answer on Stackoverflow
Solution 4 - DockerJavier RojanoView Answer on Stackoverflow
Solution 5 - Dockerpaulspartan14View Answer on Stackoverflow
Solution 6 - DockerDoğuşView Answer on Stackoverflow
Solution 7 - DockernewlogView Answer on Stackoverflow