无法使用docker run -e或--env-file选项设置环境变量

时间:2019-12-16 03:25:35

标签: docker environment-variables dockerfile

Dockerfile

apply_fun <- function(temp, name) {
   mod <- lm(lrecaudacion~lpib, temp)
   data.frame(name = name, a = mod$coefficients[1], b = mod$coefficients[2])
}

然后我跑

purrr::imap_dfr(split(boyanza, boyanza$Sector), apply_fun)

#          name     a    b
#1 Agropecuaria -8.79 1.10
#2  Manufactura -5.98 1.12

使用FROM node:10 # Create app directory WORKDIR /usr/server # 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 /usr/server/ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . /usr/server/ EXPOSE 3000 CMD [ "node", "./bin/www.js" ]`

控制台输出
    docker run -d -p 3000:3000 chatapp-back 
    -e DB_HOST="mongodb://localhost:27017/" 
    -e DB_USER="user" 
    -e DB_NAME="dbname" 
    -e DB_PASS="dbpass" 
    -e JWT_SECRET="my-jwt-secret"

我也尝试过docker logs <container-id>

但是控制台返回了

[eval]:1
JWT_SECRET=my-jwt-secret
^

ReferenceError: my is not defined
    at [eval]:1:1
    at Script.runInThisContext (vm.js:122:20)
    at Object.runInThisContext (vm.js:329:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at evalScript (internal/bootstrap/node.js:590:27)
    at startup (internal/bootstrap/node.js:265:9)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

我在这里做错了

我使用了docker run documentation

this article

and this article

1 个答案:

答案 0 :(得分:1)

重新排列docker run命令,因为节点基础docker映像的default entrypointnode,因此容器将--env-file .env视为节点进程的参数。

docker run -d -p 3000:3000  --env-file .env chatapp-back

此外,您可以在运行主进程之前进行验证。

docker run -it -p 3000:3000 --env-file .env chatapp-back -e "console.log(process.env)"