執行 Docker 容器

Docker 入門

Tim Sangster

Software Engineer @ DataCamp

先決需求

指令 用途
nano <file-name> 以 nano 文字編輯器開啟 <file-name>
touch <file-name> 建立指定名稱的空白檔案
echo "<text>" 在主控台列印 <text>
<command> >> <file> 將 <command> 的輸出附加到 <file> 末端
<command> -y 對 <command> 的所有提示自動回覆 yes
Docker 入門

Docker CLI

  • Docker 命令列介面會把指令送到 Docker daemon。
  • 每個指令都以 docker 開頭。

名為 Ubuntu 的映像檔(完整 Ubuntu OS 安裝)可啟動為可互動的 Ubuntu OS,並用 shell 操作。

Docker 入門

Docker 容器輸出

docker run <image-name>
docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon created a new container from the hello-world image which runs
 the executable that produces the output you are currently reading.
3. The Docker daemon streamed that output to the Docker client, which sent it to 
 your terminal.
Docker 入門

選擇 Docker 容器輸出

docker run <image-name>
docker run ubuntu
repl@host:/# docker run ubuntu

repl@host:/#
Docker 入門

互動式 Docker 容器

docker run 加上 -it,可在啟動的容器中取得互動式 shell。

docker run -it <image-name>
docker run -it ubuntu
docker run -it ubuntu
repl@container:/#
repl@container:/# exit
exit
repl@host:/#
Docker 入門

以背景模式執行容器

docker run 加上 -d,會於背景執行容器,並把 shell 操作權交還給你。

docker run -d <image-name>
docker run -d postgres
repl@host:/# docker run -d postgres
4957362b5fb7019b56470a99f52218e698b85775af31da01958bab198a32b072
repl@host:/#
Docker 入門

列出並停止執行中的容器

docker ps
repl@host:/# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED  
4957362b5fb7   postgres   "docker-entrypoint.s…"   About a minute ago
STATUS              PORTS      NAMES
Up About a minute   5432/tcp   awesome_curie
docker stop <container-id>
repl@host:/# docker stop cf91547fd657
cf91547fd657
Docker 入門

新指令總結

用途 指令
啟動容器 docker run <image-name>
啟動互動式容器 docker run -it <image-name>
啟動背景容器 docker run -d <image-name>
列出執行中容器 docker ps
停止容器 docker stop <container-id>
Docker 入門

一起來練習吧!

Docker 入門

Preparing Video For Download...