Skip to content

Commit 9f8a0fb

Browse files
authored
Build: Fallback to Containerfile if Dockerfile not found (#812)
Closes #782 #99 If a Dockerfile can't be found in the context dir, check for the common alternative Containerfile. This does not implement .containerignore also, solely Containerfile for now. This also funnily enough fixes us not checking for the Dockerfile IN the context directory.. woops. ## Type of Change - [x] Bug fix - [x] New feature - [ ] Breaking change - [ ] Documentation update ## Testing - [x] Tested locally - [x] Added/updated tests - [x] Added/updated docs
1 parent f2b3cbd commit 9f8a0fb

File tree

4 files changed

+193
-9
lines changed

4 files changed

+193
-9
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import Logging
19+
20+
public struct BuildFile {
21+
/// Tries to resolve either a Dockerfile or Containerfile relative to contextDir.
22+
/// Checks for Dockerfile, then falls back to Containerfile.
23+
public static func resolvePath(contextDir: String, log: Logger? = nil) throws -> String? {
24+
// Check for Dockerfile then Containerfile in context directory
25+
let dockerfilePath = URL(filePath: contextDir).appendingPathComponent("Dockerfile").path
26+
let containerfilePath = URL(filePath: contextDir).appendingPathComponent("Containerfile").path
27+
28+
let dockerfileExists = FileManager.default.fileExists(atPath: dockerfilePath)
29+
let containerfileExists = FileManager.default.fileExists(atPath: containerfilePath)
30+
31+
if dockerfileExists && containerfileExists {
32+
log?.info("Detected both Dockerfile and Containerfile, choosing Dockerfile")
33+
return dockerfilePath
34+
}
35+
36+
if dockerfileExists {
37+
return dockerfilePath
38+
}
39+
40+
if containerfileExists {
41+
return containerfilePath
42+
}
43+
44+
return nil
45+
}
46+
}

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension Application {
3232
public static var configuration: CommandConfiguration {
3333
var config = CommandConfiguration()
3434
config.commandName = "build"
35-
config.abstract = "Build an image from a Dockerfile"
35+
config.abstract = "Build an image from a Dockerfile or Containerfile"
3636
config._superCommandName = "container"
3737
config.helpNames = NameSpecification(arrayLiteral: .customShort("h"), .customLong("help"))
3838
return config
@@ -64,7 +64,7 @@ extension Application {
6464
var cpus: Int64 = 2
6565

6666
@Option(name: .shortAndLong, help: ArgumentHelp("Path to Dockerfile", valueName: "path"))
67-
var file: String = "Dockerfile"
67+
var file: String?
6868

6969
@Option(name: .shortAndLong, help: ArgumentHelp("Set a label", valueName: "key=val"))
7070
var label: [String] = []
@@ -186,7 +186,23 @@ extension Application {
186186
throw ValidationError("builder is not running")
187187
}
188188

189-
let dockerfile = try Data(contentsOf: URL(filePath: file))
189+
let buildFilePath: String
190+
if let file = self.file {
191+
buildFilePath = file
192+
} else {
193+
guard
194+
let resolvedPath = try BuildFile.resolvePath(
195+
contextDir: self.contextDir,
196+
log: log
197+
)
198+
else {
199+
throw ValidationError("failed to find Dockerfile or Containerfile in the context directory \(self.contextDir)")
200+
}
201+
buildFilePath = resolvedPath
202+
}
203+
204+
let buildFileData = try Data(contentsOf: URL(filePath: buildFilePath))
205+
190206
let systemHealth = try await ClientHealthCheck.ping(timeout: .seconds(10))
191207
let exportPath = systemHealth.appRoot
192208
.appendingPathComponent(Application.BuilderCommand.builderResourceDir)
@@ -264,7 +280,7 @@ extension Application {
264280
contentStore: RemoteContentStoreClient(),
265281
buildArgs: buildArg,
266282
contextDir: contextDir,
267-
dockerfile: dockerfile,
283+
dockerfile: buildFileData,
268284
labels: label,
269285
noCache: noCache,
270286
platforms: [Platform](platforms),
@@ -351,9 +367,7 @@ extension Application {
351367
}
352368

353369
public func validate() throws {
354-
guard FileManager.default.fileExists(atPath: file) else {
355-
throw ValidationError("Dockerfile does not exist at path: \(file)")
356-
}
370+
// NOTE: We'll "validate" the Dockerfile later.
357371
guard FileManager.default.fileExists(atPath: contextDir) else {
358372
throw ValidationError("context dir does not exist \(contextDir)")
359373
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import Logging
19+
import Testing
20+
21+
@testable import ContainerBuild
22+
23+
@Suite class BuildFileResolvePathTests {
24+
private var baseTempURL: URL
25+
private let fileManager = FileManager.default
26+
27+
init() throws {
28+
self.baseTempURL = URL.temporaryDirectory
29+
.appendingPathComponent("BuildFileTests-\(UUID().uuidString)")
30+
try fileManager.createDirectory(at: baseTempURL, withIntermediateDirectories: true, attributes: nil)
31+
}
32+
33+
deinit {
34+
try? fileManager.removeItem(at: baseTempURL)
35+
}
36+
37+
private func createFile(at url: URL, content: String = "") throws {
38+
try fileManager.createDirectory(
39+
at: url.deletingLastPathComponent(),
40+
withIntermediateDirectories: true,
41+
attributes: nil
42+
)
43+
let created = fileManager.createFile(
44+
atPath: url.path,
45+
contents: content.data(using: .utf8),
46+
attributes: nil
47+
)
48+
try #require(created)
49+
}
50+
51+
@Test func testResolvePathFindsDockerfile() throws {
52+
let contextDir = baseTempURL.path
53+
let dockerfilePath = baseTempURL.appendingPathComponent("Dockerfile")
54+
try createFile(at: dockerfilePath, content: "FROM alpine")
55+
56+
let result = try BuildFile.resolvePath(contextDir: contextDir)
57+
58+
#expect(result == dockerfilePath.path)
59+
}
60+
61+
@Test func testResolvePathFindsContainerfile() throws {
62+
let contextDir = baseTempURL.path
63+
let containerfilePath = baseTempURL.appendingPathComponent("Containerfile")
64+
try createFile(at: containerfilePath, content: "FROM alpine")
65+
66+
let result = try BuildFile.resolvePath(contextDir: contextDir)
67+
68+
#expect(result == containerfilePath.path)
69+
}
70+
71+
@Test func testResolvePathPrefersDockerfileWhenBothExist() throws {
72+
let contextDir = baseTempURL.path
73+
let dockerfilePath = baseTempURL.appendingPathComponent("Dockerfile")
74+
let containerfilePath = baseTempURL.appendingPathComponent("Containerfile")
75+
try createFile(at: dockerfilePath, content: "FROM alpine")
76+
try createFile(at: containerfilePath, content: "FROM ubuntu")
77+
78+
let result = try BuildFile.resolvePath(contextDir: contextDir)
79+
80+
#expect(result == dockerfilePath.path)
81+
}
82+
83+
@Test func testResolvePathReturnsNilWhenNoFilesExist() throws {
84+
let contextDir = baseTempURL.path
85+
86+
let result = try BuildFile.resolvePath(contextDir: contextDir)
87+
88+
#expect(result == nil)
89+
}
90+
91+
@Test func testResolvePathWithEmptyDirectory() throws {
92+
let emptyDir = baseTempURL.appendingPathComponent("empty")
93+
try fileManager.createDirectory(at: emptyDir, withIntermediateDirectories: true, attributes: nil)
94+
95+
let result = try BuildFile.resolvePath(contextDir: emptyDir.path)
96+
97+
#expect(result == nil)
98+
}
99+
100+
@Test func testResolvePathWithNestedContextDirectory() throws {
101+
let nestedDir = baseTempURL.appendingPathComponent("project/build")
102+
try fileManager.createDirectory(at: nestedDir, withIntermediateDirectories: true, attributes: nil)
103+
let dockerfilePath = nestedDir.appendingPathComponent("Dockerfile")
104+
try createFile(at: dockerfilePath, content: "FROM node")
105+
106+
let result = try BuildFile.resolvePath(contextDir: nestedDir.path)
107+
108+
#expect(result == dockerfilePath.path)
109+
}
110+
111+
@Test func testResolvePathWithRelativeContextDirectory() throws {
112+
let nestedDir = baseTempURL.appendingPathComponent("project")
113+
try fileManager.createDirectory(at: nestedDir, withIntermediateDirectories: true, attributes: nil)
114+
let dockerfilePath = nestedDir.appendingPathComponent("Dockerfile")
115+
try createFile(at: dockerfilePath, content: "FROM python")
116+
117+
// Test with the absolute path
118+
let result = try BuildFile.resolvePath(contextDir: nestedDir.path)
119+
120+
#expect(result == dockerfilePath.path)
121+
}
122+
}

docs/command-reference.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ container run -e NODE_ENV=production --cpus 2 --memory 1G node:18
8989

9090
### `container build`
9191

92-
Builds an OCI image from a local build context. It reads a Dockerfile (default `Dockerfile`) and produces an image tagged with `-t` option. The build runs in isolation using BuildKit, and resource limits may be set for the build process itself.
92+
Builds an OCI image from a local build context. It reads a Dockerfile (default `Dockerfile`) or Containerfile and produces an image tagged with `-t` option. The build runs in isolation using BuildKit, and resource limits may be set for the build process itself.
93+
94+
When no `-f/--file` is specified, the build command will look for `Dockerfile` first, then fall back to `Containerfile` if `Dockerfile` is not found.
9395

9496
**Usage**
9597

@@ -106,7 +108,7 @@ container build [OPTIONS] [CONTEXT-DIR]
106108
* `-a, --arch <value>`: Add the architecture type to the build
107109
* `--build-arg <key=val>`: Set build-time variables
108110
* `-c, --cpus <cpus>`: Number of CPUs to allocate to the builder container (default: 2)
109-
* `-f, --file <path>`: Path to Dockerfile (default: Dockerfile)
111+
* `-f, --file <path>`: Path to Dockerfile
110112
* `-l, --label <key=val>`: Set a label
111113
* `-m, --memory <memory>`: Amount of builder container memory (1MiByte granularity), with optional K, M, G, T, or P suffix (default: 2048MB)
112114
* `--no-cache`: Do not use cache

0 commit comments

Comments
 (0)