---
description: docker
---

# 20190917

上禮拜筆記:[https://blog.51cto.com/12098022/2437854](https://blog.51cto.com/12098022/2437854)

```
# docker run -it -d chusiang/takaojs1607 /bin/sh -c "while true; do echo "hi"; sleep 1; done"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-LoyshpZxNaOpMnlrJsL%2Fimage.png?alt=media\&token=9c533581-8f0f-40d8-bac0-7b1434e62193)

​準備兩台終端機

```
# cd /
# mkdir /mydata
# echo "hello world" > hi.txt
# cat hi.txt
```

```
# docker run -it -v /mydata/data chusiang/takaojs1607 /bin/bash
# cd /
# cd data
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-LoyxSOkdXmX02Xxa_Lh%2Fimage.png?alt=media\&token=bb201441-ef27-4f2d-9990-9a56de7c85b5)

```
#/bin/bash -c "while true; do echo "hi"; sleep 3 ; done"
```

```
# docker exec -it [容器名稱] bash
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-Loyzoj0K2yophOxSpat%2Fimage.png?alt=media\&token=8fd8df7e-48dd-4e01-95bc-a5d578671c04)

```
# docker run -it --name [容器名稱] --rm chusiang/takaojs1607 /bin/bash
# docker ps -a
# docker start [容器名稱]
# docker attach [容器名稱]
```

```
暫時離開容器：ctrl+p+q
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-Loz2FckfpguUW2B4DJZ%2Fimage.png?alt=media\&token=f4fc5a01-89fd-4d66-ad25-f01cc8ec0579)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-Loz2ItexorLq_3Zh1Ck%2Fimage.png?alt=media\&token=8bc5737b-f5fa-4b04-b629-bae50ca476bc)

```
# apt-get update
# apt-get install iputils-ping
如果上兩段程式有出現bug，使用：
# apt-get install --fix-missing

# apt install net-tools
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-LozEuyGlkoCyP-S8cIh%2Fimage.png?alt=media\&token=90c35500-6c72-45ff-a37c-58805771147d)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-LozEqBS-ox93hScZRl4%2Fimage.png?alt=media\&token=01b5ae5d-2511-44a3-8307-4a6484c7fa0e)

```
# docker pull httpd
# docker images
將root路徑切換到mydata下執行：
# echo "this is a index webpage" > index.html
# echo "hi" > hi.htm
# docker run -itd --name mywebserver -p 8080:80 -v /mydata:/usr/local/apache2/htdocs httpd
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LoysNGM8fSse79iomhu%2F-LozEiBoMeeQCkrukvO9%2Fimage.png?alt=media\&token=b0e61a4a-c99d-43ec-9acd-4b59f4347c56)

