Skip to content

Commit 9fc15e8

Browse files
Add optional embedding of container images into DinD:
This helps use cases where images already existing in the DinD cache is needed. Air gap envs, latency constrained/concerned envs, etc. Signed-off-by: Jacob Weinstock <[email protected]>
1 parent ff15b8a commit 9fc15e8

File tree

9 files changed

+121
-11
lines changed

9 files changed

+121
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ cache/
1212
*.swp
1313
.idea
1414
kernel/Dockerfile.autogen.*
15+
images/hook-embedded/images/*
16+
!images/hook-embedded/images/.keep

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,17 @@ The `gha-matrix` CLI command prepares a set of JSON outputs for GitHub Actions m
160160
- `DOCKER_ARCH` is used by the `linuxkit-containers` command to build the containers for the specified architecture.
161161
- `DO_PUSH`: `yes` or `no`, will push the built containers to the OCI registry; defaults to `no`.
162162

163+
### Embedding container images into the DinD (docker-in-docker), also known as [hook-docker](images/hook-docker/), container
164+
165+
For use cases where having container images already available in Docker is needed, the following steps can be taken to embed container images into hook-docker (DinD):
166+
167+
1. Create a file named `images.txt` in the [images/hook-embedded/](images/hook-embedded/) directory.
168+
1. Populate this `images.txt` file with the list of images to be embedded. See [images/hook-embedded/images.txt.example](images/hook-embedded/images.txt.example) for details on the required file format.
169+
1. Change directories to [images/hook-embedded/](images/hook-embedded/) and run the [pull-images.sh](images/hook-embedded/pull-images.sh) script. Read the comments at the top of the script for more details.
170+
1. Change directories to the root of the HookOS repository and run `sudo ./build.sh build ...` to build the HookOS kernel and ramdisk. FYI, `sudo` is needed as DIND changes file ownerships to root.
171+
163172
### Build system TO-DO list
164173

165-
- [ ] Update to Linuxkit 1.2.0 and new linuxkit pkgs; this might lead into the containerd vs dind;
166174
- [ ] `make debug` functionality (sshd enabled) was lost in the Makefile -> bash transition;
167175

168176
[formats]: https://github.com/linuxkit/linuxkit/blob/master/README.md#booting-and-testing

bash/hook-lk-containers.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function build_all_hook_linuxkit_containers() {
1313
build_hook_linuxkit_container hook-mdev HOOK_CONTAINER_MDEV_IMAGE
1414
build_hook_linuxkit_container hook-containerd HOOK_CONTAINER_CONTAINERD_IMAGE
1515
build_hook_linuxkit_container hook-runc HOOK_CONTAINER_RUNC_IMAGE
16+
build_hook_linuxkit_container hook-embedded HOOK_CONTAINER_EMBEDDED_IMAGE
1617
}
1718

1819
function build_hook_linuxkit_container() {

bash/linuxkit.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function linuxkit_build() {
7070
HOOK_CONTAINER_MDEV_IMAGE="${HOOK_CONTAINER_MDEV_IMAGE}" \
7171
HOOK_CONTAINER_CONTAINERD_IMAGE="${HOOK_CONTAINER_CONTAINERD_IMAGE}" \
7272
HOOK_CONTAINER_RUNC_IMAGE="${HOOK_CONTAINER_RUNC_IMAGE}" \
73-
envsubst '$HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_IP_IMAGE $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE' \
73+
HOOK_CONTAINER_EMBEDDED_IMAGE="${HOOK_CONTAINER_EMBEDDED_IMAGE}" \
74+
envsubst '$HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_IP_IMAGE $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE $HOOK_CONTAINER_EMBEDDED_IMAGE' \
7475
> "hook.${inventory_id}.yaml"
7576

7677
declare -g linuxkit_bin=""

images/hook-embedded/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM scratch
2+
ENTRYPOINT []
3+
WORKDIR /
4+
COPY ./images/ /
5+
CMD []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This is an example file. It explains the required format.
2+
# For the actual file, you must remove all the comments.
3+
# The format is source image, a single space, optional additional tag of the source image.
4+
#<source image> <optional additional tag of the source image>
5+
# for example:
6+
quay.io/tinkerbell/tink-worker:v0.10.0
7+
quay.io/tinkerbell/tink-worker:v0.10.0 tink-worker:v0.10.0
8+
quay.io/tinkerbell/actions/image2disk embedded/actions/image2disk
9+
quay.io/tinkerbell/actions/cexec 127.0.0.1/embedded/actions/cexec

images/hook-embedded/images/.keep

Whitespace-only changes.

images/hook-embedded/pull-images.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# This script is used to pull
4+
# This script is used to build an image that is embedded in HookOS.
5+
# The image contains the /var/lib/docker directory which has pulled images
6+
# from the images.txt file. When HookOS boots up, the DinD container will
7+
# have all the images in its cache.
8+
9+
# The purpose of doing this is so that EKS Anywhere doesn't have to set registry
10+
# and registry credentials in each Hardware object.
11+
12+
# In my testing the initramfs for HookOS with these embedded images was about 334MB.
13+
# The machine booting HookOS needed 6GB of RAM to boot up successfully.
14+
15+
set -euo pipefail
16+
17+
function main() {
18+
local dind_container="$1"
19+
local images_file="$2"
20+
# as this function maybe called multiple times, we need to ensure the container is removed
21+
trap "docker rm -f ${dind_container} &> /dev/null" RETURN
22+
# we're using set -e so the trap on RETURN will not be executed when a command fails
23+
trap "docker rm -f ${dind_container} &> /dev/null" EXIT
24+
# start DinD container
25+
# In order to avoid the src bind mount directory (./images/) ownership from changing to root
26+
# we don't bind mount to /var/lib/docker in the container because the DinD container is running as root and
27+
# will change the permissions of the bind mount directory (images/) to root.
28+
echo -e "Starting DinD container"
29+
echo -e "-----------------------"
30+
docker run -d --rm --privileged --name "${dind_container}" -v ${PWD}/images/:/var/lib/docker-embedded/ -d docker:dind
31+
32+
# wait until the docker daemon is ready
33+
until docker exec "${dind_container}" docker info &> /dev/null; do
34+
sleep 1
35+
done
36+
37+
# pull images from list
38+
# this expects a file named images.txt in the same directory as this script
39+
# the format of this file is line separated: <image> <optional tag>
40+
#
41+
# the || [ -n "$first_image" ] is to handle the last line of the file that doesn't have a newline.
42+
while IFS=" " read -r first_image image_tag || [ -n "$first_image" ] ; do
43+
echo -e "----------------------- $first_image -----------------------"
44+
docker exec "${dind_container}" docker pull $first_image
45+
if [[ $image_tag != "" ]]; then
46+
docker exec "${dind_container}" docker tag $first_image $image_tag
47+
fi
48+
done < "${images_file}"
49+
50+
# remove the contents of /var/lib/docker-embedded so that any previous images are removed. Without this it seems to cause boot issues.
51+
docker exec "${dind_container}" sh -c "rm -rf /var/lib/docker-embedded/*"
52+
# We need to copy /var/lib/docker to /var/lib/docker-embedded in order for HookOS to use the Docker images in its build.
53+
docker exec "${dind_container}" sh -c "cp -a /var/lib/docker/* /var/lib/docker-embedded/"
54+
}
55+
56+
arch="amd64"
57+
dind_container_name="hookos-dind-${arch}"
58+
images_file="images.txt"
59+
main "${dind_container_name}" "${images_file}" "${arch}"

linuxkit-templates/hook.template.yaml

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,36 @@
99
# - HOOK_CONTAINER_MDEV_IMAGE: ${HOOK_CONTAINER_MDEV_IMAGE}
1010
# - HOOK_CONTAINER_CONTAINERD_IMAGE: ${HOOK_CONTAINER_CONTAINERD_IMAGE}
1111
# - HOOK_CONTAINER_RUNC_IMAGE: ${HOOK_CONTAINER_RUNC_IMAGE}
12+
# - HOOK_CONTAINER_EMBEDDED_IMAGE: ${HOOK_CONTAINER_EMBEDDED_IMAGE}
1213
# - Other variables are not replaced: for example this is a literal dollarsign-SOMETHING: $SOMETHING and with braces: ${SOMETHING}
1314

1415
kernel:
1516
image: "${HOOK_KERNEL_IMAGE}"
16-
cmdline: "this_is_not_used=at_at_all_in_hook command_line_is_determined_by=ipxe"
17+
cmdline: "this_is_not_used=at_all_in_hook command_line_is_determined_by=ipxe"
1718

1819
init:
19-
# this sha is the first with cgroups v2 as the default
20-
- linuxkit/init:8a7b6cdb89197dc94eb6db69ef9dc90b750db598
20+
# this init container sha has support for volumes
21+
- linuxkit/init:872d2e1be745f1acb948762562cf31c367303a3b
2122
- "${HOOK_CONTAINER_RUNC_IMAGE}"
2223
- "${HOOK_CONTAINER_CONTAINERD_IMAGE}"
2324
- linuxkit/ca-certificates:v1.0.0
2425
- linuxkit/firmware:24402a25359c7bc290f7fc3cd23b6b5f0feb32a5 # "Some" firmware from Linuxkit pkg; see https://github.com/linuxkit/linuxkit/blob/master/pkg/firmware/Dockerfile
2526

27+
volumes:
28+
- name: embedded-images
29+
image: "${HOOK_CONTAINER_EMBEDDED_IMAGE}"
30+
2631
onboot:
32+
- name: embedded-images
33+
image: alpine
34+
binds:
35+
- /var/run/images:/var/run/images
36+
- embedded-images:/images
37+
command: [ "sh", "-xc", "mv /images/* /var/run/images/" ]
38+
runtime:
39+
mkdir:
40+
- /var/run/images
41+
2742
- name: rngd1
2843
image: linuxkit/rngd:v1.0.0
2944
command: [ "/sbin/rngd", "-1" ]
@@ -100,6 +115,8 @@ services:
100115
devices:
101116
- path: all
102117
type: b
118+
- path: all
119+
type: c
103120
- path: "/dev/console"
104121
type: c
105122
major: 5
@@ -187,6 +204,8 @@ services:
187204
devices:
188205
- path: all
189206
type: b
207+
- path: all
208+
type: c
190209

191210
- name: hook-bootkit
192211
image: "${HOOK_CONTAINER_BOOTKIT_IMAGE}"
@@ -219,8 +238,14 @@ services:
219238
mkdir:
220239
- /var/lib/dhcpcd
221240

222-
#dbg - name: sshd
223-
#dbg image: linuxkit/sshd:v1.0.0
241+
#SSH_SERVER - name: sshd
242+
#SSH_SERVER image: linuxkit/sshd:v1.0.0
243+
#SSH_SERVER binds.add:
244+
#SSH_SERVER - /etc/profile.d/local.sh:/etc/profile.d/local.sh
245+
#SSH_SERVER - /root/.ssh/authorized_keys:/root/.ssh/authorized_keys
246+
#SSH_SERVER - /usr/bin/nerdctl:/usr/bin/nerdctl
247+
#SSH_SERVER - /:/host_root
248+
224249

225250
files:
226251
- path: etc/profile.d/local.sh
@@ -299,10 +324,10 @@ files:
299324
ttyUSB1
300325
ttyUSB2
301326
302-
#dbg - path: root/.ssh/authorized_keys
303-
#dbg source: ~/.ssh/id_rsa.pub
304-
#dbg mode: "0600"
305-
#dbg optional: true
327+
#SSH_SERVER - path: root/.ssh/authorized_keys
328+
#SSH_SERVER source: ~/.ssh/id_rsa.pub
329+
#SSH_SERVER mode: "0600"
330+
#SSH_SERVER optional: true
306331

307332
trust:
308333
org:

0 commit comments

Comments
 (0)