docker-compose: How can I pass ARG as variable to docker-compose?

Viewed 127

I have this docker-compose file

version: "3.7"
services:
  develop:
    build:
      args:
        user: sineverba
        uid: 1000
      context: ./
    container_name: tpaw-develop
    image: tpaw-develop:0.1.0
    volumes:
      - ./:/home/sineverba/app
    networks:
      - develop-tpaw

networks:
  develop-tpaw:
    driver: bridge

I can launch it with docker-compose run --rm develop.

I want pass that args as variable from docker-compose command and not hard-coding it.

Is it possible?

I did try with

version: "3.7"
services:
  develop:
    build:
      args:
        user: ${CURRENT_USER}
        uid: 1000
      context: ./

And launch

CURRENT_USER=$(whoami) docker-compose build app

But Docker created a container with user $user inside

PS this is the main Dockerfile

FROM php:7.4-cli

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Setup PHPXDebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
#WORKDIR /var/www
WORKDIR /home/$user/app

USER $user

ENTRYPOINT /bin/bash
0 Answers
Related