0

I have a docker-compose which looks like:

version: '3.2'
services:
  jobsaf-server:
    build: 
      context: ./application
      dockerfile: Dockerfile.production
    container_name: jobsaf-server
    env_file: 
      - ./application/.env
    tty: true
    depends_on:
      - "redis"
      - "mongo"
    links:
      - mongo
      - redis
    volumes:
      - ./application/server:/var/www/app/jobsaf-website/server
      - ./application/public/assets:/var/www/app/jobsaf-website/public/assets
      - ./application/uploads:/var/www/app/jobsaf-website/uploads
      - ./application/sitemaps:/var/www/app/jobsaf-website/sitemaps
      - ./application/rss:/var/www/app/jobsaf-website/rss
      - "/etc/timezone:/etc/timezone:ro"
      - "/etc/localtime:/etc/localtime:ro"
  nginx:
    image: nginx:stable
    tty: true
    env_file: 
      - ./.env
    environment:
      - NGINX_HOST=${APP_HOST}
      - NGINX_PORT=${APP_PORT}
      - PUID=1001
      - PGID=1001
      - TZ=Asia/Kabul
    links:
      - jobsaf-server
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/ssl/star_jobs_af.pem:/etc/ssl/star_jobs_af.pem
      - ./nginx/ssl/jobs.af.key:/etc/ssl/jobs.af.key
      - ./nginx/ssl/star_jobs_af.crt:/etc/ssl/star_jobs_af.crt
    ports:
     - "80:80"
     - "443:443"
  mongo:
    image: mongo:latest
    container_name: mongo
    tty: true
    env_file:
      - ./.env
    volumes:
      - "db-data:/data/db"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${DB_USER}
      - MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
      - MONGO_INITDB_DATABASE=admin
    ports:
      - "0.0.0.0:27017:27017"
  redis:
    image: redis
    container_name: redis
    tty: true
volumes:
  db-data:
    # - /data/db
networks:
  front-tier:
  back-tier:

It build jobsaf-server:latest by default.

what I want is to keep the old tag and build the new one.

let say, while building the images I should pass something similar to this

docker-compose -f docker-compose.production --tag=1.0.1

the above command should build for me and image with tag jobsaf-server:1.0.1

Is it really possible to have such result? Or is there any alternative solution for it

Thanks in advance.

Note: I want to keep the old image, in case if my new image has issue, then I can use the old image.

2 Answers 2

2
version: '3.2'
services:
  jobsaf-server:
    image: jobsaf-server:${TAG}
    build: 
      context: ./application
      dockerfile: Dockerfile.production
...

The best way to supply the tag is with a .env file like this:

TAG=1.0.1

Docker-compose will pick this up automatically.

4
  • Is it passible via terminal? for example, docker-compose build --TAG=1.0.1 May 11, 2019 at 4:02
  • Yes, use --build-arg TAG=1.0.1. if this doesn't work then you can set the environment variable before running docker-compose build
    – Mihai
    May 11, 2019 at 4:07
  • Thanks, can I use docker-compose up -d --build-arg TAG=1.0.1 May 11, 2019 at 4:24
  • I double checked and that is only for build arguments in the Dockerfile. You need to export the TAG env variable first. How exactly to do it depends on your operating system. On Mac should be like: TAG=1.0.1 docker-compose up -d --build
    – Mihai
    May 11, 2019 at 4:30
0

From @Mihai suggestion following steps worked for me, incase if someone else needed.

version: '3.2'
services:
  jobsaf-server:
    image: jobsaf-server:${TAG}
    build: 
      context: ./application
      dockerfile: Dockerfile.production

To build:

Run TAG=1.0 docker-compose build it will create jobsaf-server:1.0

To Up:

Run TAG=1.0 docker-compose up -d

To down:

Run TAG=1.0 docker-compose down

Note: we can add TAG to .env file also by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.