如何在Dockerfile中更改文件权限?

时间:2019-05-08 11:48:58

标签: docker sonarqube dockerfile

我有以下Dockerfile:

FROM sonarqube

ADD https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
RUN chmod +r /opt/sonarqube/extensions/plugins/ && chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/

然后出现以下错误:

Step 3/3 : RUN chmod +x /opt/sonarqube/extensions/plugins/ && chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/
 ---> Running in 0fd0abc88b73
chown: changing ownership of '/opt/sonarqube/extensions/plugins/sonar-gitlab-plugin-4.0.0.jar': Operation not permitted
The command '/bin/sh -c chmod +x /opt/sonarqube/extensions/plugins/ && chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/' returned a non-zero code: 1

如果我这样做:

FROM sonarqube


ADD https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
RUN sudo chmod +r /opt/sonarqube/extensions/plugins/ && sudo chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/

我收到以下错误:

Step 3/3 : RUN sudo chmod +x /opt/sonarqube/extensions/plugins/ && sudo chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/
 ---> Running in c7dc030ea2ac
/bin/sh: 1: sudo: not found
The command '/bin/sh -c sudo chmod +x /opt/sonarqube/extensions/plugins/ && sudo chown -R sonarqube.sonarqube /opt/sonarqube/extensions/plugins/' returned a non-zero code: 127

如何在Dockerfile中更改文件的权限?

2 个答案:

答案 0 :(得分:3)

chmod +r /opt/sonarqube/extensions/plugins/引发错误,因为声纳容器以用户sonarqube身份运行(请参阅Dockerfile中的USER sonarqube命令:https://github.com/SonarSource/docker-sonarqube/blob/master/7.7-community/Dockerfile

sudo未安装在映像中,因此您将无法使用sudo运行命令。而是,如M. Alekseev所述,将用户更改为root并运行您的自定义命令。

USER root
RUN ...
RUN ...

# switch back to user sonarqube for security
USER sonarqube

建议您在安装自定义软件包等后切换回用户sonarqube

请注意,您可能需要设置root用户随后创建的文件的权限。

答案 1 :(得分:1)

如果您想以root用户身份运行命令,请使用const string ThumbUrl = "https://graph.windows.net/myorganization/users/{0}/thumbnailPhoto?api-version=1.6"; // Attempts to retrieve the thumbnail image for the specified user, with fallback. // Returns: Fully formatted string for supplying as the src attribute value of an img tag. private string GetUserThumbnail(string userId) { string thumbnail = "some base64 encoded fallback image"; string mediaType = "image/jpg"; // whatever your fallback image type is string requestUrl = string.Format(ThumbUrl, userId); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", GetToken()); HttpResponseMessage response = client.GetAsync(requestUrl).Result; if (response.IsSuccessStatusCode) { // Read the response as a byte array var responseBody = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); // The headers will contain information on the image type returned mediaType = response.Content.Headers.ContentType.MediaType; // Encode the image string thumbnail = Convert.ToBase64String(responseBody); } return $"data:{mediaType};base64,{thumbnail}"; } // Factored out for use with other calls which may need the token private string GetToken() { return HttpContext.Current.Session["Token"] == null ? string.Empty : HttpContext.Current.Session["Token"].ToString(); } 行来切换到root用户,然后执行命令,而不是使用sudo