当下运行容器的 3 个步骤
容器 WordPress

> 
> 在本教程中，你将学习如何在一个“吊舱”中运行两个容器来托管一个 WordPress 站点。
> 
> 
> 


![](/data/attachment/album/202205/12/121429y2s9v96rxxk2litk.jpg "Shipping containers stacked")


无论你是将其作为工作的一部分、未来的工作机会或者仅仅是出于对新技术的兴趣，容器对很多人，即使是经验丰富的系统管理员，可能是非常难以应付的。那么如何真正开始使用容器呢？从容器到 [Kubernetes](https://opensource.com/tags/kubernetes) 的成长路径是什么？另外，为什么有不止一条路径？如你所料，最好的起点就是现在。


### 1、了解容器


略一回忆，容器的开端可以追溯到早期 BSD 及其特殊的 chroot 监狱，但让我们直接跳到发展中期讲起。


之前，Linux 内核引入了 “<ruby> 控制组 <rt>  cgroup </rt></ruby>”，允许你能够使用 “<ruby> 命名空间 <rt>  namespace </rt></ruby>” 来“标记”进程。当你将进程分组到一个命名空间时，这些进程的行为就像在命名空间之外的东西不存在一样，这就像你把这些进程放入某种容器中。当然，这种容器是虚拟的，它位于计算机内部，它和你操作系统的其余进程使用相同的内核、内存和 CPU，但你用容器包含了这些进程。


分发的预制容器仅包含运行它所包含的应用程序必须的内容。使用容器引擎，如 [Podman](https://www.redhat.com/sysadmin/podman-guides-2020)、Docker 或 CRI-O，你可以运行一个容器化应用程序，而无需进行传统意义上的安装。容器引擎通常是跨平台的，因此即使容器运行在 Linux 上，你也可以在其他 Linux、MacOS 或 Windows 上启动容器。


更重要的是，当需求量很大时，你可以运行同一应用程序的多个容器。


现在你知道了什么是容器，下一步是运行一个容器。


### 2、运行一个容器


在运行容器之前，你应该有一个想要运行它的理由。你可以编一个，这有助于你对让容器创建过程感兴趣，这样你就会受到鼓舞，真正去使用你所运行的容器。毕竟，运行容器但不使用它提供的应用程序，只能证明你没有注意到任何故障，但使用容器证明它可以工作。


我推荐从 WordPress 开始，它是一个很流行的 Web 应用程序，容易使用，所以一旦容器运行起来，你就可以测试使用它。虽然你可以轻松地配置一个 WordPress 容器，但还是有很多配置选项可以引导你发现更多运行容器的方式（例如运行数据库容器）以及容器如何通信。


我使用 Podman，它是一个友好、方便且无守护进程的容器引擎。如果你没有安装 Podman，可以改用 Docker 命令。它们都是很棒的开源容器引擎，而且它们的语法是相同的（只需输入 `docker` 而不是 `podman`）。因为 Podman 没有守护进程，所以它需要更多的配置，但为了这种运行免 root、无守护进程的容器的能力是值得的。


如果你使用 Docker，可以跳到下面的 [运行 WordPress 容器](file:///Users/xingyuwang/develop/TranslateProject-wxy/translated/tech/tmp.1zBHYsK8TH#wp) 小节，否则，打开终端安装并配置 Podman：



```
$ sudo dnf install podman

```

容器会产生许多进程，通常只有 root 用户有权创建数千个进程 ID。创建一个名为 `/etc/subuid` 的文件，定义一个适当的起始 UID 和大量合法的 PID，这样就可以为你添加一些额外的进程 ID：



```
seth:200000:165536

```

在名为 `/etc/subgid` 的文件中对你的组执行相同的操作。在这个例子中，我的主要组是 `staff`（对你来说可能是 `users`，或者和你的用户名一样，这取决于你的系统）。



```
staff:200000:165536

```

最后，确认你的用户可以管理很多命名空间：



```
$ sysctl --all --pattern user_namespaces
user.max_user_namespaces = 28633

```

如果你的用户无权管理超过 28,000 个命名空间，创建 `/etc/sysctl.d/userns.conf` 文件来增加数量并输入：



```
user.max_user_namespaces=28633

```

#### 运行 WordPress 容器


现在，无论你使用的是 Podman 还是 Docker，你都可以从在线容器仓库中下载 WordPress 容器并运行它。你可以使用以下 Podman 命令完成所有这些操作：



```
$ podman run --name mypress \
    -p 8080:80 -d wordpress

```

给 Podman 一会时间来找到容器、从互联网下载它，然后启动。


在收到终端返回提示符后，启动 Web 浏览器，打开 `localhost:8080`。WordPress 正在运行，等待你进行设置。


![WordPress 容器](/data/attachment/album/202205/12/121432u8wfzo1qltggjgoo.jpg "WordPress running in a container")


不过，你很快就会遇到障碍，因为 WordPress 使用数据库来存储数据，因此你需要为其提供一个数据库。


在继续之前，停止并删除 WordPress 容器：



```
$ podman stop mypress
$ podman rm mypress

```

### 3、在吊舱中运行容器


正如名字所暗示的那样，容器在设计上是独立的。在容器中运行的应用程序不应该与在容器外的应用程序或基础设施进行交互。因此，当一个容器需要另一个容器才能运行时，一种解决方案是将这两个容器放在一个更大的容器中，称为 “<ruby> 吊舱 <rt>  pod </rt></ruby>”。吊舱确保其容器可以共享重要的命名空间以便相互通信。


创建一个新的吊舱，为它提供一个名称，以及希望能够访问的端口：



```
$ podman pod create \
    --name wp_pod \
    --publish 8080:80

```

确认吊舱存在：



```
$ podman pod list
POD ID        NAME     STATUS    INFRA ID      # OF CONTAINERS
100e138a29bd  wp_pod   Created   22ace92df3ef   1

```

#### 将容器添加到吊舱


现在你已经为相互依赖的容器创建了一个吊舱，你可以通过指定一个运行的吊舱来启动每个容器。


首先，启动一个数据库容器。你可以创建自己的凭据，只要在 WordPress 连接到数据库时使用相同的凭据。



```
$ podman run --detach \
    --pod wp_pod \
    --restart=always \
    -e MYSQL_ROOT_PASSWORD="badpassword0" \
    -e MYSQL_DATABASE="wp_db" \
    -e MYSQL_USER="tux" \
    -e MYSQL_PASSWORD="badpassword1" \
    --name=wp_db mariadb

```

接下来，在同一个吊舱中启动 WordPress 容器：



```
$ podman run --detach \
    --restart=always --pod=wp_pod \
    -e WORDPRESS_DB_NAME="wp_db" \
    -e WORDPRESS_DB_USER="tux" \
    -e WORDPRESS_DB_PASSWORD="badpassword1" \
    -e WORDPRESS_DB_HOST="127.0.0.1" \
    --name mypress wordpress

```

现在启动你最喜欢的网络浏览器并打开 `localhost:8080`。


这一次，设置会正常进行。WordPress 会连接到数据库，因为你在启动容器时传递了这些环境变量。


![WordPress 启动](/data/attachment/album/202205/12/121432qzimdegicdid77eg.jpg "WordPress setup")


创建用户账户后，你可以登录查看 WordPress 仪表板。


![WordPress dashboard running in a container](/data/attachment/album/202205/12/121432aoznhx7grkahae1w.jpg "WordPress dashboard running in a container")


### 下一步


你已经创建了两个容器，并在一个吊舱中运行了它们。你现在已经了解了如何在自己的服务器上运行容器及服务。如果你想迁移到云，容器非常适合你。使用像 Kubernetes 和 OpenShift 这样的工具，你可以自动化启动 [集群上的容器和吊舱](https://enterprisersproject.com/article/2020/9/pod-cluster-container-what-is-difference)。如果你正在考虑采取下一步行动，阅读 Kevin Casey 的 [3 个开始使用 Kubernetes 的方法](https://enterprisersproject.com/article/2019/11/kubernetes-3-ways-get-started)，并尝试他提到的 Minikube 教程。




---


via: <https://opensource.com/article/22/2/start-running-containers>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Whether you're interested in them as part of your job, for future job opportunities, or just out of interest in new technology, containers can seem pretty overwhelming to even an experienced systems administrator. So how do you actually get started with containers? And what's the path from containers to [Kubernetes](https://opensource.com/tags/kubernetes)? Also, why is there a path from one to the other at all? As you might expect, the best place to start is the beginning.


## 1. Understanding containers

On second thought, starting at the beginning arguably dates back to early BSD and their special chroot jails, so skip ahead to the middle instead.

Not so very long ago, the Linux kernel introduced *cgroups*, which enables you to "tag" processes with something called a *namespace*. When you group processes together into a namespace, those processes act as if nothing outside that namespace exists. It's as if you've put those processes into a sort of container. Of course, the container is virtual, and it exists inside your computer. It runs on the same kernel, RAM, and CPU that the rest of your operating system is running on, but you've contained the processes.

Pre-made containers get distributed with just what's necessary to run the application it contains. With a container engine, like [Podman](https://www.redhat.com/sysadmin/podman-guides-2020), Docker, or CRI-O, you can run a containerized application without installing it in any traditional sense. Container engines are often cross-platform, so even though containers run Linux, you can launch containers on Linux, macOS, or Windows.

More importantly, you can run more than one container of the same application when there's high demand for it.

Now that you know what a container is. The next step is to run one.

**[ Get the cheat sheet: What’s the difference between a pod, a cluster, and a container? ]**

## 2. Run a container

Before running a container, you should have a reason for running a container. You can make up a reason, but it's helpful for that reason to interest you, so you're inspired actually to use the container you run. After all, running a container but never using the application it provides only proves that you're not noticing any failures, but using the container demonstrates that it works.

I recommend WordPress as a start. It's a popular web application that's easy to use, so you can test drive the app once you've got the container running. While you can easily set up a WordPress container, there are many configuration options, which can lead you to discover more container options (like running a database container) and how containers communicate.

I use Podman, which is a friendly, convenient, and daemonless container engine. If you don't have Podman available, you can use the Docker command instead. Both are great open source container engines, and their syntax is identical (just type `docker`

instead of `podman`

). Because Podman doesn't run a daemon, it requires more setup than Docker, but the ability to run rootless daemonless containers is worth it.

If you're going with Docker, you can skip down to the [WordPress subheading](#wp). Otherwise, open a terminal to install and configure Podman:

`$ sudo dnf install podman`


Containers spawn many processes, and normally only the root user has permission to create thousands of process IDs. Add some extra process IDs to your user by creating a file called `/etc/subuid`

and defining a suitably high start UID with a suitable large number of permitted PIDs:

`seth:200000:165536`


Do the same for your group in a file called `/etc/subgid`

. In this example, my primary group is `staff`

(it may be `users`

for you, or the same as your username, depending on how you've configured your system.)

`staff:200000:165536`


Finally, confirm that your user is also permitted to manage thousands of namespaces:

```
$ sysctl --all --pattern user_namespaces
user.max_user_namespaces = 28633
```


If your user doesn't have permission to manage at least 28,000 namespaces, increase the number by creating the file `/etc/sysctl.d/userns.conf`

and enter:

`user.max_user_namespaces=28633`


Running WordPress as a container

Now, whether you're using Podman or Docker, you can pull a WordPress container from a container registry online and run it. You can do all this with a single Podman command:

```
$ podman run --name mypress \
-p 8080:80 -d wordpress
```


Give Podman a few moments to find the container, copy it from the internet, and start it up.

Start a web browser once you get a terminal prompt back and navigate to `localhost:8080`

. WordPress is running, waiting for you to set it up.

![WordPress running in a container WordPress running in a container](https://opensource.com/sites/default/files/uploads/podman-wordpress.jpg)

(Seth Kenlon, CC BY-SA 4.0)

It doesn't take long to reach your next hurdle, though. WordPress uses a database to keep track of data, so you need to provide it with a database where it can store its information.

Before continuing, stop and remove the WordPress container:

```
$ podman stop mypress
$ podman rm mypress
```


## 3. Run containers in a pod

Containers are, by design and, as their name suggests, self-contained. An application running in a container isn't supposed to interact with applications or infrastructure outside of its container. So when one container requires another container to function, one solution is to put those two containers inside a bigger container called a *pod*. A pod ensures that its containers can share important namespaces to communicate with one another.

Create a new pod, providing a name for the pod and which ports you want to be able to access:

```
$ podman pod create \
--name wp_pod \
--publish 8080:80
```


Confirm that the pod exists:

```
$ podman pod list
POD ID NAME STATUS INFRA ID # OF CONTAINERS
100e138a29bd wp_pod Created 22ace92df3ef 1
```


### Add a container to a pod

Now that you have a pod for your interdependent containers, you launch each container by specifying a pod for it to run in.

First, launch a database. You can make up your own credentials as long as you use those same credentials when connecting to the database from WordPress.

```
$ podman run --detach \
--pod wp_pod \
--restart=always \
-e MYSQL_ROOT_PASSWORD="badpassword0" \
-e MYSQL_DATABASE="wp_db" \
-e MYSQL_USER="tux" \
-e MYSQL_PASSWORD="badpassword1" \
--name=wp_db mariadb
```


Next, launch the WordPress container into the same pod:

```
$ podman run --detach \
--restart=always --pod=wp_pod \
-e WORDPRESS_DB_NAME="wp_db" \
-e WORDPRESS_DB_USER="tux" \
-e WORDPRESS_DB_PASSWORD="badpassword1" \
-e WORDPRESS_DB_HOST="127.0.0.1" \
--name mypress wordpress
```


Now launch your favorite web browser and navigate to `localhost:8080`

.

This time, the setup goes as expected. WordPress connects to the database because you've passed those environment variables while launching the container.

![WordPress setup WordPress setup](https://opensource.com/sites/default/files/uploads/wordpress-setup.jpg)

(Seth Kenlon, CC BY-SA 4.0)

After you've created a user account, you can log in to see the WordPress dashboard.

![WordPress running in a container WordPress dashboard running in a container](https://opensource.com/sites/default/files/uploads/wordpress-welcome.jpg)

(Seth Kenlon, CC BY-SA 4.0)

## Next steps

You've created two containers, and you've run them in a pod. You know enough now to run services in containers on your own server. If you want to move to the cloud, containers are, of course, well-suited for that. With tools like Kubernetes and OpenShift, you can automate the process of launching [containers and pods on a cluster](https://enterprisersproject.com/article/2020/9/pod-cluster-container-what-is-difference). If you're thinking about taking the next step, read [3 ways to get started with Kubernetes](https://enterprisersproject.com/article/2019/11/kubernetes-3-ways-get-started) by Kevin Casey, and give the Minikube tutorial he mentions a try.

## 1 Comment