https://stackoverflow.com/questions/60451712/how-to-build-docker-image-from-singularity-image
Generally speaking, it’s done in the other direction: building a singularity image based on Docker. If you plan on storing it as a docker image rather than singularity, your best bet would be to build it as Docker and convert to singularity as needed. I know of several labs using this method: build via Dockerfile, push to Docker hub for storage, build singularity image directly from docker hub image.
As @tsnowlan said in their answer, typically the workflow is from Docker to Singularity. But there is a way to make a Docker image from an existing Singularity image. This would not make use of Docker’s layer caching features.
The general idea is to:
- Dump the Singularity image filesystem as a squashfs file.
- Extract the squashfs file into a directory.
- Create a Dockerfile that inherits from scratch, copies over the Singularity image’s filesystem, and sets environment variables and other things.
- Build the Docker image.
Here it is in bash, demonstrated on alpine:latest
:
singularity pull docker://alpine:latest
# Find out which SIF ID to use (look for Squashfs)
singularity sif list alpine_latest.sif
# Get the environment variables defined in the Singularity image.
singularity sif dump 2 alpine_latest.sif
singularity sif dump 3 alpine_latest.sif > data.squash
unsquashfs -dest data data.squash
# See the Dockerfile definition below
docker build --tag alpine:latest .
Contents of Dockerfile:
FROM scratch
COPY data /
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CMD ["/bin/ash"]
For more information on Singularity and Docker, I recommend looking at Singularity’s documentation on the topic.
context = {
'model_copy_dest': self.fopr.path_join(model_location, 'model/'),
'tarined_model_scenario': SCENARIO_CHOICES[model.type],
'tarined_model_location': model_location,
'tarined_model_name': json.loads(
model.parameters).get('model_name'),
}
from django.conf import settings
from django.template.loader import render_to_string
if settings.LICO.ARCH == 'kube':
context['base_image'] = self.container_opr.search_letrain_image(
image_name='letrain-gpu')
elif settings.LICO.ARCH == 'singularity':
context['letrain_data_path'] = settings.LETRAIN_IMAGE
docker_file_path = self.fopr.path_join(model_location, 'Dockerfile')
with self.fopr.open_file(docker_file_path, 'w') as f:
new_content = render_to_string(DOCKERFILE, context=context)
f.file_handle.write(new_content)
dockerfile
{% if base_image %}
FROM {{ base_image }}
{% else %}
FROM scratch
COPY {{ letrain_data_path }} /
{% endif %}
COPY model {{ model_copy_dest }}
ENTRYPOINT ["python", "/yyy/xxx/letrain-run-script/service.py",\
"--scenario={{ tarined_model_scenario }}",\
"--deploy_path={{ tarined_model_location }}", "--port=80",\
"--model_name={{ tarined_model_name }}"]
ENV PATH=$PATH":/usr/local/cuda/bin"
ENV LETRAIN_PATH="/opt/letrain"
ENV export LETRAIN_PATH
dockerfile 可以写 多行 ,用\
分隔。 使用 ""
双引号, 因为 '
单引号 报错。