Docker build images

Resources

Building images without compose

In kubernetes iamges are used directly w/o docker compose. Hence, building is to rely on Dockerfile and build context exclusively.

This affects two aspects:

  • environmental variables are set via --build-arg switches for each variable. If not values are supplied, value from shell environment that runs docker-build is taken
  • Dockerfile must be set explicitly on the command line via -f flag.
  • The last argument is the context where additional files required to build the image reside

In total:

export ARG1=SOME_VALUE
export ARG2=OTHER_VALUE
#take . as the context
docker build --build-arg ARG1 --build-arg ARG2 -f /path/to/dockerfile .

Tag image with an appropriate name:

studen@labkey:~/images/labkeyDB$ docker image ls
#get IMAGE ID for next step
studen@labkey:~/images/labkeyDB$ docker image tag <IMAGE ID> <NAME>
#now image can be referred to by <NAME> which can include a tag (latest used if none given)

Save image:

#use <NAME> from previous step
studen@labkey:~/images/labkeyDB$ docker save --output <NAME>.tar <NAME>
#remove <NAME> from docker repository (not needed any longer)
studen@labkey:~/images/labkeyDB$ docker rmi <NAME>

Import into containerd repository. Adding -n=k8s.io makes image available to kubernetes, and ctr image ls will not show such images, but crictl images does.

studen@labkey:~/images/labkeyDB$ sudo ctr -n=k8s.io image import <NAME>.tar
#check if image made it
studen@labkey:~/images/labkeyDB$ sudo crictl images
IMAGE                   TAG     IMAGE ID        SIZE
[..]
docker.io/library/<NAME>  latest  XXXXXXX         410MB
[..]
#remove .tar 
studen@labkey:~/images/labkeyDB$ rm <NAME>.tar

Discussion