Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose Overriding

I have two Docker Compose files, docker-compose.yml looks like this

version: '2'
services:
  mongo:
    image: mongo:3.2
    restart: always
    volumes:
      - /mnt/data/mongodb/data/db:/data/db
  redis:
    image: redis:3
    restart: always
  application:
    build: .
    image: localregistry:5000/mz_application:latest
    ports:
      - "3000:3000"
    links:
      - mongo:mongo
      - redis:redis
    restart: always

and another file used when I do deployment docker-deploy.yml

version: '2'
services:
  application:
    image: localregistry5000/mz_application:latest
    links:
      - mongo:mongo
      - redis:redis
    restart: always

  nginx:
    restart: always
    build: ./nginx/
    ports:
      - "80:80"
    volumes_from:
      - application
    restart: always
    links:
      - application:application
    command: /bin/bash -c "nginx -g 'daemon off;'"
    tty: false

So the idea behind using those files is as follows. When I run

docker-compose build

it builds and pushes an image to our local docker registry.

docker-compose -f docker-compose.yml -f docker-deploy.yml build &&
docker-compose -f docker-compose.yml -f docker-deploy.yml up -d

To deploy and run application pulled from the local registry rather than being rebuilt.

Unfortunately it rebuilds it because when I override application, build is still present as it is taken from docker-compose.yml file. How to I remove it ? Is there a way to do this - building and pushing and image using one docker-compose file and then only pull using docker compose file override ?

like image 997
EvgeniySharapov Avatar asked Feb 01 '17 18:02

EvgeniySharapov


People also ask

What is a docker compose override?

The docker-compose. override. yml is the configuration file where you can override existing settings from docker-compose. yml or even add completely new services. By default, this file does not exist and you must create it.

Does docker compose override Dockerfile?

Docker-compose command doesn't override Dockerfile CMD.

Does docker compose command override entrypoint?

All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.

Does docker compose down destroy volumes?

Stops containers and removes containers, networks, volumes, and images created by up .


1 Answers

First of all, if you let me, I found several things to consider. I don't know why do you want to have the same two build destination images but anyway I'll try to answer solutions better than ask why do you try to do that.

  1. Changing image name you build different images. Maybe you could try with "deploy-version" for example.
  2. If you don't want to change image name because you prefer (I don't know why) build it twice, you could try following command, setting different container_name in compose files:

Furthermore, note that in your docker-deploy.yml file, for application service, you're not defining build section, so, docker-compose -f docker-deploy.yml build will build image with default Dockerfile and path: .

services:
  application:
    build: .
    image: localregistry:5000/mz_application:latest
    ports:
      - "3000:3000"
    links:
      - mongo:mongo
      - redis:redis
    restart: always
    container_name: app_compose

services:
  application:
    image: localregistry:5000/mz_application:latest
    links:
      - mongo:mongo
      - redis:redis
    restart: always
    container_name: app_deploy
  1. Consider that multiple docker-compose build should be defined with different images. Maybe you are trying to deploy different containers from the same image. I see that only difference between application service definition in both docker-compose files is ports declaration, because build: . is the same that don't put anything. When you use docker-compose build command, it ignores ports: section. When you use docker-compose up command, it ignores build: section.

Definitively, if you want to get up a concrete service without rebuilding avoiding image rewriting, you can also do:

docker-compose -f docker-compose.yml build
docker-compose -f docker-deploy.yml run -d nginx /bin/bash -c "nginx -g 'daemon off;'"
like image 135
Alejandro Galera Avatar answered Sep 22 '22 00:09

Alejandro Galera