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:

    1. Dump the Singularity image filesystem as a squashfs file.
    2. Extract the squashfs file into a directory.
    3. Create a Dockerfile that inherits from scratch, copies over the Singularity image’s filesystem, and sets environment variables and other things.
    4. Build the Docker image.

    Here it is in bash, demonstrated on alpine:latest:

    1. singularity pull docker://alpine:latest
    2. # Find out which SIF ID to use (look for Squashfs)
    3. singularity sif list alpine_latest.sif
    4. # Get the environment variables defined in the Singularity image.
    5. singularity sif dump 2 alpine_latest.sif
    6. singularity sif dump 3 alpine_latest.sif > data.squash
    7. unsquashfs -dest data data.squash
    8. # See the Dockerfile definition below
    9. docker build --tag alpine:latest .

    Contents of Dockerfile:

    1. FROM scratch
    2. COPY data /
    3. ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    4. CMD ["/bin/ash"]

    For more information on Singularity and Docker, I recommend looking at Singularity’s documentation on the topic.

    1. context = {
    2. 'model_copy_dest': self.fopr.path_join(model_location, 'model/'),
    3. 'tarined_model_scenario': SCENARIO_CHOICES[model.type],
    4. 'tarined_model_location': model_location,
    5. 'tarined_model_name': json.loads(
    6. model.parameters).get('model_name'),
    7. }
    8. from django.conf import settings
    9. from django.template.loader import render_to_string
    10. if settings.LICO.ARCH == 'kube':
    11. context['base_image'] = self.container_opr.search_letrain_image(
    12. image_name='letrain-gpu')
    13. elif settings.LICO.ARCH == 'singularity':
    14. context['letrain_data_path'] = settings.LETRAIN_IMAGE
    15. docker_file_path = self.fopr.path_join(model_location, 'Dockerfile')
    16. with self.fopr.open_file(docker_file_path, 'w') as f:
    17. new_content = render_to_string(DOCKERFILE, context=context)
    18. f.file_handle.write(new_content)

    dockerfile

    1. {% if base_image %}
    2. FROM {{ base_image }}
    3. {% else %}
    4. FROM scratch
    5. COPY {{ letrain_data_path }} /
    6. {% endif %}
    7. COPY model {{ model_copy_dest }}
    8. ENTRYPOINT ["python", "/yyy/xxx/letrain-run-script/service.py",\
    9. "--scenario={{ tarined_model_scenario }}",\
    10. "--deploy_path={{ tarined_model_location }}", "--port=80",\
    11. "--model_name={{ tarined_model_name }}"]
    12. ENV PATH=$PATH":/usr/local/cuda/bin"
    13. ENV LETRAIN_PATH="/opt/letrain"
    14. ENV export LETRAIN_PATH

    dockerfile 可以写 多行 ,用\ 分隔。 使用 "" 双引号, 因为 ' 单引号 报错。