转: https://stackoverflow.com/questions/56082506/docker-compose-build-with-new-tag

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:

  

t 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

 

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


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.

 

posted on 2022-10-31 19:34  swing07  阅读(646)  评论(0编辑  收藏  举报