事实证明,Elastic Beanstalk 确实只是从任务定义中删除了特权标志。您可以通过将其包含在您在应用程序包中上传到 EB 的 Dockerrun.aws.json 文件中来确认这一点,然后转到 aws 中的 ECS 控制面板并查看 EB 为您的容器集群创建的任务定义。特权标志现在将设置为 false。这实际上是古怪的。
为了解决这个问题,我不得不花费很多时间来编写一个 ebextension,它等待部署以启动所有容器,然后循环 Dockerrun.aws.json 并提取任何应该具有特权的容器定义然后对这些容器的正在运行的非特权版本进行 docker 检查,然后使用来自 docker inspect 的现有运行配置停止并重新运行它们,但特权标志设置回 true。 ebextension 的文件在此处的要点中提供:https://gist.github.com/therealjessesanford/5a012218889831926169
注意:您不能在 Dockerrun.aws.json 文件的同一容器定义节中使用 essential: true 和 privileged: true。这两个参数与此 hack 是互斥的。
我还将在此处为 Google 员工内嵌它们:
.ebextensions/0001_restart_privileged_containers.config
container_commands:
01-move-restart-hook:
command: cp -f .ebextensions/files/00_restart_containers_with_privileges.sh /opt/elasticbeanstalk/hooks/appdeploy/post/00_restart_containers_with_privileges.sh && chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/post/00_restart_containers_with_privileges.sh
02-move-stop-hook:
command: cp -f .ebextensions/files/02stop_privileged_containers.sh /opt/elasticbeanstalk/hooks/appdeploy/pre/02stop_privileged_containers.sh && chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/pre/02stop_privileged_containers.sh
.ebextensions/files/00_restart_containers_with_privileges.sh
#!/bin/bash
set -ex
. /opt/elasticbeanstalk/hooks/common.sh
EB_CONFIG_APP_STAGING=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
export DOCKERRUN_AWS_JSON=$EB_CONFIG_APP_STAGING/Dockerrun.aws.json
while read -r container_short_name; do
CURRENT_CONTAINER_ID=$(docker ps --no-trunc -q --filter=name=.$container_short_name)
CONTAINER_LONG_NAME=$(docker inspect --format='{{.Name}}' $CURRENT_CONTAINER_ID)
CURRENT_CONFIG=$(docker inspect --format='{{json .Config}}' $CURRENT_CONTAINER_ID)
NEW_HOST_CONFIG=$(docker inspect --format='"HostConfig":{{json .HostConfig}}' $CURRENT_CONTAINER_ID | sed 's/\"Privileged\":false/\"Privileged\":true/I')
echo "Stopping unprivileged $CONTAINER_LONG_NAME"
docker stop $CURRENT_CONTAINER_ID
docker rm $CURRENT_CONTAINER_ID
NEW_CONTAINER_ID=$(curl --unix-socket /var/run/docker.sock -X POST -H "Content-Type: application/json" http:/containers/create?name=$CONTAINER_LONG_NAME -d "${CURRENT_CONFIG%?},$NEW_HOST_CONFIG}" | jq -r '.Id')
echo "Starting privileged $CONTAINER_LONG_NAME"
docker start $NEW_CONTAINER_ID
sed -i "s/$CURRENT_CONTAINER_ID/$NEW_CONTAINER_ID/g" /var/lib/ecs/data/ecs_agent_data.json
done <<< "$(jq -r '.containerDefinitions[] | select(.privileged == true) | .name' $DOCKERRUN_AWS_JSON)"
.ebextensions/files/02stop_priviliged_containers.sh
#!/bin/bash
set -ex
. /opt/elasticbeanstalk/hooks/common.sh
EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
export DOCKERRUN_AWS_JSON=$EB_CONFIG_APP_CURRENT/Dockerrun.aws.json
while read -r container_short_name; do
CURRENT_CONTAINER_ID=$(docker ps -q --filter=name=.$container_short_name)
if [[ ! -z $CURRENT_CONTAINER_ID && "FOOBAR$CURRENT_CONTAINER_ID" != "FOOBAR" ]]; then
CONTAINER_LONG_NAME=$(docker inspect --format='{{.Name}}' $CURRENT_CONTAINER_ID)
echo "Stopping unprivileged $CONTAINER_LONG_NAME"
docker stop $CURRENT_CONTAINER_ID || true
docker rm $CURRENT_CONTAINER_ID || true
fi
done <<< "$(jq -r '.containerDefinitions[] | select(.privileged == true) | .name' $DOCKERRUN_AWS_JSON)"
./Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "happy_container_name",
"image": "tutum.co/happy/happy_container",
"memory": 128,
"essential": false,
"privileged": true
}
]
}