Search code examples
dockerdocker-composedockerfile

Docker ft. .env no cooperation


I'm working on a dynamic DockerFile which can communicate with an .env document. I created this regarding a good local environment for my development team. They use Docker Desktop and create their Docker Dev environment with a Docker-Compose, DockerFile and .env configuration.

Only once I run it inside the Docker Desktop application, everything seems to be fine, it only stops at the part inside the DockerFile when they are depending of the .env document.

DockerFile top part:

# Use a dummy base image
FROM alpine:latest AS dummy

# Define base image
# LANGUAGE: Programming language to use for development (Default: php)
ARG LANGUAGE

# LANGUAGE_VERSION: Language version
ARG LANGUAGE_VERSION

# WEB_SERVER: Web server
ARG WEB_SERVER

# Import environment variables from docker-compose-dev.env
ENV LANGUAGE=$LANGUAGE \
    LANGUAGE_VERSION=$LANGUAGE_VERSION \
    WEB_SERVER=$WEB_SERVER

# Define base image
FROM ${LANGUAGE}:${LANGUAGE_VERSION}-${WEB_SERVER}

RUN if [ -z "${LANGUAGE}" ]; then \
        echo "Error: LANGUAGE argument is missing"; \
        exit 1; \
    fi

# Install required packages and dependencies

My .env example:

# --------------------------------------------------------
#           Docker Compose Configuration
# --------------------------------------------------------

# Programming Language Configuration
# --------------------------------------------------------
# Programming language to use for development
# Options: php, flutter
# Default: php
LANGUAGE=php
# e.g.

My Docker-Compose example:

version: '3.8'

services:
  web:
    env_file:
      - .env
    stdin_open: true
    tty: true
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - ${WEB_SERVER_PORT}:${WEB_SERVER_PORT}
    volumes:
      - ${APP_SOURCE_CODE_PATH}:${CONTAINER_WORKDIR}
    environment:
      - APP_ENV=${APP_ENV}
    networks:
        - mynetwork
    init: true

Some parts are missing regarding arguments, but I only share with you guy's the examples, not the full argument list.

Thanks a lot!


Solution

  • I found the solution. I need to present the environment and arguments inside the docker compose and only make use of .env, otherwise the docker compose didn't saw it.