Skip to content

Generate container recipes with environments#14202

Merged
tgamblin merged 12 commits intospack:developfrom
alalazo:features/container_command
Jan 31, 2020
Merged

Generate container recipes with environments#14202
tgamblin merged 12 commits intospack:developfrom
alalazo:features/container_command

Conversation

@alalazo
Copy link
Copy Markdown
Member

@alalazo alalazo commented Dec 17, 2019

depends on #13534
closes #7204

This PR adds a new command to Spack:

$ spack containerize -h
usage: spack containerize [-h] [--config CONFIG]

creates recipes to build images for different container runtimes

optional arguments:
  -h, --help       show this help message and exit
  --config CONFIG  configuration for the container recipe that will be generated

which takes an environment with an additional container section:

spack:
  specs:
  - gromacs build_type=Release 
  - mpich
  - fftw precision=float
  packages:
    all:
      target: [broadwell]

  container:
    # Select the format of the recipe e.g. docker,
    # singularity or anything else that is currently supported
    format: docker
    
    # Select from a valid list of images
    base:
      image: "ubuntu:18.04"
      spack: prerelease

    # Additional system packages that are needed at runtime
    os_packages:
    - libgomp1

and turns it into a Dockerfile or a Singularity definition file, for instance:

# Build stage with Spack pre-installed and ready to be used
FROM spack/ubuntu-bionic:prerelease as builder

# What we want to install and how we want to install it
# is specified in a manifest file (spack.yaml)
RUN mkdir /opt/spack-environment \
&&  (echo "spack:" \
&&   echo "  specs:" \
&&   echo "  - gromacs build_type=Release" \
&&   echo "  - mpich" \
&&   echo "  - fftw precision=float" \
&&   echo "  packages:" \
&&   echo "    all:" \
&&   echo "      target:" \
&&   echo "      - broadwell" \
&&   echo "  config:" \
&&   echo "    install_tree: /opt/software" \
&&   echo "  concretization: together" \
&&   echo "  view: /opt/view") > /opt/spack-environment/spack.yaml

# Install the software, remove unecessary deps and strip executables
RUN cd /opt/spack-environment && spack install && spack autoremove -y
RUN find -L /opt/view/* -type f -exec readlink -f '{}' \; | \
    xargs file -i | \
    grep 'charset=binary' | \
    grep 'x-executable\|x-archive\|x-sharedlib' | \
    awk -F: '{print $1}' | xargs strip -s


# Modifications to the environment that are necessary to run
RUN cd /opt/spack-environment && \
    spack env activate --sh -d . >> /etc/profile.d/z10_spack_environment.sh

# Bare OS image to run the installed executables
FROM ubuntu:18.04

COPY --from=builder /opt/spack-environment /opt/spack-environment
COPY --from=builder /opt/software /opt/software
COPY --from=builder /opt/view /opt/view
COPY --from=builder /etc/profile.d/z10_spack_environment.sh /etc/profile.d/z10_spack_environment.sh

RUN apt-get -yqq update && apt-get -yqq upgrade                                   \
 && apt-get -yqq install libgomp1 \
 && rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["/bin/bash", "--rcfile", "/etc/profile", "-l"]

@alalazo alalazo added feature A feature is missing in Spack environments containers labels Dec 17, 2019
@alalazo alalazo requested a review from tgamblin December 17, 2019 23:07
@sknigh

This comment has been minimized.

@victorusu
Copy link
Copy Markdown
Contributor

@alalazo, just to clarify... The PR will stop at the phase of creating the Dockerfile or a Singularity definition file? Or will it continue to build an image?

@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Dec 20, 2019

@victorusu It will just create the recipe. The rationale is that a recipe can be consumed in many ways and it wouldn't make sense for Spack to force users to employ only the ones we'll wrap (e.g. you can send it to a remote builder, build it with different tools, etc.). I also think it wouldn't be that great convenience to users to have the build wrapped, while there will be some maintenance cost associated with it.

@alalazo alalazo force-pushed the features/container_command branch 3 times, most recently from 934cf5c to 5a65ada Compare December 27, 2019 15:56
@alalazo alalazo force-pushed the features/container_command branch from 5a65ada to c4b306d Compare January 2, 2020 10:09
@alalazo alalazo force-pushed the features/container_command branch 3 times, most recently from a1b8654 to b7f1fc9 Compare January 8, 2020 15:00
Copy link
Copy Markdown
Member Author

@alalazo alalazo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@becker33 This should be ready for a first review, I left a couple of comments below and feel free to ask anything if things seem unclear. Thanks!

@alalazo alalazo marked this pull request as ready for review January 9, 2020 08:48
@alalazo alalazo requested a review from becker33 January 9, 2020 08:48
@sknigh
Copy link
Copy Markdown

sknigh commented Jan 9, 2020

If I wanted my container ignore certs (e.g. spack install -k) how would I do that? Does the existing spack environment schema already support it?

@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Jan 13, 2020

If I wanted my container ignore certs (e.g. spack install -k) how would I do that? Does the existing spack environment schema already support it?

I think:

config:
  verify_ssl: false

in the Spack environment should do it.

Copy link
Copy Markdown
Member

@becker33 becker33 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple questions about the tests, and a couple comments about code clarity.

@alalazo alalazo force-pushed the features/container_command branch 2 times, most recently from d5dbef5 to 12fd35f Compare January 16, 2020 10:37
@sknigh
Copy link
Copy Markdown

sknigh commented Jan 23, 2020

@alalazo This implementation can also save space by deleting .spack directories and static libraries before squashing.

Something like this...

# Remove static libraries
find /opt/software/* -type f -name \*.a | xargs rm -f
find /opt/view/* -type l -name \*.a | xargs rm -f

# Remove .spack directories
rm -rf /opt/software/*/.spack

Copy link
Copy Markdown
Member

@tgamblin tgamblin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some minor change requests, but otherwise this is nearly ready to go!

The command 'spack containerize' has been added to generate
recipes to build container images from a spack.yaml file. The
image format supported so far are 'docker' and 'singularity'.

Recipe generation is based on Jinja2 templates, most of the logic
is under the 'spack.container' package.
@alalazo alalazo force-pushed the features/container_command branch from 2c614a2 to 2e21587 Compare January 30, 2020 10:20
@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Jan 30, 2020

@tgamblin I think the requests have been addressed. Let me know if there's anything else to be done.

@tgamblin tgamblin merged commit 9635ff3 into spack:develop Jan 31, 2020
@alalazo alalazo deleted the features/container_command branch January 31, 2020 07:28
@samcom12
Copy link
Copy Markdown

Can you please share a sample to build for singularity recipe with spack command?

@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Feb 13, 2020

@samcom12 If you use:

format: singularity

Spack should output a valid Singularity definition file.

@samcom12
Copy link
Copy Markdown

Thank you Alalazo...
Can you please share a command to create a singularity def file using spack command.
Sorry for troubling you, but I didn't get a example for same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

containers environments feature A feature is missing in Spack

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants