2025年3月31日 星期一

Ubuntu 20.04 上使用 Docker / Container

大語言模型教得很好,真的不必再到處 google 了

如何安裝 Docker / Container 這邊就簡單講一下

也許以後的方法又不一樣

1. 更新現有套件

sudo apt update && sudo apt upgrade -y

2. 安裝相關套件

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
3. 加入 Docker 官方 GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. 設置 Stable Repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. 安裝 Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

6. 啟用 Docker

sudo systemctl enable docker
sudo systemctl start docker

7. 查看版本

sudo docker --version
sudo docker compose version

8. 運行 hello world 容器

sudo docker run hello-world

如果成功的話應該會看到一段 Hello from Docker 訊息

這邊就不貼了

然後以下是幾個常用指令

(a) 列出所有 image

sudo docker images

(b) 執行 image

sudo docker run -d --name container-name image-name

images 就是躺在 disk 裡面的東西

使用 docker run 它就會載入變成 container

這個 container-name 不能重複

至於怎麼製作 image 我們在下一篇文章再講

(c) 列出所有的 container

sudo docker ps -a

     列出正在運行的 container

sudo docker ps

(d) 停止 container

sudo docker stop container-name

(e) 執行 container

如果之前已經使用過 run 把 image 變成 container

那麼再次啟動時要使用 start

sudo docker start container-name

(f) 移除 container

sudo docker rm container-name

(g) 移除 image

sudo docker rmi image-name

上面提到的都是使用 "container name" & "image name"

但其實也可以用 "container id" & "image id"


差不多講到這


2025年3月1日 星期六

如何在 ubuntu 20.04 把 shell script 寫成 service

 首先,當你有問題時,第一個該試的就是問大語言模型

大語言模型回答得很好

我這邊就簡單的講一下所需步驟

1. 建立 systemd service unit file

基本上這類型的檔案都會放在 /etc/systemd/system 這個目錄下

舉例來說,我的 script 叫做 my-script.sh

而我想要建立一個 my-script.service

我只需要打開 terminal 並執行

sudo nano /etc/systemd/system/my-script.service


2. 在上面的指令打開檔案後,加入下列內容

[Unit]
Description=My Custom Script
After=network.target

[Service]
ExecStart=/bin/bash /path/to/your/script.sh
WorkingDirectory=/path/to/working/directory
StandardOutput=journal
StandardError=journal
Restart=on-failure
User=your_user_name
Group=your_group_name

[Install]
WantedBy=multi-user.target

這邊補充一些項目

(a) ExecStart 其實可以寫成

ExecStart="/path/to/your/script.sh"

然後以我自己的情況來說

(b) WorkingDirectory 我會寫成 sh 的 folder

也就是下面這樣

WorkingDirectory=/path/to/your

(c) [Unit] 裡面的 After 如果不需要的話可以留空

After=

直接刪掉也許也可以吧

(d) Restart 除了 "on-failure" 之外,還有 "always"

(e) 還有一個參數叫做 RestartSec

RestartSec=60

應該是掛掉後 隔 60 秒再啟動

(f) User, Group 那些都可以刪掉沒差

sudo apt update
sudo apt install samba

(g) Type 目前遇到都是寫 simple 就可以了

(h) StandardOutput, StandardError 用來輸出 logs (目前沒用過) 

3. 重新讀取 service file

sudo systemctl daemon-reload


4. 啟用 service

sudo systemctl enable my-script.service


5. 開始 service

sudo systemctl start my-script.service


6. 檢查 service 狀態

sudo systemctl status my-script.service


7. 停止 service

sudo systemctl stop my-script.service


註: 如果你的 sh 沒有執行權限,可以使用下列指令

chmod +x /path/to/your/script.sh


大概就這樣吧,寫成 service 就可以很輕鬆地背景執行了



2025年1月31日 星期五

兩台電腦如何存取同一個檔案

今天遇到的問題 : 兩台電腦如何存取同一個檔案

大概有想到幾個方法

1. ubuntu 開 share folder 給 windows

2. windows 開 share folder 給 unbuntu

3. 用 mongoDB 做 KV store

4. 用 amazonS3

最後採用 1 的作法,順便記錄一下

一開始先是上網 google 了一下

很快就找到一堆人講,試了 2 個都不 work,也不知道是缺啥

打開 chatgpt 問一下詳細多了

step1. 安裝 Samba(SMB)

sudo apt update
sudo apt install samba

step2. 編輯設定檔

sudo nano /etc/samba/smb.conf

打開後 scroll 到最下面,然後加入下面這段

[SharedFolder]
path = /path/to/share
available = yes
valid users = your_username
read only = no
browsable = yes
public = yes
writable = yes

step3. 設定 Samba 密碼

sudo smbpasswd -a your_username

step4. 重啟 Samba 服務

sudo systemctl restart smbd

step5. 防火牆允許 Samba 服務

sudo ufw allow samba

step6. 完成,現在可以在File Explorer輸入下列位置就可以訪問了

\\ubuntu_ip_address\SharedFolder

他會要你輸入 username & password 就是你在 step3 設置的

----------------------------------------------------------------------------------------------

如果我在 Ubuntu 有兩個資料夾要分享該怎麼做?

chatgpt 的回答如下,供參考

step1. 編輯設定檔

sudo nano /etc/samba/smb.conf

打開後 scroll 到最下面,然後加入下面這段

[Folder1]
path = /home/username/folder1
available = yes
valid users = your_username
read only = no
browsable = yes
public = yes
writable = yes

[Folder2]
path = /home/username/folder2
available = yes
valid users = your_username
read only = no
browsable = yes
public = yes
writable = yes

----------------------------------------------------------------------------------------------

如果要用 python 存取 shared folder 該怎麼做?

1. 使用 smbprotocol

pip install smbprotocol

然後使用下面這段程式

import smbprotocol
from smbprotocol import smb2
from smbprotocol import client

# Initialize the SMB protocol (need to run this before anything else)
smbprotocol.ClientConfig(username="your_username", password="your_password", domain="WORKGROUP")

# Set the server address and share name
server = "192.168.1.100"  # IP address of the Ubuntu machine with the shared folder
share = "Folder1"  # The name of the share in smb.conf
file_path = "testfile.txt"  # File inside the shared folder you want to access

# Connect to the share
client = smb2.SMB2(server, 445)

# Open the shared folder and access the file
with client.open(share, file_path, "rb") as file:
    data = file.read()  # Read the file content
    print(data.decode())  # Print the content of the file

# You can also use client.list() to list files and directories
for item in client.list(share):
    print(item)

But

我想使用 json 來做存取,譬如下面這樣

with open(json_path, mode="w", encoding="utf-8") as f: 
    json.dump(json_data, f, indent=4, ensure_ascii=False)

這時候 chatgpt 就建議使用 mount 的方式了

如果是用 windows 的話,打開 File Explorer 的首頁

按右鍵就會出現 Add a network location



在 windows 我可以用以下 path 存取

path = r"\\192.168.50.75\sharedfolder\bb.json"

但這段字丟到 linux 上會有問題,用下面這樣打開 json 會報錯

with open(json_path, mode="r", encoding="utf-8") as f: 
    json_data = json.loads(f.read())

因為 linux 上使用的是 "/" 而不是 "\"

所以要做

path = path.replace("\\", "/")

大概是這樣