WSL DOCKER 设置中 Java docker-compose 权限被拒绝

问题描述 投票:0回答:1

我有一个奇怪的设置,我无法在 wsl docker 设置中构建 java 映像,但我可以在 Windows Docker 设置中进行构建。错误是权限被拒绝:未知。我read我需要在dockerfile中添加

chmod +x file.sh
但仍然是同样的错误。问题是我可以在 Windows Docker 中构建 docker 镜像,即使 dockerfile 中没有 chmod

结构

.
├── docker
│   ├── nginx
│   │   └── conf.d
│   ├── Dockerfile
│   └── entrypoint.sh
├── project
└── docker-compose.yml

docker-compose.yml

version: "3.8"
services:
  app:
    container_name: myapp-app
    # restart: always
    build: ./docker
    ports:
      - "8080:8080"
      - "5005:5005"
    tty: true
    volumes:
      - ./project:/var/www/project:cached
      - ./docker/entrypoint.sh:/usr/local/bin/entrypoint.sh:cached
      - ./.m2:/root/.m2

   ... other setup not related

Dockerfile

FROM eclipse-temurin:17-jdk-alpine

RUN apk add --no-cache bash procps curl tar

# common for all images
ENV MAVEN_HOME /usr/share/maven

COPY --from=maven:3.9.5-eclipse-temurin-11 ${MAVEN_HOME} ${MAVEN_HOME}
COPY --from=maven:3.9.5-eclipse-temurin-11 /usr/local/bin/mvn-entrypoint.sh /usr/local/bin/mvn-entrypoint.sh
COPY --from=maven:3.9.5-eclipse-temurin-11 /usr/share/maven/ref/settings-docker.xml /usr/share/maven/ref/settings-docker.xml

RUN ln -s ${MAVEN_HOME}/bin/mvn /usr/bin/mvn

ARG MAVEN_VERSION=3.9.5
ARG USER_HOME_DIR="/root"
# ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

# WORKDIR
WORKDIR /var/www/project

#Start application
RUN ["chmod", "+x", "/usr/local/bin/entrypoint.sh"] # the chmod command recommended.
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

CMD [""]

入口点.sh

#!/bin/sh
# You can set here anything you need before starting the spring boot application

clear
export JAVA_TOOL_OPTIONS=-Dspring-boot.run.jvmArguments=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
while true; do
    sleep 1
done

错误堆栈跟踪

ERROR: for myapp-app  Cannot start service app: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/usr/local/bin/entrypoint.sh": permission denied: unknown

ERROR: for app  Cannot start service app: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/usr/local/bin/entrypoint.sh": permission denied: unknown
ERROR: Encountered errors while bringing up the project.
make: *** [Makefile:4: up] Error 1
java docker docker-compose windows-subsystem-for-linux
1个回答
0
投票

说明

      - ./docker/entrypoint.sh:/usr/local/bin/entrypoint.sh:cached

您的

chmod +x
没有任何效果,因为您稍后使用绑定挂载覆盖了该文件。如果该文件在本地没有可执行标志 (
x
),则容器中不会有它。

事实上,我认为你的 Dockerfile 在当前状态下根本不会构建,这是错误的,它混合了

RUN
CMD
的语法:

RUN ["chmod", "+x", "/usr/local/bin/entrypoint.sh"]

解决方案

选项 1:首先将入口点复制到图像

...
COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

还要从您的

docker-compose.yml

上移除绑定安装座
    volumes:
      - ./project:/var/www/project:cached
      - ./.m2:/root/.m2

选项 2:如果可能,请设置本地计算机上已有的权限

我不太明白你的系统设置到底如何,所以请注意,这不能直接在 Windows 上运行,但可能在 WSL 中运行(如果不起作用,请检查 chmod WSL (Bash) 不起作用) .

无论如何,您可以在挂载之前将文件权限设置为可执行,因此在WSL内部运行

chmod +x ./docker/entrypoint.sh

这样,您就可以保留绑定挂载并从 Dockerfile 中删除错误的

RUN
指令。

此解决方案还可以增强选项 1:那么您也不需要该解决方案中的

RUN chmod +x ...
部分。

© www.soinside.com 2019 - 2024. All rights reserved.