Skip to content

Commit e94c3f8

Browse files
committed
support local registry and test it
1 parent 0aa3e2d commit e94c3f8

3 files changed

Lines changed: 110 additions & 12 deletions

File tree

adr-003-docker-image-download.md

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ graph TD
6565
A1[Simulated Fetching Logic]
6666
A2[Basic Image Listing]
6767
A3[Placeholder Pull Function]
68+
A4[Load Images from .tar Files]
69+
A5[Run Locally Loaded Images]
70+
A6[Manifest Parsing]
6871
end
6972
7073
%% Intermediate Stage
7174
subgraph Intermediate
7275
B1[Registry Interface Implementation]
73-
B2[Manifest Parsing]
74-
B3[Layer Downloading]
76+
B2[Layer Downloading]
7577
end
7678
7779
%% End Goal
@@ -85,8 +87,7 @@ graph TD
8587
%% Connections
8688
A --> B1
8789
B1 --> B2
88-
B2 --> B3
89-
B3 --> C1
90+
B2 --> C1
9091
C1 --> C2
9192
C2 --> C3
9293
C3 --> C4
@@ -98,10 +99,12 @@ graph TD
9899
- Simulated fetching logic is used to mimic image downloads.
99100
- Basic image listing functionality is implemented.
100101
- The `Pull` function exists as a placeholder without real registry interaction.
102+
- Added support for loading images from `.tar` files.
103+
- Added functionality to run locally loaded images.
104+
- Manifest parsing is now part of the current stage.
101105

102106
2. **Intermediate Stage**:
103107
- Introduce a `Registry` interface to abstract interactions with container registries.
104-
- Implement manifest parsing to identify required layers.
105108
- Add functionality for downloading image layers from registries.
106109

107110
3. **End Goal**:
@@ -110,6 +113,88 @@ graph TD
110113
- Extract layers to create a functional root filesystem for containers.
111114
- Achieve compatibility with real Docker workflows.
112115

116+
## Updates: Local Registry and Tar Functionality
117+
118+
### Local Registry Support
119+
We added support for pulling images from a local registry. The `run` command now parses the registry URL directly from the image name. For example:
120+
121+
```bash
122+
./basic-docker run localhost:5000/alpine /bin/sh -c "echo Hello from local registry"
123+
```
124+
125+
This allows users to specify images hosted on a local registry (e.g., `localhost:5000`) or any custom registry URL.
126+
127+
### Tar File Loading
128+
We also implemented functionality to load images from `.tar` files. This is useful for offline environments or pre-packaged images. The `load` command can be used as follows:
129+
130+
```bash
131+
./basic-docker load alpine.tar
132+
```
133+
134+
After loading, the image can be verified using the `images` command:
135+
136+
```bash
137+
./basic-docker images
138+
```
139+
140+
The output will include the image name, size, and whether its content is verified.
141+
142+
### Example Workflow
143+
1. **Load an Image from a Tar File**:
144+
```bash
145+
./basic-docker load busyboximage.tar
146+
```
147+
Output:
148+
```
149+
Loading image from 'busyboximage.tar'...
150+
Image 'busyboximage' loaded successfully.
151+
```
152+
153+
Create the registry
154+
155+
$ docker run -d -p 5000:5000 --name registry registry:2
156+
Unable to find image 'registry:2' locally
157+
2: Pulling from library/registry
158+
44cf07d57ee4: Pull complete
159+
bbbdd6c6894b: Pull complete
160+
8e82f80af0de: Pull complete
161+
3493bf46cdec: Pull complete
162+
6d464ea18732: Pull complete
163+
Digest: sha256:a3d8aaa63ed8681a604f1dea0aa03f100d5895b6a58ace528858a7b332415373
164+
Status: Downloaded newer image for registry:2
165+
d317a23d345324b5e4dc71c33f2548244b5ed0b877b44917db605ec856bfc431
166+
167+
$ ./basic-docker run localhost:5000/alpine /bin/sh -c "echo Hello from local registry"
168+
Environment detected: inContainer=true, hasNamespacePrivileges=true, hasCgroupAccess=false
169+
Fetching image 'localhost:5000/alpine' from registry...
170+
Image 'localhost:5000/alpine' fetched successfully.
171+
Starting container container-1745001780
172+
Warning: Namespace isolation is not permitted. Executing without isolation.
173+
Hello from local registry
174+
175+
2. **Run an Image from a Local Registry**:
176+
```bash
177+
./basic-docker run localhost:5000/alpine /bin/sh -c "echo Hello from local registry"
178+
```
179+
Output:
180+
```
181+
Fetching image 'localhost:5000/alpine' from registry...
182+
Image 'localhost:5000/alpine' fetched successfully.
183+
Starting container container-1234567890
184+
Hello from local registry
185+
```
186+
187+
3. **List Images**:
188+
```bash
189+
./basic-docker images
190+
```
191+
Output:
192+
```
193+
IMAGE NAME SIZE CONTENT VERIFIED
194+
busyboximage 4530692 Yes
195+
localhost:5000/alpine 1234567 Yes
196+
```
197+
113198
## Alternatives Considered
114199
1. **Continue with Simulated Fetching**:
115200
- Simpler to implement but limits functionality.
@@ -124,4 +209,5 @@ graph TD
124209

125210
## References
126211
- [Docker Image Specification](https://github.com/moby/moby/blob/master/image/spec/v1.2.md)
127-
- [OCI Image Format Specification](https://github.com/opencontainers/image-spec)
212+
- [OCI Image Format Specification](https://github.com/opencontainers/image-spec)
213+

image.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ type Registry interface {
4646
FetchLayer(repo, digest string) (io.ReadCloser, error)
4747
}
4848

49-
// DockerHubRegistry is a default implementation of the Registry interface for Docker Hub.
49+
// DockerHubRegistry is a default implementation of the Registry interface for Docker Hub or custom registries.
5050
type DockerHubRegistry struct {
5151
BaseURL string
5252
}
5353

54-
// NewDockerHubRegistry creates a new instance of DockerHubRegistry.
55-
func NewDockerHubRegistry() *DockerHubRegistry {
54+
// NewDockerHubRegistry creates a new instance of DockerHubRegistry with an optional custom registry URL.
55+
func NewDockerHubRegistry(customURL string) *DockerHubRegistry {
56+
if customURL == "" {
57+
customURL = "https://registry-1.docker.io/v2/"
58+
}
5659
return &DockerHubRegistry{
57-
BaseURL: "https://registry-1.docker.io/v2/",
60+
BaseURL: customURL,
5861
}
5962
}
6063

main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,17 @@ func run() {
337337
fmt.Printf("Using locally loaded image '%s'.\n", imageName)
338338
} else {
339339
fmt.Printf("Fetching image '%s' from registry...\n", imageName)
340-
registry := NewDockerHubRegistry()
341-
image, err := Pull(registry, imageName)
340+
// Extract registry URL and repository from image name
341+
parts := strings.SplitN(imageName, "/", 2)
342+
registryURL := "https://registry-1.docker.io/v2/" // Default to Docker Hub
343+
repo := imageName
344+
if len(parts) > 1 {
345+
registryURL = fmt.Sprintf("http://%s/v2/", parts[0])
346+
repo = parts[1]
347+
}
348+
349+
registry := NewDockerHubRegistry(registryURL)
350+
image, err := Pull(registry, repo)
342351
if err != nil {
343352
fmt.Printf("Error: Failed to fetch image '%s': %v\n", imageName, err)
344353
os.Exit(1)

0 commit comments

Comments
 (0)