Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

Dockerfile doesnt get the arguments passed from docker compose

I'm unable to pass my arguments from docker-compose.yml into build

docker-compose.yml

version: '2'
services:
  mail_prod:
    #env_file:
    #  - .env
    build:
      context: ./server
      dockerfile: ./Dockerfile
      args:
        - FROM_VERIFIED_EMAIL=sadasd
        - HOST_SMTP=asdasd
        - PORT_SMTP=asdasdsad
        - NAME_SMTP=asdasd
        - PASS_SMTP=iasdasdasd
        - AUTH_TOKEN_MAILSEND=asdasdasdasd
    ports:
      - 8080:8080
    restart: always

Dockerfile:

FROM node:12

# Create app directory
WORKDIR /usr/src/app

ARG HOST_SMTP
ARG PORT_SMTP
ARG NAME_SMTP
ARG PASS_SMTP
ARG AUTH_TOKEN_MAILSEND
ARG FROM_VERIFIED_EMAIL

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080

CMD node server.js $HOST_SMTP $PORT_SMTP $NAME_SMTP $PASS_SMTP $AUTH_TOKEN_MAILSEND $FROM_VERIFIED_EMAIL

server.js

'use strict'

// Print the arguments which are passed
var myArgs = process.argv.slice(2);
console.log('myArgs: ', myArgs);

This is what I get in output when I run the container:

mail_prod_1 | myArgs: []

Where am i going wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

from the docker-compose documenentation - Args definition:

Add build arguments, which are environment variables accessible only during the build process.

So one way I can suggest is to pass those arguments into environment variables for later use by your application (right after your ARG lines):

ENV HOST_SMTP=$HOST_SMTP
ENV PORT_SMTP=$PORT_SMTP
ENV NAME_SMTP=$NAME_SMTP
ENV PASS_SMTP=$PASS_SMTP
ENV AUTH_TOKEN_MAILSEND=$AUTH_TOKEN_MAILSEND
ENV FROM_VERIFIED_EMAIL=$FROM_VERIFIED_EMAIL

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...