[英]Docker compose obtain env variables from .env file and pass to docker build
我希望能夠為我的 docker 容器配置 env 變量,並在帶有 .env 文件的構建過程中使用它們
我目前有以下.env
文件:
SSH_PRIVATE_KEY=TEST
APP_PORT=8040
我的docker-compose :
version: '3'
services:
companies:
image: companies8
environment:
- SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}
ports:
- ${APP_PORT}:${APP_PORT}
env_file: .env
build:
context: .
args:
- SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}
我的Dockerfile :
FROM python:3.7
# set a directory for the app
COPY . .
#Accept input argument from docker-compose.yml
ARG SSH_PRIVATE_KEY=abcdef
ENV SSH_PRIVATE_KEY $SSH_PRIVATE_KEY
RUN echo $SSH_PRIVATE_KEY
# Pass the content of the private key into the container
RUN mkdir -p /root/.ssh
RUN chmod 400 /root/.ssh
RUN echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
RUN echo "$SSH_PUBLIC_KEY" > /root/.ssh/id_rsa.pub
RUN chmod 400 /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa.pub
RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/id_rsa && ssh-keyscan bitbucket.org > /root/.ssh/known_hosts
RUN ssh -T git@bitbucket.org
#Install the packages
RUN pip install -r v1/requirements.txt
# Tell the port number the container should expose
EXPOSE 8040
# run the command
CMD ["python", "v1/__main__.py"]
我在我的 Windows 上設置了相同的 SSH_PRIVATE_KEY 環境變量,值為“test1”,構建日志給了我結果“test1”來自
ENV SSH_PRIVATE_KEY $SSH_PRIVATE_KEY
RUN echo $SSH_PRIVATE_KEY
不是 .env 文件中的值。
我需要這個是因為我的 requirements.txt 中列出的一些庫在內部存儲庫中,我需要 ssh 來訪問它們,因此需要 ssh 私鑰。 可能有另一種正確的方法來使用它,但它是我想要實現的一般場景 - 將 .env 文件中的 env 變量值傳遞給我的 docker build
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.