Docker 入門
Tim Sangster
Software Engineer @ DataCamp
hello-world 映像會印出文字後停止。
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.
含有 python 的映像可在啟動時直接開啟 python。
docker run python3-sandbox
Python 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
...
....
>>> exit()
repl@host:/#
CMD <shell-command>
CMD 指令:
啟動應用程式以執行工作流程,或接受外部連線。
CMD python3 my_pipeline.py
CMD postgres
啟動一個腳本,再由它啟動多個應用程式
CMD start.sh
CMD python3 start_pipeline.py
$$
$$
越通用的映像需要越通用的啟動指令。
啟動映像
docker run <image>
以自訂啟動指令啟動映像
docker run <image> <shell-command>
互動式啟動映像並使用自訂啟動指令
docker run -it <image> <shell-command>
docker run -it ubuntu bash
| 用途 | Dockerfile 指令 |
|---|---|
| 在由映像啟動的容器中加入要執行的 shell 指令。 | CMD <shell-command> |
| 用途 | Shell 指令 |
|---|---|
| 覆寫映像中的 CMD | docker run <image> <shell-command> |
| 覆寫映像中的 CMD,並以互動式執行 | docker run -it <image> <shell-command> |
Docker 入門