Featured image of post Recent docker notes

Recent docker notes

記錄最近跑 docker / singularity 的指令

引入本機資料

在container內使用外部檔案,設定 --volume-v 並指定在container可以access的路徑名稱,例如:

1
2
3
## my input file: /path/to/input/input.txt

docker run -v /path/to/input:/data my_container:latest -input /data/input.txt

singularity 也有相似指令 --bind

1
singularity exec --bind /path/to/input:/data,/path/to/other:/index my_sing.sif -input /data/input.txt -output /index 

修改寫入權限

有時遇到 docker 輸出檔案的權限為 root ,若以後要修改刪除等操作會不方便,使用 --user $(id -u):$(id -g) 來將輸出的owner, group 權限給下指令的使用者,例如:

1
docker run -it -rm -v /path/to/input:/data --user $(id -u):$(id -g) my_container:latest -input /data/input.txt -output /data/output.txt

若owner為使用者,之後還可通過linux chgrp 修改 group 權限

其他指令

加上 --rm 讓container執行完一併刪除 加上 --it 進入互動模式,但有可能出現 the input device is not a TTY 錯誤

用 Dockerfile 建立 docker 並使用

前陣子使用某docker遇到bug無法執行成功,因此稍微修改他的dockerfile -> push至dockerhub -> pull 回來使用 註1: 以下有匿名處理 docker name 請參考code就好XD 註2: 需要註冊 https://hub.docker.com/ 才能push 註3: my references

首先登入dockerhub 建立新專案

回到linux建立docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## cd to dockerfile directory and build docker
docker build -t local/origin_docker_fixed:latest .

## need user login 
docker login

## rename docker tag (origin TAG is <none>)
## the image name may be the same as the repository we created
docker tag 53a8d0dbxxxx pxchen/origin_docker_fixed

## check 
docker image ls

## push to the existing repository
docker push pxchen112/msisensor-pro-fixed

回到 dockerhub,點進去看push的image 以及Dockerfile layer

常用的 image base

基礎映像 標籤 特點
Alpine Linux alpine:latest 輕量、安全、適合容器化應用,小映像大小
Ubuntu ubuntu:latest 廣泛使用的 Linux 發行版,功能豐富
CentOS centos:latest 穩定、適用於企業級應用,基於 RHEL
Debian debian:latest 穩定,有廣泛的套件庫
Scratch scratch 最小化基礎映像,不包含操作系統
Golang golang:latest 包含 Go 程序語言和開發工具的基礎映像
Node.js node:latest 包含 Node.js 環境的基礎映像

將建立好的 docker 存成 tar 檔並加載到其他環境

1
2
3
docker save -o yourdocker.tar local/origin_docker_fixed:latest

docker load yourdocker.tar
comments powered by Disqus