Skip to content

Commit 4d791f4

Browse files
authored
fix: Add default file permission (755 for image build) (#1176)
1 parent 1d6cbea commit 4d791f4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

docs/api/create_docker_image.md

+11
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,14 @@ _ = new ImageFromDockerfileBuilder()
101101
!!!tip
102102

103103
Testcontainers for .NET detects your Docker host configuration. You do **not** have to set the Docker daemon socket.
104+
105+
## Known issues
106+
107+
- When building an image using Testcontainers for .NET and switching the user's context (`USER` statement) in a Dockerfile, the user won't automatically become the [owner](https://github.com/testcontainers/testcontainers-dotnet/issues/1171#issuecomment-2099197840) of the working directory, which seems to be the case when building the image from the CLI. If the running process requires write access to the working directory, it is necessary to set the permissions explicitly (the base image in this example already contains the user `app`):
108+
109+
```Dockerfile
110+
FROM mcr.microsoft.com/dotnet/sdk:8.0
111+
WORKDIR /app
112+
RUN chown app:app .
113+
USER app
114+
```

src/Testcontainers/Images/DockerfileArchive.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Images
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Runtime.InteropServices;
78
using System.Text;
89
using System.Text.RegularExpressions;
910
using System.Threading;
@@ -148,7 +149,12 @@ public async Task<string> Tar(CancellationToken ct = default)
148149
using (var inputStream = new FileStream(absoluteFilePath, FileMode.Open, FileAccess.Read))
149150
{
150151
var entry = TarEntry.CreateTarEntry(relativeFilePath);
151-
entry.Size = inputStream.Length;
152+
entry.TarHeader.Size = inputStream.Length;
153+
154+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
155+
{
156+
entry.TarHeader.Mode = (int)Unix.FileMode755;
157+
}
152158

153159
await tarOutputStream.PutNextEntryAsync(entry, ct)
154160
.ConfigureAwait(false);

0 commit comments

Comments
 (0)