iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0
Software Development

0 到 100 Linux快打旋風系列 第 17

Day17 - L先生(Linux)與D小姐(Docker)的秘密約會 (2)

  • 分享至 

  • xImage
  •  

前言

上一篇有跟大家分享一下 Docker 跟 Linux 之間的關係,本篇教大家如何安裝與操作~

開始安裝 Docker

  1. 更新 apt-get
    $sudo apt-get update

  2. 安裝軟件包以允許apt通過HTTPS使用存儲庫
    $sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common

  3. 添加Docker的官方GPG金鑰
    $curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  4. 設置穩定的存儲庫
    sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"

  5. 安裝最新版本的Docker Engine和容器
    $sudo apt-get install docker-ce docker-ce-cli containerd.io -y

  6. 安裝完成後可以查看 docker 版本,也可以得知是否有安裝成功
    $docker -v

不想用 root 身份管理 Docker

$ sudo groupadd docker $ sudo usermod -aG docker $USER

$newgrp docker $sudo systemctl restart docker.service

最後使用指令,來查看是否正常
$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/ 

就代表說成功了

實作啟動

既然已經安裝完成的話,我們就來分享如何使用 Docker

  1. 首先建立一個專案目錄
mkdir docker-test
cd docker-test
  1. 建立一個簡單的應用程式,我這邊使用的是 python 檔
# app.py
print("Docker Master!")
  1. 建立一個 Dockerfile
# 使用官方的 Python 基礎映像
FROM python:3.8-slim

# 將當前目錄內容複製到映像檔中的 /app 目錄
COPY . /app

# 設定工作目錄為 /app
WORKDIR /app

# 設定啟動命令,運行 Python 應用程式
CMD ["python", "app.py"]
  1. 建立 Docker 映像檔
docker build -t docker-test .
  1. 執行 Docker 容器
docker run docker-test

原則上可以看到終端回傳

Docker Master!
  1. 檢查 Docker 映像檔
docker images
  1. 查看有哪些容器正在運行
docker ps -a

回傳畫面如下

CONTAINER ID   IMAGE         COMMAND           CREATED             STATUS                         PORTS     NAMES
8abb877cc078   docker-test   "python app.py"   16 seconds ago      Exited (0) 14 seconds ago                gifted_mclean
f75796b8a83b   hello-world   "/hello"          37 minutes ago      Exited (0) 37 minutes ago                festive_yonath
6211e451682b   hello-world   "/hello"          About an hour ago   Exited (0) About an hour ago             distracted_driscoll

以上就大功告成拉

結論與感想

本篇希望大家能更加瞭解 Docker 的使用方式以及如何建立 Dockerfile

資料來源

https://philipzheng.gitbook.io/docker_practice/image/create

https://hackmd.io/@CynthiaChuang/The-Workflow-for-Creating-Your-Own-Custom-Image#Step3%EF%BD%9C%E6%92%B0%E5%AF%AB-Dockerfile


上一篇
Day16 - L先生(Linux)與D小姐(Docker)的秘密約會 (1)
下一篇
Day18 - Linux 簡易設置網頁伺服器
系列文
0 到 100 Linux快打旋風18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言