Docker and Singularity


Docker and Singularity

Docker 安装和基本配置

安装步骤

$ curl -fsSL test.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

启动docker

$ sudo systemctl enable docker
$ sudo systemctl start docker

建立 docker 用户组,免sudo运行

默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
建立 docker 组:

$ sudo groupadd docker

将当前用户加入 docker 组:

$ sudo usermod -aG docker $USER

退出当前终端并重新登录,进行如下测试。

测试 Docker 是否安装正确

$ docker run --rm hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull completeDigest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps: 
1. The Docker client contacted the Docker daemon. 
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.    (amd64) 
3. The Docker daemon created a new container from that image which runs the    executable that produces the output you are currently reading. 
4. The Docker daemon streamed that output to the Docker client, which sent it    to your terminal.

To try something more ambitious, you can run an Ubuntu container with: 
   $ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID: 
 https://hub.docker.com/
   
For more examples and ideas, visit: 
  https://docs.docker.com/get-started/

若能正常输出以上信息,则说明安装成功。

Docker的基本使用

启动docker container

$ docker run -it --rm -v /home/jwang/Desktop/:/home/jovyan/work/test gitlab-registry.in2p3.fr/epta/docker_singularity:master_std /bin/bash

# -v   参数: 挂载/home/jwang/Desktop/至/home/jovyan/work/test的目录,注意使用绝对路径
# --rm 参数: 在容器终止运行后自动删除容器文件
# -it  参数:容器的 Shell 映射到当前的 Shell,然后你在本机窗口输入的命令,就会传入容器。
# /bin/bash:容器启动以后,内部第一个执行的命令。这里是启动 Bash,保证用户可以使用 Shell

镜像image有关命令

$ docker pull ubuntu:22.04 # 获取一个新的镜像ubuntu:22.04
$ docker image ls #列出本机的所有 image 文件
$ docker rmi ubuntu:22.04  #删除本机 image 文件ubuntu:22.04
$ docker search httpd # 查找镜像httpd

容器container有关命令

$ docker ps -a  # 查看所有容器列表
$ docker run -it ubuntu /bin/bash # 启动容器,并以命令行模式进入该容器
$ docker ps -a # 查出容器的 ID
$ docker rm [containerID] # 删除指定的容器

docker start

前面的docker run命令是新建容器,每运行一次,就会新建一个容器。同样的命令运行两次,就会生成两个一模一样的容器文件。如果希望重复使用容器,就要使用docker start命令,它用来启动已经生成、已经停止运行的容器文件。

$ docker start [containerID]

docker stop

前面的docker kill命令终止容器运行,相当于向容器里面的主进程发出 SIGKILL 信号。而docker stop命令也是用来终止容器运行,相当于向容器里面的主进程发出 SIGTERM 信号,然后过一段时间再发出 SIGKILL 信号。

$ docker stop [containerID]

这两个信号的差别是,应用程序收到 SIGTERM 信号以后,可以自行进行收尾清理工作,但也可以不理会这个信号。如果收到 SIGKILL 信号,就会强行立即终止,那些正在进行中的操作会全部丢失。

docker logs

docker container logs命令用来查看 docker 容器的输出,即容器里面 Shell 的标准输出。如果docker run命令运行容器的时候,没有使用-it参数,就要用这个命令查看输出。

$ docker logs [containerID]

docker container exec

docker container exec命令用于进入一个正在运行的 docker 容器。如果docker run命令运行容器的时候,没有使用-it参数,就要用这个命令进入容器。一旦进入了容器,就可以在容器的 Shell 执行命令了。

$ docker container exec -it [containerID] /bin/bash

Singularity 安装和基本配置

Singularity 安装

# 下载并安装Go-lang
$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt update
$ sudo apt install golang-go


#下载并解压Singularity
$ export VERSION=3.7.0 && # adjust this as necessary \
    wget https://github.com/hpcng/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz && \
    tar -xzf singularity-${VERSION}.tar.gz && \
    cd singularity


#编译Singularity
$ ./mconfig && \
    make -C builddir && \
    sudo make -C builddir install

检查是否安装成功

$ singularity
Usage:
  singularity [global options...] <command>

Available Commands:
  build       Build a Singularity image
  cache       Manage the local cache
  capability  Manage Linux capabilities for users and groups
  config      Manage various singularity configuration (root user only)
  delete      Deletes requested image from the library
  exec        Run a command within a container
  inspect     Show metadata for an image
  instance    Manage containers running as services
  key         Manage OpenPGP keys
  oci         Manage OCI containers
  overlay     Manage an EXT3 writable overlay image
  plugin      Manage Singularity plugins
  pull        Pull an image from a URI
  push        Upload image to the provided URI
  remote      Manage singularity remote endpoints, keyservers and OCI/Docker registry credentials
  run         Run the user-defined default command within a container
  run-help    Show the user-defined help for an image
  search      Search a Container Library for images
  shell       Run a shell within a container
  sif         siftool is a program for Singularity Image Format (SIF) file manipulation
  sign        Attach digital signature(s) to an image
  test        Run the user-defined tests within a container
  verify      Verify cryptographic signatures attached to an image
  version     Show the version for Singularity

Run 'singularity --help' for more detailed usage information.

Sigularity镜像image

Singularity 镜像文件(Singularity Image File, sif)是一种内容只读的文件格式,其文件内容不能被修改。

Singularity 可以从 Singularity Hub (以 shub:// 开头)或者 Docker Hub (以 docker:// 开头)来获取镜像

$ sudo singularity pull docker://ubuntu # 获取预编译镜像,完成后会在本地目录生成文件 <image-name>_<tag>.sif
$ singularity -d build lolcow.simg shub://GodloveD/lolcow # build 镜像,会在本地目录 build 出镜像


$ sudo singularity instance list # 查看运行的容器实例
$ sudo singularity shell -B /opt:/opt ubuntu.simg # 绑定目录 -B,类似于 docker 的 -v 参数

$ singularity exec --bind /mnt/nfs:/mnt/nfs ubuntu.latest.sif /bin/bash script.sh 使用--bind选项映射主机目录到容器内

Author: Jun Wang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Jun Wang !
 Previous
Astronomical coordinate systems Astronomical coordinate systems
在天文学中,有几种常用的坐标系,用于描述天体在天空中的位置。每种坐标系都有其独特的用途和适用范围。在这里,我总结了及总常见的天文学坐标系。
2023-03-21
Next 
LOFAR introduction LOFAR introduction
The Low-Frequency Array (LOFAR) is a cutting-edge radio telescope network designed to observe the universe at low frequencies, ranging from 10 MHz to 240 MHz.
2022-07-05