How to define build-args in docker-compose?

后端 未结 3 565
情书的邮戳
情书的邮戳 2020-12-29 03:30

I have the following docker-compose file

version: \'3\'
services:
    node1:
            build: node1
            image: node1
            container_name: n         


        
3条回答
  •  猫巷女王i
    2020-12-29 04:04

    You can define your args with your build command of docker-compose.

    Example Dockerfile:

    FROM nginx:1.13
    
    RUN apt-get -y update && apt-get install -y \
        apache2-utils && \
        rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
    
    ARG username
    ARG password
    
    RUN htpasswd -bc /etc/nginx/.htpasswd $username $password
    

    docker-compose file:

    version: '3'
    services:
      node1:
         build: ./dir
    

    The ./dir contains the Dockerfile and I build with this command:

    docker-compose build --build-arg username="my-user" --build-arg password="my-pass"
    

    I see already:

    Step 5/5 : RUN htpasswd -bc /etc/nginx/.htpasswd $username $password
     ---> Running in 80735195e35d
    Adding password for user my-user
     ---> c52c92556825
    

    I bring my stack up:

    docker-compose up -d
    

    Now I can check inside the nginx container and my username and (encrypted) password are there:

    docker@default:~/test$ docker exec -it test_node1_1 bash
    root@208e4d75e2bd:/# cat /etc/nginx/.htpasswd
    my-user:$apr1$qg4I/5RO$KMaOPvjbnKdZH37z2WYfe1
    

    Using this method your able to pass build args to your docker-compose stack.

提交回复
热议问题