dockerdocker-compose

If docker-compose.yml and Dockerfile both specify env vars which has precedence?


If I have Dockerfile:

FROM ubuntu:18.04

ENV NAME=Dockerfile-wins

CMD echo $NAME

and a docker-compose.yml with:

version: '3'

services:

  myservice:
    build: .
    container_name: myservice
    environment:
      - NAME=docker-compose-wins

when I run docker-compose up myservice which one will win out and why?


Solution

  • docker-compose wins out as it overrides whats in the Dockerfile. What is specified in the Dockerfile is a default. and can be overridden by using the docker command which is what docker-compose does.

    This is documented in the docs here

    under the covers essentially docker-compose is running something like

    docker run -e VARIABLE=VALUE