2017-09-27 214 views
-1

內部系統環境變量我是很新的泊塢窗。我有一個需求,docker容器應該讀取像AWS_INSTANCE_ID這樣的系統環境變量,它是aws中的一個實例標識。通常啓動時,我曾經使用shell腳本如下如何閱讀泊塢窗容器

EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`" 
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id' 
export EC2_INSTANCE_ID=$EC2_INSTANCE_ID 
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`" 
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone' 
export EC2_AVAIL_ZONE=$EC2_AVAIL_ZONE 
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`" 

當前Dockerfile

FROM node:boron 
WORKDIR /usr/src/app 
# Install app dependencies 
COPY package.json . 
RUN npm install 
# Bundle app source 
COPY . . 
CMD [ "npm", "start" ] 

我如何可以讀取泊塢容器這些系統變量添加這些環境變量?

+0

'搬運工運行-e .. .'? – BMitch

+0

的[我如何通過環境變量來Docker容器?(https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers) – juanlumn

+0

@BMitch可能的複製我無法像AWS那樣運行它。我正在使用ECS服務來處理這個問題。 –

回答

1

在你的項目中創建一個shell腳本

env.sh

EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`" 
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id' 
export EC2_INSTANCE_ID=$EC2_INSTANCE_ID 
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`" 
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone' 
export EC2_AVAIL_ZONE=$EC2_AVAIL_ZONE 
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`" 

修改您dockerfile以下

FROM node:boron 
WORKDIR /usr/src/app 
# Install app dependencies 
COPY package.json . 
RUN npm install 
# Bundle app source 
COPY . . 
COPY env.sh /etc/profile.d/awsenv.sh 
ENTRYPOINT ["/bin/sh", "-lc"] 
CMD ["env && exec npm start"] 

現在圖像啓動時它會自動有環境變量