Search code examples
dockerapptainer

Equivalent of "context" in docker build for apptainer build?


I am adapting some Python code that uses Docker to use Apptainer instead, as I only have access to the latter on the HPC system where I am working. The code generates commands with the following form:

docker build --build-arg PYTHON_VERSION={version} {extra} -t {IMAGE_NAME} -f {dockerfile} {CONTAINER_MAIN.parent}

where {version}, {extra}, {IMAGE_NAME}, {dockerfile}, and {CONTAINER_MAIN.parent} are set elsewhere in the code. {extra} is either empty, or a string that begins with "--build-arg".

I want to make a similar command with apptainer.

So far, I've figured out the following:

  • Apptainer also has a --build-arg option that seems to do the same thing, so that part looks like it can stay the same.
  • docker build's -t option sets the name for the resulting image, which looks (?) like it's the first positional argument for `apptainer build'
  • docker build's -f option specifies the Dockerfile. This should map to the second positional argument in apptainer build, though I (may?) have to convert the Dockerfile to an apptainer definiton file by the recipe here.
  • docker build has one positional argument, as explained here: a path that gives the build's context, saying where the build process can look for files

I can't figure out what the equivalent of that last argument is in apptainer. How do I give apptainer build a context where it can find files?


Solution

  • There is a --bind flag which you could use. As in the docs, you need to make sure the mount point exists in the container, for example:

    Bootstrap: docker
    From: docker.io/library/debian:stable
    
    %setup
        mkdir -p $APPTAINER_ROOTFS/mnt/bind
    
    %post
        # use files from host system here
    

    Then run apptainer build --bind CONTAINER_MAIN.parent:/mnt/bind IMAGE_NAME DEFINITION_FILE