TranslateProject/sources/tech/20210712 Use Docker Compose with Podman to Orchestrate Containers on Fedora.md
DarkSun 8af347ebcb 选题[tech]: 20210712 Use Docker Compose with Podman to Orchestrate Containers on Fedora
sources/tech/20210712 Use Docker Compose with Podman to Orchestrate Containers on Fedora.md
2021-07-12 17:12:19 +08:00

7.4 KiB
Raw Blame History

Use Docker Compose with Podman to Orchestrate Containers on Fedora

Photo by Jonas on Unsplash

Docker Compose is an open-source tool used by developers for orchestrating containers locally or in production. If you are new to containers, I suggest checking out the following links:

Podman, a powerful alternative to Docker CLI, has attracted a lot of developers recently. However, Podman users faced a challenge. Docker Compose was expected to work with Docker daemon only. So, Podman users had to use other alternatives to Docker Compose like using Podman Compose that runs services defined in the Compose file inside a Podman pod. (To learn more about Podman Compose, check out my article Manage Containers on Fedora with Podman Compose on Fedora Magazine.) Another method was manually running different containers of an application and then generating a Kubernetes file with

podman generate

to be later re-run with

podman play

. (To learn more about this method, check out this Moving from docker-compose to Podman pods on Red Hat.)

Podman and Docker Compose

Both of the Docker Compose alternatives mentioned previously have their limitations. At the least they need you to know a little bit more than Container and Docker basics.The good news is that Podman has added support for Docker Compose since version 3.0 so you can now run your traditional docker-compose.yml files with Podman backend. Podman does this by setting up a UNIX socket for

docker-compose

to interact with, similar to the Docker daemon. In this article I will show you how to use Docker Compose both with rootful and rootless Podman.

Required Software

Install the following packages on your system to run Docker Compose with Podman:

  • podman: Tool for managing containers
  • docker-compose: Tool for orchestrating containers
  • podman-docker: Installs a script named docker that emulates docker CLI using Podman. Also links Docker CLI man pages and podman.

Install the above packages using dnf:

sudo dnf install -y podman podman-docker docker-compose

Setting Up Podman Socket

Set up the Podman socket in order for Docker Compose to work:

sudo systemctl enable podman.socket
sudo systemctl start podman.socket
sudo systemctl status podman.socket

This sets up a Unix socket in to communicate with Docker Compose and symlinks it to /var/run/docker.sock. To test if you can communicate with the socket, run the following curl command:

sudo curl -H "Content-Type: application/json" --unix-socket /var/run/docker.sock http://localhost/_ping

If the output from the above command is OK, it means your setup was successful.

Running A Sample Project with Docker Compose

Now, you can start orchestrating your project by going to the project folder and running

sudo docker-compose up

. As an example, I will be using a docker-compose.yml from a sample WordPress project I created as a demo for this article. You can find it https://github.com/djnotes/wp-compose-sample. Clone the repository on your system and from within the directory, start docker-compose.

sudo docker-compose up

If everything goes well, you will see docker-compose bringing up the services defined in the compose YAML file. Access the new WordPress instance at http://localhost:8080 after the containers are created. To stop the project, you can press Ctrl + C in the terminal where you executed docker-compose up. To remove the containers, execute

sudo docker-compose down

Running Docker Compose with Rootless Podman

The setup shown above uses Podman in root-ful mode. Notice the sudo keyword preceding most of the commands used. Often you will not need to run your projects as root. So, having the option to run docker-compose as a regular user is pretty handy. As of version 3.2.0, Podman supports Docker-Compose with rootless Podman. The setup, however, changes as follows:

systemctl --user enable podman.socket
systemctl --user start podman.socket
systemctl --user status podman.socket
export DOCKER_HOST=///run/user/$UID/podman/podman.sock

Note that when starting the podman socket as non-root user, a user-level socket will be created at /run/user/$UID/podman/podman.sock, where $UID refers is the non-root users user ID. We need to set the DOCKER_HOST environment variable to that socket so that Docker Compose can talk to the correct socket. You can add the environment variable to your ~/.bash_profile to make it persistent across system reboots. In root-ful mode, the socket is created in /run/podman/podman.sock which is symlinked to /var/run/docker.sock (the default socket expected by the docker-compose binary). So, we didnt need to set DOCKER_HOST variable then.

Now, in rootless mode, we can simply run the command


docker-compose up


without “sudo” in the project root folder. This will bring up our WordPress site running on Docker Compose with Podman backend.

WordPress instance running with Docker Compose with Podman backend

Further Reading:


via: https://fedoramagazine.org/use-docker-compose-with-podman-to-orchestrate-containers-on-fedora/

作者:Mehdi Haghgoo 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出