Amazon-Ec2

docker 有捲權限,但 docker-compose 沒有

  • August 12, 2018

我已經在 Amazon Linux 伺服器上安裝了 Docker,並為其授予了sudo usermod -aG docker $USER. /var/lib/docker/volumes當我從命令行啟動我的 Jenkins docker 容器(在 EBS 安裝卷上具有主目錄,安裝在)時;

docker run -d \
--restart=always \
--name=jenkins-core \
--hostname=jenkins-core \
-p 8080:8080 \
-p 50000:50000 \
--env JENKINS_OPTS="--prefix=/core" \
-v jenkins-core:/var/jenkins_home \
jenkins/jenkins:lts

一切正常。但是,當我嘗試從它開始docker-compose up -dsudo docker-compose up -d得到時;

touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

並且 docker 容器進入引導循環,試圖重新啟動。我無法弄清楚為什麼權限錯誤docker-compose但在docker.

我試過sudo chown $(whoami):$(whoami) /usr/local/bin/docker-compose了,但沒有用。我docker-compose從這里安裝;https://docs.docker.com/compose/install/

這是docker-compose.yml還有一個.env變數文件(未附加)

version: "3.6"
services:
 jenkins-core:
   image:           jenkins/jenkins:lts
   container_name:  jenkins-core
   restart:         always
   ports:
     - ${JENKINS_CORE_HOST_PORT_8080}:${JENKINS_PORT_8080}
     - ${JENKINS_CORE_HOST_PORT_50000}:${JENKINS_PORT_50000}
   environment:
     - JENKINS_OPTS=--prefix=${JENKINS_CORE_PREFIX}
     - JAVA_OPTS=-Duser.timezone=${TZ}
   volumes:
     - ${JENKINS_CORE_HOME_DIR}:/var/jenkins_home
 jenkins-integrations:
   image:           jenkins/jenkins:lts
   container_name:  jenkins-integrations
   restart:         always
   ports:
     - ${JENKINS_INTEGRATIONS_HOST_PORT_8080}:${JENKINS_PORT_8080}
     - ${JENKINS_INTEGRATIONS_HOST_PORT_50000}:${JENKINS_PORT_50000}
   environment:
     - JENKINS_OPTS=--prefix=${JENKINS_INTEGRATIONS_PREFIX}
     - JAVA_OPTS=-Duser.timezone=${TZ}
   volumes:
     - ${JENKINS_INTEGRATIONS_HOME_DIR}:/var/jenkins_home
 portainer:
   image:           portainer/portainer
   container_name:  portainer
   restart:         always
   environment:
     - TZ=${TZ}
   ports:
     - ${PORTAINER_PORT_9000}:9000
   volumes:
     - /etc/localtime:/etc/localtime:ro
     - /var/run/docker.sock:/var/run/docker.sock
     - ${DOCKERCONFDIR}/portainer:/data
   command:         -H unix:///var/run/docker.sock
 watchtower:
   image:           v2tec/watchtower
   container_name:  watchtower
   restart:         always
   environment:
     - TZ=${TZ}
   volumes:
     - /var/run/docker.sock:/var/run/docker.sock
   command:         --schedule 0 0 4 * * * --cleanup

請在此處查看:https ://github.com/jenkinsci/docker/blob/master/README.md在用法下。它說NOTE: Avoid using a bind mount from a folder on the host machine into /var/jenkins_home, as this might result in file permission issues (the user used inside the container might not have rights to the folder on the host machine)...

所以你的docker run作品的原因是因為docker 卷-v jenkins-core:/var/jenkins_home在哪裡。jenkins-core但是,在撰寫中,您使用綁定掛載到主機上的某個文件夾。

引用自:https://serverfault.com/questions/925471