docker 安裝
這裡主要介紹
linux
、
Windows 10
和
macOS
上的安裝。
Linux
$ sudo curl -sS https://get.docker.com/ | sh
測試
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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 bash
Share 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/
*如果機器有支援深度學習的Gpu,可以繼續執行如下指令以支援容器對gpu的調用[目前僅支援linux]:
# Add the package repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
點選下方課程連結即可直達Linux下安裝Docker視訊教程。
https://tianchi.aliyun.com/course/351/4127windows
手動下載下傳安裝
點選以下連結下載下傳
Stable (opens new window)或
Edge (opens new window)版本的 Docker Desktop for Windows。
下載下傳好之後輕按兩下
Docker Desktop Installer.exe
開始安裝。
使用
winget (opens new window)安裝
# stable
$ winget install Docker.DockerDesktop
# edge
$ winget install Docker.DockerDesktopEdge
運作
在 Windows 搜尋欄輸入 Docker 點選 Docker Desktop 開始運作。
Docker 啟動之後會在 Windows 工作列出現鲸魚圖示。
等待片刻,點選 Got it 開始使用 Docker。
macOS
如果你已經安裝了 Homebrew ,使用brew 安裝非常友善
Homebrew (opens new window)的
Cask (opens new window)已經支援 Docker Desktop for Mac,是以可以很友善的使用 Homebrew Cask 來進行安裝:
$ brew cask install docker
沒有安裝brew也可以手動下載下傳安裝
點選下載下傳
版本的 Docker Desktop for Mac。
如同 macOS 其它軟體一樣,安裝也非常簡單,輕按兩下下載下傳的
.dmg
檔案,然後将那隻叫
Moby (opens new window)的鲸魚圖示拖拽到
Application
檔案夾即可(其間需要輸入使用者密碼)。
從應用中找到 Docker 并點選運作。
運作之後,會在右上角菜單欄看到多了一個鲸魚圖示,這個圖示表明了 Docker 的運作狀态。
第一次點選圖示,可能會看到這個安裝成功的界面,點選 "Got it!" 可以關閉這個視窗。
以後每次點選鲸魚圖示會彈出操作菜單。
啟動終端後,通過指令可以檢查安裝後的 Docker 版本。
$ docker --version
Docker version 19.03.8, build afacb8b
$ docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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 bash
Share 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/
點選下方課程連結即可直達macOS下安裝Docker視訊教程。
https://tianchi.aliyun.com/course/351/4128docker 基礎指令學習
拉取鏡像
docker pull [選項] [docker鏡像位址:标簽]
如:
docker pull hello-world:latest
運作鏡像
$ docker run 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 bash
Share 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 run -it --rm ubuntu:18.04 bash
root@e7009c6ce357:/# uname -a
Linux bff9f261bab2 4.15.0-106-generic #107-Ubuntu SMP Thu Jun 4 11:27:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@e7009c6ce357:/# exit
docker run
就是運作容器的指令,後面如果隻跟鏡像,那麼就執行鏡像的預設指令然後退出。
-
:這是兩個參數,一個是-it
:互動式操作,一個是-i
終端。我們這裡打算進入-t
執行一些指令并檢視傳回結果,是以我們需要互動式終端。bash
-
:這個參數是說容器退出後随之将其删除。預設情況下,為了排障需求,退出的容器并不會立即删除,除非手動--rm
。我們這裡隻是随便執行個指令,看看結果,不需要排障和保留結果,是以使用docker rm
可以避免浪費空間。--rm
-
:這是指用ubuntu:18.04
鏡像為基礎來啟動容器。ubuntu:18.04
-
:放在鏡像名後的是 指令,這裡我們希望有個互動式 Shell,是以用的是bash
。bash
進入容器後,我們可以在 Shell 下操作,執行任何所需的指令。通過
exit
退出。
檢視本地鏡像(list 鏡像)
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5f515359c7f8 5 days ago 183 MB
nginx latest 05a60462f8ba 5 days ago 181 MB
IMAGE ID 是鏡像的唯一辨別。
檢視運作中的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9363b1b51118 testlog:1 "bash" 7 weeks ago Up 7 weeks vigilant_bhaskara
CONTAINER ID 容器唯一id 可以通過指定這個ID操作exec shell進入容器、commit 這個容器的修改、tag 給這個容器打标簽等
docker ps 羅列的是目前活躍的容器
要檢視所有容器執行docker ps -a
$ docker ps -a
進入運作中/背景運作的容器
$ docker exec -it [CONTAINER ID] /bin/bash
進入運作中的容器後不僅可以調試鏡像,還可以對鏡像做修改如安裝python包,如果想對修改做保留,可以執行3.7送出
儲存修改
docker commit [CONTAINER ID] registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion
注意:通過commint的形式儲存現場為一個新的鏡像雖然也能直覺的達到建構新鏡像的目的,但是實際操作中,并不推薦這種形式,因為1.commit操作不僅會把有用的修改儲存下來,對一些無關的修改也會儲存下來(每一個指令行操作都會生成存儲如ls操作)就會導緻鏡像比較臃腫;2.因為commit操作屬于黑箱操作,後續如果有什麼問題維護起來會比較麻煩。
建議commit僅作為保留現場的手段,然後通過修改dockerfile建構鏡像。
打TAG
有時需要對臨時版本,或者節點版本做一個标記保留,打TAG标簽非常好用,并不會額外占用空間
docker tag registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion my_tmp_version:0.1
推送鏡像到倉庫
docker push registry.cn-shanghai.aliyuncs.com/test/pytorch:myversion
使用dockerfile建構鏡像
Dockerfile示例(注意一般檔案名命名為Dockerfile 無字尾名,如果命名為其他名字,建構時需要額外指定檔案名)
# Base Images
## 從天池基礎鏡像建構(from的base img 根據自己的需要更換,建議使用天池open list鏡像連結:https://tianchi.aliyun.com/forum/postDetail?postId=67720)
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
##安裝依賴包
RUN pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
##或者從requirements.txt安裝
##RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
## 把目前檔案夾裡的檔案建構到鏡像的根目錄下,并設定為預設工作目錄
ADD . /
WORKDIR /
## 鏡像啟動後統一執行 sh run.sh
CMD ["sh", "run.sh"]
點選下方課程連結即可直達dockerdfile入門視訊教程。
https://tianchi.aliyun.com/course/351/4129建構鏡像
docker build -t registry.cn-shanghai.aliyuncs.com/target:test .
如要指定dockerfile :
docker build -f ./dockerfile -t registry.cn-shanghai.aliyuncs.com/target:test .
删除鏡像/容器
删除鏡像:
docker rmi registry.cn-shanghai.aliyuncs.com/target:test
删除容器:
docker rm [CONTAINER ID]
如果容器還在運作,則會删除失敗,應先結束掉容器:
docker kill [CONTAINER ID]
檢視運作中的容器:
docker ps
檢視所有容器:
docker ps -a
正常技巧
a.檢查基礎鏡像軟體源和pip源是否替換為國内源,如果非國内源後續每次建構鏡像會比較浪費時間。
b.必備軟體包可直接安裝于基礎鏡像内,以減少每次建構鏡像時都要安裝一遍的等待時間。
c.鏡像面臨調試問題時,可互動式進入容器後直接調試修改,直到成功後退出再在dockerfile中修改。
d.養成使用Dockerfile的習慣,不要依賴于commit
e.每次鏡像修改都給定新的版本号或标簽,友善區分版本管理,有意義的版本最好使用有含義的字元作為版本号,如:frist_submit
深度學習常用鏡像集合(包含國内源和海外源)
https://tianchi.aliyun.com/forum/postDetail?postId=67720海外選手網速受限送出方案
a.使用github等代碼托管(推薦code.aliyun.com)
b.在cr(阿裡雲容器服務)産品做代碼源綁定,并開啟自動建構
c.建構完成設定hook自動送出(警惕次數消耗),或者謹慎起見建構完成後手動送出。
觸發器設定方法:
https://help.aliyun.com/document_detail/60949.html?spm=5176.8351553.0.dexternal.7f231991sUvASL點選下方課程連結即可直達如何建立自己的基礎鏡像視訊教程。
https://tianchi.aliyun.com/course/351/4131