Docker 初學者的基礎指令與操作

本篇將初次體驗 docker 的操作


初次學習一項新的技術或語言 hello world 已經是一個慣例了,而 docker 官方也有提供此 image 供大家體驗。

docker hello world

輸入指令 docker run hello-world

1
2
3
4
5
6
7
8
9
10
11
 ✗ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

...下略

可以看到我們電腦並沒有此 image,因此會自動從網路上下載你想啟用的 image

輸入指令 docker image ls hello-world
可以看到 image 已經下載回來了。

1
2
3
➜  blog git:(master) ✗ docker image ls hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 4 months ago 13.3kB

輸入指令 docker run hello-world

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
➜  blog git:(master) ✗ docker run hello-world

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/

這樣其實就結束了,但各位肯定是沒什麼感覺得,我們先把上面的操作刪除吧!
因為上述的 image 已經被造出 container 了,所以刪除步驟會是 刪除 container -> 刪除 image

刪除 Container

指令 docker container ls -a

多輸入 -a 是因為該 container 執行完就關閉了,但還存在我們電腦中,可以看到下面的狀態已經是 Exited

1
2
3
➜  blog git:(master) ✗ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4f21b712f3e hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago nifty_chatterjee

指令 docker container rm c4f21b712f3e

1
2
➜  blog git:(master) ✗ docker container rm c4f21b712f3e
c4f21b712f3e

這個地方可以只用字首來省略整串 ID,但要記得別真的只用一個字,多打兩三個字避免誤刪相同字首開頭的 image。


刪除 Image

首先列出所有 image docker image ls

再進行刪除 image docker image rm feb5d9fea6a5

1
2
3
4
5
6
7
8
9
➜  blog git:(master) ✗ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 4 months ago 13.3kB

➜ blog git:(master) ✗ docker image rm feb5
Untagged: hello-world:latest
Untagged: [email protected]sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359

使用新的 nginx 應用程式來體驗 docker

  • 下載 nginx image: docker pull nginx
    因為有一些參數要使用,所以這邊暫時不用 docker run nginx

  • 執行 nginx `docker run -d -p 8080:80 c3

    • c3 是使用縮寫,請先確認你的 nginx image id 是多少 (docker image ls nginx)
    • -p 是將原本的對外 80 port 透過 8080 轉導,畢竟 80 port 已經預設已被佔用
    • -d detach 的縮寫,於背景執行,這樣就不用開一個新的 terminal
1
2
3
4
5
6
➜  blog git:(master) ✗ docker run -d -p 8080:80  c3
3bff5b5885ad83a449d61715bd9b8a6efa617fddd2dcbb25b3edb55733b9d8c2

➜ blog git:(master) ✗ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3bff5b5885ad c3 "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp affectionate_chaplygin

確認已經開始執行後,可以打開瀏覽器輸入 http://localhost:8080,確認看到 nginx 歡迎畫面就成功囉!


確認 container 狀態

container 狀態與啟動等細節都存放在 inspect 指令內

指令:docker inspect xxx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  ~ docker inspect 3b
[
{
"Id": "3bff5b5885ad83a449d61715bd9b8a6efa617fddd2dcbb25b3edb55733b9d8c2",
"Created": "2022-02-17T07:22:22.8048696Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,

//下略

確認 container log

有時候我們需要知道 container、發生什麼事,這時候就需要查看他的 log

指令:docker container logs xxx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜  ~ dk container logs 3b
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/02/17 07:22:24 [notice] 1#1: using the "epoll" event method
2022/02/17 07:22:24 [notice] 1#1: nginx/1.21.6
2022/02/17 07:22:24 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/17 07:22:24 [notice] 1#1: OS: Linux 5.10.25-linuxkit
2022/02/17 07:22:24 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/17 07:22:24 [notice] 1#1: start worker processes
2022/02/17 07:22:24 [notice] 1#1: start worker process 36
172.17.0.1 - - [17/Feb/2022:07:22:37 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" "-"
172.17.0.1 - - [17/Feb/2022:07:22:37 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" "-"
2022/02/17 07:22:37 [error] 34#34: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080", referrer: "http://localhost:8080/"

本篇介紹了如何下載 image、啟動容器、刪除容器、刪除 image,相關指令有很多細節,可以再去查看說明書。

  • docker
  • docker image
  • docker container
  • docker container logs
  • docker inspect

隨後面章節會提到更多。