Docker 指令

Ubuntu安裝部分可參考此

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04

查看執行中的容器

sudo docker ps

終止某個container by ID

sudo docker stop 3e0485555356

使用docker hub可搜尋所有別人寫好的可用container https://hub.docker.com/

1.列出所有執行中的container

docker ps

2.列出被使用過名稱的container

docker ps -a

3.停止特定container

輸入

docker status

會顯示類似如下

接著

4.停止所有container

5.移除所有使用名稱的container

在kill或stop container後要再把名稱移除才可再次重新使用

6.以名字顯示running中的container

7.進入Docker的Terminal

8. Volume

可用來保存容器內的資料或是共通資料,named Volume 或 Host Volume。

記得使用host volume 時 路徑要用全名

之後更改host 或 docker 上 volume資料夾內的檔案後另一端也會跟著改變

9. 將特定資料夾指定為 Volume

使用 local-persist plugin,之後可以直接使用此 Volume,避免創建新的 Volume,如果用 docker-compose 要搭配 external: true

> https://docs.docker.com/compose/compose-file/compose-file-v3/#external

10. 檢視已經存在的 Volume

11. 將 docker log 存成檔案

mongo 之類的 docker image 產生的 log 也可以直接從 docker log 看到

12. host 與 container 複製

One specific file can be copied TO the container like:

One specific file can be copied FROM the container like:

13. docker 網路互通使用 network gateway

範例:

14. docker 之間使用 localhost 互相網路連通 (新方式,不用設置 network gateway)

雖然可用 docker run --network=host ,但此做法 docker 連得到 host (本機電腦) localhost 其他 port, 但電腦連不進 docker)

只要將代碼內的 localhost 改為 host.docker.internal 則可以連到其他電腦localhost,電腦也連得到 docker

Linux 要記得加上 flag: docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash

docker-compose 要加上如下:

https://stackoverflow.com/a/67158212/4622645

Last updated

Was this helpful?