> For the complete documentation index, see [llms.txt](https://stb11816.gitbook.io/python_note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stb11816.gitbook.io/python_note/docker.md).

# Docker

### 安裝

參考資料：

<https://marcus116.blogspot.com/2019/01/docker-docker-for-windows.html>

issue

virtualization-must-be-enabled

以系統管理員執行 + 重開電腦

```
bcdedit /set hypervisorlaunchtype auto
```

<https://blog.csdn.net/mythest/article/details/92999646>

### 基本指令

查看Image與Container

```
docker images
docker ps -a

# 查看使用中的container
docker ps -l
```

停止+刪除container

```
docker stop [container_id]
docker rm [container_id]
```

刪除可加上-f，強制刪除

搜尋image

```
docker search ubuntu -f is-official=true
```

ubuntu為關鍵字，is-official=true 表示要搜尋是官方的 Docker image

下載image

```
docker pull ubuntu
```

刪除image

```
docker rmi [image_id]
```

#### 啟動container

直接啟動，會自動給name，若沒有image會自動去下載

```
docker run image_name 或
docker run image_id
```

* \--name

```
docker run --name demo_test ubuntu
```

image記得放在最後

* -it

讓標準輸入維持在打開的狀態 + 替Container配置一個虛擬的終端機

```
docker run --name demo_test -it ubuntu
```

啟動後會進入終端機畫面

* -d

讓Container進入背景執行

```
docker run --name demo_test -d ubuntu
```

* -p

可以將主機的Port綁定到Container的Port，格式為host\_port:container\_port

```
docker run -p 8080:80 ubuntu
```

把主機上的8080連接埠綁定Container的80連接埠，所以可從主機使用127.0.0.1:port或ip:port連線至container的埠口

* -v

```
docker run -v /host/v1:/container/v2 ubuntu
docker run -v //c/Uesrs/Amin/www:/var/www/html。
```

掛載目錄，其格式為host\_volume:container\_volume

冒號前面是本機的目錄位置，冒號後面則是Container中的路徑(必需是絕對路徑)

範例二表示將Container內的/var/www/html資料夾路徑與外部 C：/Uesrs/Amin/www資料夾路徑相連，這樣可讓外部的檔案可直接與Container內部進行共同存取。

* \--rm

Container執行結束(exit)之後會自動被移除，移除後不會顯示在docker ps -a

```
docker run -p 8080:80 --rm ubuntu
```

* \--restart=always

如果 container 遇到例外的情況被 stop 掉，例如是重新開機，docker 會試著重新啟動此 container

<http://white5168.blogspot.com/2016/05/windowsdocker.html#.XnM4pKgzaUm>

<https://ithelp.ithome.com.tw/articles/10191634>

推薦：

[https://www.jinnsblog.com/2018/10/docker-container-command.html](https://legacy.gitbook.com/book/stb11816/python_note/edit#)

#### container狀態

查詢狀態

```
docker ps -a
```

啟動成功 ==> Up

停止 ==> Exited (0)

#### 進入/離開container

* 進入container

已經啟動的container才能使用exec進入

```
docker exec -it [container_name or container_id] bash
```

* 啟動然後直接進入container

```
docker start -ia [container_name]
```

* 離開container

```
exit
```

若以-ia進入，使用exit離開時container 會被關閉，status會變成 Exited(0)

若以exec進入，exit後狀態為Up

* 放置背景執行

可用鍵盤指令，container可維持在「Up」

```
ctrl + p
ctrl + q
```

* 再次回到背景執行的container

```
docker attach [container_name]
```

#### start/stop container

停止，狀態變成「Exited (0)」

```
docker stop [container_name]
```

啟動，狀態變成「Up」

```
docker start [container_name]
```

啟動然後直接進入container

```
docker start -ia [container_name]
```

#### 網路

查看網路設定

```
docker network ls
```

docker run會預設將建立的container放到"bridge"

查看bridge詳細資料

```
docker network inspect bridge
```

可以看到container所使用的網段

查詢container所使用的ip，container\_name可變更

```
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name
```

新增網段

```
docker network create -d bridge new_name
```

將container設定至新網段

```
docker run --name mysql579 --net new_name -e MYSQL_ROOT_PASSWORD=Nosecret -d mysql:5.7.9
```

若container已建立，需要更新網段

```
docker network connect new_name mysql579
```

斷開原本的bridge網段

```
docker network disconnect bridge mysql579
```

<https://dotblogs.com.tw/swater111/2017/01/03/171042>

## 針對已啟用 docker container 修改 Port

<https://roy051023.github.io/2018/01/15/Running-Docker-Change-Port/>

docker-machine

<https://www.runoob.com/docker/docker-machine.html>

#### 操作範例

啟動container範例 (mysql)，若沒有image沒有則會直接下載

```
docker run --name mysql579 -e MYSQL_ROOT_PASSWORD=Nosecret -d mysql:5.7.9
```

container名稱 = mysql579

root密碼 = Nosecret

版本 = 5.7.9

啟動後無法從windows連線至mysql579

```
docker run --name mysql800 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Nosecret -d mysql:8.0.0
```

加上-p 指令是要把container的某個port發佈到host主機上,

例如 -p 3306:3306 就是要把container 上的3306埠發佈在host的3306埠上

此就可以藉由windows的3306連線至container的3306了\~

要進入mysql579的mysql指令系統, 可以用一個新指令 docker exec

```
docker exec -it mysql579 mysql -uroot -p
```

這個指令的意思是想執行(execute) mysql579這個container裡的mysql指令, -it是互動模式的意思

<https://dotblogs.com.tw/swater111/2016/12/29/151738>

#### Linux套件

若要使用netstat

```
apt-get install net-tools
```

若要使用ping

```
apt-get install iputils-ping
```

Windows Container

<https://skychang.github.io/2017/01/06/Docker-Docker_for_Windows_10_First/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/docker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
