Add ability to import images from another Docker registry

.....though we should refrain from using this unless absolutely 
necessary
This commit is contained in:
Starbeamrainbowlabs 2021-09-27 02:27:57 +01:00
parent 7b891ff1f4
commit bf162ac725
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 39 additions and 3 deletions

View file

@ -29,10 +29,10 @@ Multiple reasons:
4. Compatibility: 4. Compatibility:
- I use [Hashicorp Nomad](https://www.nomadproject.io/), so some of these Dockerfiles are written explicitly with Hashicorp Nomad in mind - e.g. the [`NOMAD_PORT_*` environment variables](https://www.nomadproject.io/docs/job-specification/network#port-parameters). - I use [Hashicorp Nomad](https://www.nomadproject.io/), so some of these Dockerfiles are written explicitly with Hashicorp Nomad in mind - e.g. the [`NOMAD_PORT_*` environment variables](https://www.nomadproject.io/docs/job-specification/network#port-parameters).
- My Hashicorp Nomad cluster is comprised chiefly of Raspberry Pis (currently running armv7l, but an upgrade to arm64 is planned eventually), and many Docker containers on the Docker Hub are built by default for amd64 - My Hashicorp Nomad cluster is comprised chiefly of Raspberry Pis (currently running armv7l, but an upgrade to arm64 is planned eventually), and many Docker containers on the Docker Hub are built by default for amd64
5. Maintainability: I want to ensure I keep my Docker images up-to-date, so I rebuild them myself regularly via my [Continuous Integration server](https://laminar.ohwg.net/) 5. Maintainability: I want to ensure I keep my Docker images up-to-date, so I rebuild them myself regularly via my [Continuous Integration server](https://laminar.ohwg.net/) ([see also](https://starbeamrainbowlabs.com/blog/article.php?article=posts/392-own-your-code-series-list.html), and [also this](https://starbeamrainbowlabs.com/blog/article.php?article=posts/451-cluster-11-lock-and-key.html))
### Why do I need to run a private Docker registry for `imagebuilder.sh` to work? ### Why do I need to run a private Docker registry for `imagebuilder.sh` to work?
`imagebuild.sh` is designed to automatically build the specified Docker image and then push it to a private Docker registry because then the hosts in my Hashicorp Nomad `imagebuilder.sh` is designed to automatically build the specified Docker image and then push it to a private Docker registry because then the hosts in my Hashicorp Nomad
### These Dockerfiles don't work for me! ### These Dockerfiles don't work for me!
These Dockerfiles are specific to my environment. They depend on a patched version of `minideb` as a base image, which this package is also responsible for building. The key changes to `minideb` include: These Dockerfiles are specific to my environment. They depend on a patched version of `minideb` as a base image, which this package is also responsible for building. The key changes to `minideb` include:
@ -96,8 +96,9 @@ UID | GID | Container | Notes
At present, 3 image types are present: At present, 3 image types are present:
- **`base`:** A base image - a script is called with an auto-generated output directory as the 1st and only argument. When the script exits the directory is checked for a `.tar.gz` file - **`base`:** A base image - a script is called with an auto-generated output directory as the 1st and only argument. When the script exits the directory is checked for a `.tar.gz` file
- **`base-nopush`:** A variant of the above that doesn't automatically much to the docker registry on completion. - **`base-nopush`:** A variant of the above that doesn't automatically push to the docker registry on completion.
- **`docker`:** A `Dockerfile` is is built with `docker build` before being pushed to the docker registry. - **`docker`:** A `Dockerfile` is is built with `docker build` before being pushed to the docker registry.
- **`import`:** Imports a Docker image from another Docker registry.
### Creating a new image ### Creating a new image
Each image should have it's own subdirectory inside the `image` directory. The following files should be present for a `docker` image type: Each image should have it's own subdirectory inside the `image` directory. The following files should be present for a `docker` image type:
@ -112,9 +113,13 @@ For `base` and `base-nopush` image types, the following files should be present:
- `IMAGE_NAME.sh`: A `.sh` file named after the name of the parent directory. For example, the `minideb` image contains the script `minideb.sh` - `IMAGE_NAME.sh`: A `.sh` file named after the name of the parent directory. For example, the `minideb` image contains the script `minideb.sh`
- `type.txt`: Should contain either the word `base` or `base-nopush` - `type.txt`: Should contain either the word `base` or `base-nopush`
For `import` image types, the file `imagetype.txt` should be present and contain the (fully qualified) name of the image to import.
Optionally, any image type can contain the following files: Optionally, any image type can contain the following files:
- `dependents.txt`: The names of images that depend on this image - 1 image name per file. This is read by my continuous integration system to queue rebuilds of dependent Docker containers once the current image has finished building & pushing automatically. - `dependents.txt`: The names of images that depend on this image - 1 image name per file. This is read by my continuous integration system to queue rebuilds of dependent Docker containers once the current image has finished building & pushing automatically.
## Licence ## Licence
The contents of this repository is licenced under the _Mozilla Public License 2.0_ (MPL-2.0). This license can be found in the _LICENSE_ file in this repository. The contents of this repository is licenced under the _Mozilla Public License 2.0_ (MPL-2.0). This license can be found in the _LICENSE_ file in this repository.

View file

@ -211,6 +211,37 @@ case "${subcommand}" in
task_end "$?"; task_end "$?";
;; ;;
import)
task_begin "Finding remote image name";
if [[ ! -r "${imagedir}/imagename.txt" ]]; then
echo "Error: 'import' image type specified, but no imagename.txt file was found containing the (fully qualified) image name to pull from.";
exit 8;
fi
imagename_remote="$(tr -d "[:blank:]" <"${imagedir}/imagename.txt")"
if [[ -z "${imagename_remote}" ]]; then
echo "Error: An empty image name to pull isn't valid.";
exit 7;
fi
echo -e "[${HOSTNAME}:imagebuilder] Remote image name is ${FGRN}${HC}${imagename_remote}${RS}";
task_end "$?";
task_begin "Downloading image";
execute docker pull "${imagename_remote}";
task_end "$?" "Error: Failed to download image";
task_begin "Retagging image";
execute docker tag "${imagename_remote}" "${docker_tag}";
task_end "$?";
task_begin "Pushing image";
execute docker push "${docker_tag}";
task_end "$?";
;;
# ██ ██ ███ ██ ██ ██ ███ ██ ██████ ██ ██ ███ ██ # ██ ██ ███ ██ ██ ██ ███ ██ ██████ ██ ██ ███ ██
# ██ ██ ████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██ # ██ ██ ████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██