我想通过docker-compose构建图像,并设置特定的标签。文档说:
Compose将构建并标记一个生成的名称,并使用该名称 形象。< / p >
但是我找不到一种方法来指定标签,对于构建的图像,我总是看到“最新”标签。
原来的答案15年11月20日:
截至今天,没有特定标记的选项。Docker compose只是发挥它的魔力,像你看到的那样分配一个标签。在调用docker-compose之后,你总是可以让一些脚本调用docker tag <image> <tag>。
docker tag <image> <tag>
现在有一个如上所述的选项或在这里
build: ./dir image: webapp:tag
文档/工具似乎已经更新,现在可以将image标记添加到脚本中。这对我来说很成功。
image
例子:
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
如果你指定了image和build,那么Compose用image中指定的webapp和可选标签来命名构建的图像:
这将导致一个名为webapp并标记为tag的图像,从./dir构建。
webapp
tag
./dir
你可以试试:
services: nameis: container_name: hi_my build: . image: hi_my_nameis:v1.0.0
如果你已经构建了你的映像,你可以通过使用docker tag命令re-tag它:
docker tag
docker tag imagename imagename:v1.0 docker tag imagename:v1.0 imagename:v1.1
如果你有多个标签附加到你的存储库,如果你想删除其中一个,你可以使用docker rmi命令:
docker rmi
$ docker rmi imagename:v1.0 Untagged imagename:v1.0
参考:
我想补充一点,您还可以通过环境变量或.env文件管理标记版本。
https://docs.docker.com/compose/environment-variables/#the-env-file
export TAG=1.11
version: '3.3' services: baggins.api.rest: image: my.image.name:${TAG} build: context: ../.. dockerfile: app/Docker/Dockerfile.release ports: ...
docker-compose config来验证
docker-compose config
在我的ci管道中,我的第一个构建被标记为用于运行测试的丢弃值。然后,我将标记更改为latest并重新构建(几乎是即时的,因为它都是缓存的),然后推到注册表。
它为我工作: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
然后运行docker compose build --no-cache输出,如下所示:
docker compose build --no-cache
> 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
上面控制台输出spring-postgres:1.0和spring-postgres:latest
spring-postgres:1.0
spring-postgres:latest