mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
translated
This commit is contained in:
parent
833c8b7c6c
commit
b81324743f
@ -1,180 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Manage containers with Podman Compose)
|
||||
[#]: via: (https://fedoramagazine.org/manage-containers-with-podman-compose/)
|
||||
[#]: author: (Mehdi Haghgoo https://fedoramagazine.org/author/powergame/)
|
||||
|
||||
Manage containers with Podman Compose
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Containers are awesome, allowing you to package your application along with its dependencies and run it anywhere. Starting with Docker in 2013, containers have been making the lives of software developers much easier.
|
||||
|
||||
One of the downsides of Docker is it has a central daemon that runs as the root user, and this has security implications. But this is where Podman comes in handy. Podman is a [daemonless container engine][2] for developing, managing, and running OCI Containers on your Linux system in root or rootless mode.
|
||||
|
||||
There are other articles on Fedora Magazine you can use to learn more about Podman. Two examples follow:
|
||||
|
||||
* [Using Pods with Podman on Fedora][3]
|
||||
* [Podman with Capabilities on Fedora][4]
|
||||
|
||||
|
||||
|
||||
If you have worked with Docker, chances are you also know about Docker Compose, which is a tool for orchestrating several containers that might be interdependent. To learn more about Docker Compose see its [documentation][5].
|
||||
|
||||
### What is Podman Compose?
|
||||
|
||||
[Podman Compose][6] is a project whose goal is to be used as an alternative to Docker Compose without needing any changes to be made in the docker-compose.yaml file. Since Podman Compose works using pods, it’s good to check a refresher definition of a pod.
|
||||
|
||||
> A _Pod_ (as in a pod of whales or pea pod) is a group of one or more [containers][7], with shared storage/network resources, and a specification for how to run the containers.
|
||||
>
|
||||
> [Pods – Kubernetes Documentation][8]
|
||||
|
||||
The basic idea behind Podman Compose is that it picks the services defined inside the _docker-compose.yaml_ file and creates a container for each service. A major difference between Docker Compose and Podman Compose is that Podman Compose adds the containers to a single pod for the whole project, and all the containers share the same network. It even names the containers the same way Docker Compose does, using the _‐‐add-host_ flag when creating the containers, as you will see in the example.
|
||||
|
||||
### Installation
|
||||
|
||||
Complete install instructions for Podman Compose are found on its [project page][6], and there are several ways to do it. To install the latest development version, use the following command:
|
||||
|
||||
```
|
||||
pip3 install https://github.com/containers/podman-compose/archive/devel.tar.gz
|
||||
```
|
||||
|
||||
Make sure you also have [Podman installed][9] since you’ll need it as well. On Fedora, to install Podman use the following command:
|
||||
|
||||
```
|
||||
sudo dnf install podman
|
||||
```
|
||||
|
||||
### Example: launching a WordPress site with Podman Compose
|
||||
|
||||
Imagine your _docker-compose.yaml_ file is in a folder called _wpsite_. A typical _docker-compose.yaml_ (or _docker-compose.yml_) for a WordPress site looks like this:
|
||||
|
||||
```
|
||||
version: "3.8"
|
||||
services:
|
||||
web:
|
||||
image: wordpress
|
||||
restart: always
|
||||
volumes:
|
||||
- wordpress:/var/www/html
|
||||
ports:
|
||||
- 8080:80
|
||||
environment:
|
||||
WORDPRESS_DB_HOST: db
|
||||
WORDPRESS_DB_USER: magazine
|
||||
WORDPRESS_DB_NAME: magazine
|
||||
WORDPRESS_DB_PASSWORD: 1maGazine!
|
||||
WORDPRESS_TABLE_PREFIX: cz
|
||||
WORDPRESS_DEBUG: 0
|
||||
depends_on:
|
||||
- db
|
||||
networks:
|
||||
- wpnet
|
||||
db:
|
||||
image: mariadb:10.5
|
||||
restart: always
|
||||
ports:
|
||||
- 6603:3306
|
||||
|
||||
volumes:
|
||||
- wpdbvol:/var/lib/mysql
|
||||
|
||||
environment:
|
||||
MYSQL_DATABASE: magazine
|
||||
MYSQL_USER: magazine
|
||||
MYSQL_PASSWORD: 1maGazine!
|
||||
MYSQL_ROOT_PASSWORD: 1maGazine!
|
||||
networks:
|
||||
- wpnet
|
||||
volumes:
|
||||
wordpress: {}
|
||||
wpdbvol: {}
|
||||
|
||||
networks:
|
||||
wpnet: {}
|
||||
```
|
||||
|
||||
If you come from a Docker background, you know you can launch these services by running _docker-compose up_. Docker Compose will create two containers named _wpsite_web_1_ and _wpsite_db_1_, and attaches them to a network called _wpsite_wpnet_.
|
||||
|
||||
Now, see what happens when you run _podman-compose up_ in the project directory. First, a pod is created named after the directory in which the command was issued. Next, it looks for any named volumes defined in the YAML file and creates the volumes if they do not exist. Then, one container is created per every service listed in the _services_ section of the YAML file and added to the pod.
|
||||
|
||||
Naming of the containers is done similar to Docker Compose. For example, for your web service, a container named _wpsite_web_1_ is created. Podman Compose also adds localhost aliases to each named container. Then, containers can still resolve each other by name, although they are not on a bridge network as in Docker. To do this, use the option _–add-host_. For example, _–add-host web:localhost_.
|
||||
|
||||
Note that _docker-compose.yaml_ includes a port forwarding from host port 8080 to container port 80 for the web service. You should now be able to access your fresh WordPress instance from the browser using the address _<http://localhost:8080>_.
|
||||
|
||||
![WordPress Dashboard][10]
|
||||
|
||||
### Controlling the pod and containers
|
||||
|
||||
To see your running containers, use _podman ps_, which shows the web and database containers along with the infra container in your pod.
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
a364a8d7cec7 docker.io/library/wordpress:latest apache2-foregroun... 2 hours ago Up 2 hours ago 0.0.0.0:8080-&gt;80/tcp, 0.0.0.0:6603-&gt;3306/tcp wpsite_web_1
|
||||
c447024aa104 docker.io/library/mariadb:10.5 mysqld 2 hours ago Up 2 hours ago 0.0.0.0:8080-&gt;80/tcp, 0.0.0.0:6603-&gt;3306/tcp wpsite_db_1
|
||||
12b1e3418e3e k8s.gcr.io/pause:3.2
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
You can also verify that a pod has been created by Podman for this project, named after the folder in which you issued the command.
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
POD ID NAME STATUS CREATED INFRA ID # OF CONTAINERS
|
||||
8a08a3a7773e wpsite Degraded 2 hours ago 12b1e3418e3e 3
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
To stop the containers, enter the following command in another command window:
|
||||
|
||||
```
|
||||
podman-compose down
|
||||
```
|
||||
|
||||
You can also do that by stopping and removing the pod. This essentially stops and removes all the containers and then the containing pod. So, the same thing can be achieved with these commands:
|
||||
|
||||
```
|
||||
podman pod stop podname
|
||||
podman pod rm podname
|
||||
```
|
||||
|
||||
Note that this does not remove the volumes you defined in _docker-compose.yaml_. So, the state of your WordPress site is saved, and you can get it back by running this command:
|
||||
|
||||
```
|
||||
podman-compose up
|
||||
```
|
||||
|
||||
In conclusion, if you’re a Podman fan and do your container jobs with Podman, you can use Podman Compose to manage your containers in development and production.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/manage-containers-with-podman-compose/
|
||||
|
||||
作者:[Mehdi Haghgoo][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/powergame/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/01/podman-compose-1-816x345.jpg
|
||||
[2]: https://podman.io
|
||||
[3]: https://fedoramagazine.org/podman-pods-fedora-containers/
|
||||
[4]: https://fedoramagazine.org/podman-with-capabilities-on-fedora/
|
||||
[5]: https://docs.docker.com/compose/
|
||||
[6]: https://github.com/containers/podman-compose
|
||||
[7]: https://kubernetes.io/docs/concepts/containers/
|
||||
[8]: https://kubernetes.io/docs/concepts/workloads/pods/
|
||||
[9]: https://podman.io/getting-started/installation
|
||||
[10]: https://fedoramagazine.org/wp-content/uploads/2021/01/Screenshot-from-2021-01-08-06-27-29-1024x767.png
|
@ -0,0 +1,184 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Manage containers with Podman Compose)
|
||||
[#]: via: (https://fedoramagazine.org/manage-containers-with-podman-compose/)
|
||||
[#]: author: (Mehdi Haghgoo https://fedoramagazine.org/author/powergame/)
|
||||
|
||||
用 Podman Compose 管理容器
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
容器很棒,让你可以将你的应用连同其依赖项一起打包,并在任何地方运行。从 2013 年的 Docker 开始,容器已经让软件开发者的生活变得更加轻松。
|
||||
|
||||
Docker 的一个缺点是它有一个中央守护进程,它以 root 用户的身份运行,这对安全有影响。但这正是 Podman 的用武之地。Podman 是一个 [无守护进程容器引擎][2],用于开发、管理和在你的 Linux 系统上以 root 或无 root 模式运行 OCI 容器。
|
||||
|
||||
在 Fedora Magazine 上还有其他文章,你可以用来了解更多关于 Podman 的信息。下面有两个例子:
|
||||
|
||||
* [在 Fedora 上使用 Podman 的 Pod][3]
|
||||
* [在 Fedora 上具有 Capabilities 的Podman][4]
|
||||
|
||||
|
||||
|
||||
如果你使用过 Docker,你很可能也知道 Docker Compose,它是一个用于编排多个可能相互依赖的容器的工具。要了解更多关于 Docker Compose 的信息,请看它的[文档][5]。
|
||||
|
||||
### 什么是 Podman Compose?
|
||||
|
||||
[Podman Compose][6]是一个目标作为 Docker Compose 的替代品,不需要对 docker-compose.yaml 文件进行任何修改的项目。由于 Podman Compose 使用 pod 工作,所以最好看下 pod 的最新定义。
|
||||
|
||||
|
||||
> 一个_Pod_(如一群鲸鱼或豌豆荚)是由一个或多个[容器][7]组成的组,具有共享的存储/网络资源,以及如何运行容器的规范。
|
||||
>
|
||||
> [Pods - Kubernetes 文档][8]
|
||||
|
||||
Podman Compose 的基本思想是,它选中 _docker-compose.yaml_ 文件里面定义的服务,为每个服务创建一个容器。Docker Compose 和 Podman Compose 的一个主要区别是,Podman Compose 将整个项目的容器添加到一个单一的 pod 中,而且所有的容器共享同一个网络。它甚至用和 Docker Compose 一样的方式命名容器,在创建容器时使用 _--add-host_ 标志,你会在例子中看到。
|
||||
|
||||
|
||||
### 安装
|
||||
|
||||
Podman Compose 的完整安装说明可以在[项目页面][6]上找到,它有几种方法。要安装最新的开发版本,使用以下命令:
|
||||
|
||||
```
|
||||
pip3 install https://github.com/containers/podman-compose/archive/devel.tar.gz
|
||||
```
|
||||
|
||||
确保你也安装了 [Podman][9],因为你也需要它。在 Fedora 上,使用下面的命令来安装Podman:
|
||||
|
||||
```
|
||||
sudo dnf install podman
|
||||
```
|
||||
|
||||
### 例子:用 Podman Compose 启动一个 WordPress 网站
|
||||
|
||||
想象一下,你的 _docker-compose.yaml_ 文件在一个叫 _wpsite_ 的文件夹里。一个典型的 WordPress 网站的 _docker-compose.yaml_ (或 _docker-compose.yml_) 文件是这样的:
|
||||
|
||||
```
|
||||
version: "3.8"
|
||||
services:
|
||||
web:
|
||||
image: wordpress
|
||||
restart: always
|
||||
volumes:
|
||||
- wordpress:/var/www/html
|
||||
ports:
|
||||
- 8080:80
|
||||
environment:
|
||||
WORDPRESS_DB_HOST: db
|
||||
WORDPRESS_DB_USER: magazine
|
||||
WORDPRESS_DB_NAME: magazine
|
||||
WORDPRESS_DB_PASSWORD: 1maGazine!
|
||||
WORDPRESS_TABLE_PREFIX: cz
|
||||
WORDPRESS_DEBUG: 0
|
||||
depends_on:
|
||||
- db
|
||||
networks:
|
||||
- wpnet
|
||||
db:
|
||||
image: mariadb:10.5
|
||||
restart: always
|
||||
ports:
|
||||
- 6603:3306
|
||||
|
||||
volumes:
|
||||
- wpdbvol:/var/lib/mysql
|
||||
|
||||
environment:
|
||||
MYSQL_DATABASE: magazine
|
||||
MYSQL_USER: magazine
|
||||
MYSQL_PASSWORD: 1maGazine!
|
||||
MYSQL_ROOT_PASSWORD: 1maGazine!
|
||||
networks:
|
||||
- wpnet
|
||||
volumes:
|
||||
wordpress: {}
|
||||
wpdbvol: {}
|
||||
|
||||
networks:
|
||||
wpnet: {}
|
||||
```
|
||||
|
||||
如果你用过 Docker,你就会知道你可运行 _docker-compose up_ 来启动这些服务。Docker Compose 会创建两个名为 _wpsite_web_1_ 和 _wpsite_db_1_ 的容器,并将它们连接到一个名为 _wpsite_wpnet_ 的网络。
|
||||
|
||||
|
||||
现在,看看当你在项目目录下运行 _podman-compose up_ 时会发生什么。首先,一个以执行命令的目录命名的 pod 被创建。接下来,它寻找 YAML 文件中定义的任何名称的卷,如果它们不存在,就创建卷。然后,在 YAML 文件的 _services_ 部分列出的每个服务都会创建一个容器,并添加到 pod 中。
|
||||
|
||||
容器的命名与 Docker Compose 类似。例如,为你的 web 服务创建一个名为 _wpsite_web_1_ 的容器。Podman Compose 还为每个命名的容器添加了 localhost 别名。之后,容器仍然可以通过名字互相解析,尽管它们并不像 Docker 那样在一个桥接网络上。要做到这一点,使用选项 _-add-host_。例如,_-add-host web:localhost_。
|
||||
|
||||
请注意,_docker-compose.yaml_ 包含了一个从主机 8080 端口到容器 80 端口的 Web 服务的端口转发。现在你应该可以通过浏览器访问新 WordPress 实例,地址为 _<http://localhost:8080>_。
|
||||
|
||||
|
||||
![WordPress Dashboard][10]
|
||||
|
||||
### 控制 pod 和容器
|
||||
|
||||
要查看正在运行的容器,使用 _podman ps_,它可以显示 web 和数据库容器以及 pod 中的 infra 容器。
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
a364a8d7cec7 docker.io/library/wordpress:latest apache2-foregroun... 2 hours ago Up 2 hours ago 0.0.0.0:8080-&gt;80/tcp, 0.0.0.0:6603-&gt;3306/tcp wpsite_web_1
|
||||
c447024aa104 docker.io/library/mariadb:10.5 mysqld 2 hours ago Up 2 hours ago 0.0.0.0:8080-&gt;80/tcp, 0.0.0.0:6603-&gt;3306/tcp wpsite_db_1
|
||||
12b1e3418e3e k8s.gcr.io/pause:3.2
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
你也可以验证 Podman 已经为这个项目创建了一个 pod,以你执行命令的文件夹命名。
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
POD ID NAME STATUS CREATED INFRA ID # OF CONTAINERS
|
||||
8a08a3a7773e wpsite Degraded 2 hours ago 12b1e3418e3e 3
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
要停止容器,在另一个命令窗口中输入以下命令:
|
||||
|
||||
```
|
||||
podman-compose down
|
||||
```
|
||||
|
||||
你也可以通过停止和删除 pod 来实现。这实质上是停止并移除所有的容器,然后再删除包含的 pod。所以,同样的事情也可以通过这些命令来实现:
|
||||
|
||||
```
|
||||
podman pod stop podname
|
||||
podman pod rm podname
|
||||
```
|
||||
|
||||
请注意,这不会删除你在 _docker-compose.yaml_ 中定义的卷。所以,你的 WordPress 网站的状态被保存下来了,你可以通过运行这个命令来恢复它。
|
||||
|
||||
```
|
||||
podman-compose up
|
||||
```
|
||||
|
||||
总之,如果你是一个 Podman 粉丝,并且用 Podman 做容器工作,你可以使用 Podman Compose 来管理你的开发和生产中的容器。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/manage-containers-with-podman-compose/
|
||||
|
||||
作者:[Mehdi Haghgoo][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/powergame/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/01/podman-compose-1-816x345.jpg
|
||||
[2]: https://podman.io
|
||||
[3]: https://fedoramagazine.org/podman-pods-fedora-containers/
|
||||
[4]: https://fedoramagazine.org/podman-with-capabilities-on-fedora/
|
||||
[5]: https://docs.docker.com/compose/
|
||||
[6]: https://github.com/containers/podman-compose
|
||||
[7]: https://kubernetes.io/docs/concepts/containers/
|
||||
[8]: https://kubernetes.io/docs/concepts/workloads/pods/
|
||||
[9]: https://podman.io/getting-started/installation
|
||||
[10]: https://fedoramagazine.org/wp-content/uploads/2021/01/Screenshot-from-2021-01-08-06-27-29-1024x767.png
|
Loading…
Reference in New Issue
Block a user