如何在 Fedora 上使用 Podman
Podman 是一個無守護(hù)程序的容器引擎,用于在你的 Linux 系統(tǒng)上開發(fā)、管理和運行 OCI 容器。在這篇文章中,我們將介紹 Podman 以及如何用 nodejs 構(gòu)建一個小型應(yīng)用來使用它。該應(yīng)用將是非常簡單和干凈的。
安裝 Podman
Podman 的命令就與 docker 相同,如果你已經(jīng)安裝了 Docker,只需在終端輸入 alias docker=podman
。
在 Fedora 中,Podman 是默認(rèn)安裝的。但是如果你因為任何原因沒有安裝,你可以用下面的命令安裝它:
sudo dnf install podman
對于 Fedora silverblue 用戶,Podman 已經(jīng)安裝在你的操作系統(tǒng)中了。
安裝后,運行 “hello world” 鏡像,以確保一切正常:
podman pull hello-world
podman 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/
簡單的 Nodejs 應(yīng)用
首先,我們將創(chuàng)建一個文件夾 webapp
,在終端輸入以下命令:
mkdir webapp && cd webapp
現(xiàn)在創(chuàng)建文件 package.json
,該文件包括項目運行所需的所有依賴項。在文件 package.json
中復(fù)制以下代碼:
{
"dependencies": {
"express": "*"
},
"scripts": {
"start": "node index.js"
}
}
創(chuàng)建文件 index.js
,并在其中添加以下代碼:
const express = require('express')
const app = express();
app.get('/', (req, res)=> {
res.send("Hello World!")
});
app.listen(8081, () => {
console.log("Listing on port 8080");
});
你可以從 這里 下載源代碼。
創(chuàng)建 Dockerfile
首先,創(chuàng)建一個名為 Dockerfile
的文件,并確保第一個字符是大寫,而不是小寫,然后在那里添加以下代碼:
FROM node:alpine
WORKDIR usr/app
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
確保你在 webapp
文件夾內(nèi),然后顯示鏡像,然后輸入以下命令:
podman build .
確保加了 .
。鏡像將在你的機器上創(chuàng)建,你可以用以下命令顯示它:
podman images
最后一步是輸入以下命令在容器中運行該鏡像:
podman run -p 8080:8080 <image-name>
現(xiàn)在在你的瀏覽器中打開 localhost:8080
,你會看到你的應(yīng)用已經(jīng)工作。
停止和刪除容器
使用 CTRL-C
退出容器,你可以使用容器 ID 來刪除容器。獲取 ID 并使用這些命令停止容器:
podman ps -a
podman stop <container_id>
你可以使用以下命令從你的機器上刪除鏡像:
podman rmi <image_id>
在 官方網(wǎng)站 上閱讀更多關(guān)于 Podman 和它如何工作的信息。