Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10096

How I can automatically mark a docker php image that has its version as a tag as latest?

For my custom docker php building system I have the following docker-compose

version: "3.7"
services:
  base72:
    build:
      context: .
      dockerfile: ./dockerfiles/7.2/Dockerfile
    image: myapp/php7.2:$VERSION_PHP_72
    stdin_open: true
    tty: true

  develop72:
    build:
      context: .
      dockerfile: ./dockerfiles/7.2/Dockerfile_develop
    links:
      - base72
    image: myapp/php7.2-dev:$VERSION_PHP_72
    volumes:
      - "./www:/var/www/html"
nginx:
    image: nginx:alpine
    ports:
      - 7880:7880
    links:
      - "develop72:develop72"
    volumes:
      - "./www:/var/www/html"
      - "./dist/nginx.conf:/etc/nginx/nginx.conf:ro"

And I build it with the following buildspec.yml

version: 0.2

phases:
  install:
    runtime-versions:
      docker: 18
  pre_build:
   commands:
     - docker login -u $USER -p $TOKEN
  build:
    commands:
     - docker-compose build
  post_build:
    commands:
      - docker-compose up -d
      - docker push myapp/php7.2
      - docker push myapp/php7.2-dev

Via an .env file I implement a versioning system method docker images:

VERSION_PHP_72=20191218212112

Each version number is the current date in the format YYYYMMDDHHMMSS. So I want the version I build from my env file also to be considered the latest. Do you know How I can do that?

Upvotes: 0

Views: 287

Answers (2)

David Maze
David Maze

Reputation: 159495

docker tag can assign a tag to an existing image. I would highly recommend also pushing the timestamped version of your image, so that it's easy to deploy a known-good version and to roll back later if things don't go well. (This is also very important in Kubernetes.)

I'd try a sequence like this:

build:
  commands:
   - docker-compose build
post_build:
  commands:
    - . ./.env && docker tag myapp/php7.2:$VERSION_PHP_72 myapp/php7.2:latest
    - . ./.env && docker push myapp/php7.2:$VERSION_PHP_72 myapp/php7.2:latest
    - . ./.env && docker tag myapp/php7.2-dev:$VERSION_PHP_72 myapp/php7.2-dev:latest
    - . ./.env && docker push myapp/php7.2-dev:$VERSION_PHP_72 myapp/php7.2-dev:latest

Upvotes: 1

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10096

For the solution you need the following 2 components:

  1. docker commit that will allow you to specify a tag to a container
  2. docker-compose ps -q That will allow you to get the container is of a specific service.

The commands above will be combined into this oneliner:

docker commit $(docker-compose ps -q ^service^) ^image_name^:latest

Where: * ^service^: The docker-compose service you define in docker-compose.yml * ^image_name^: The image you want to push into the container as latest.

So in order to push the myapp/php7.2-dev you will need to run:

docker commit $(docker-compose ps -q develop72) myapp/php7.2-dev:latest

Therefore the end codebuild.yml will be:

version: 0.2

phases:
  install:
    runtime-versions:
      docker: 18
  pre_build:
   commands:
     - docker login -u $USER -p $TOKEN
  build:
    commands:
     - docker-compose build
  post_build:
    commands:
      - docker-compose up -d
      - docker commit $(docker-compose ps -q base72) myapp/php7.2:latest
      - docker commit $(docker-compose ps -q develop72) myapp/php7.2-dev:latest
      - docker push myapp/php7.2
      - docker push myapp/php7.2-dev

Upvotes: 0

Related Questions