Skip to content

Instantly share code, notes, and snippets.

@luizanao
Forked from kaaquist/podman_macos.md
Last active January 26, 2022 13:43
Show Gist options
  • Save luizanao/1540093c9b5981d10fa1738c3182dedd to your computer and use it in GitHub Desktop.
Save luizanao/1540093c9b5981d10fa1738c3182dedd to your computer and use it in GitHub Desktop.
Podman with docker-compose on MacOS.

Podman with docker-compose on MacOS.

Podman an alternative to Docker Desktop on MacOS

This is a fork of @kaaquist original gist. Thanks for sharing!

Getting podman installed and started is super easy.
Just use brew to install it.

> brew install podman

Now since podman uses a VM just like the Docker Client on MacOS we need to initialize that and start it.

I'd recommend creating the VM with arealistic set of resources, for me it was:

> podman machine init --cpus 4 --disk-size 50 --memory 4096
> podman machine start

Now we are set to go.

If you want you can create a symlink so podman can be executed with "docker" command.

> ln -s /usr/local/bin/podman /usr/local/bin/docker

Now most of the commands in podman are the same so try podman images and you will get a list of images.
Else the podman --help command list all the help you need.

To get docker-compose without the docker client for mac. You can install it using the brew command.

> brew install docker-compose

When that is done you now should have the ability to use docker-compose with podman.

On MacOS the podman project does not expose the podman.socket which is similar to docker.socket, by default. So to get docker-compose working one needs to expose the socket.

To get the socket running run the following commands.
First we need to find the port it is exposed on in the VM.

> podman system connection ls

This command will show all the users/connections you have available - defaults: core or user.

You can switch between them by:

> podman system connection default podman-machine-default-root
# or podman-machine-default if you feel like

I choose root connection since core user has some limitations that bother me, such don't allow me to expose lower ports (80, 443, etc)

Then we need to take that port and create a forward ssh connection to that.

> ssh -fnNT -L/tmp/podman.sock:/run/user/1000/podman/podman.sock -i ~/.ssh/podman-machine-default ssh://root@localhost:<port to socket> -o StreamLocalBindUnlink=yes
> export DOCKER_HOST='unix:///tmp/podman.sock'

Second, we expose the DOCKER_HOST env variable that is used by docker-compose. Be aware that if the connection is disconnected one needs to delete/overwrite the /tmp/podman.socket to run the forward command got o Pro-tip:

Pro-tip: Instead of keep repeating this process every time you close your terminal session / restart computer, you can let ~/.bashrc do that for you.

Copy this to ~/.bashrc to auto-load podman ssh forwarding and env var everytime you open a new terminal session.

# Podman containers
export DOCKER_HOST='unix:///tmp/podman.sock'
warmup_podman(){
    is_ssh_tunel_setup=$(ps aux | grep -i "ssh -fnNT -L/tmp/podman.sock:/run/podman/podman.sock" | grep -v grep | wc -l)
    if [[ $is_ssh_tunel_setup -eq 0 ]]; then
        port=$(podman system connection ls | grep -i root |  awk '{print $3}' | sed -n 's/^.*localhost:\([^/]*\).*/\1/p')
        ssh -fnNT -L/tmp/podman.sock:/run/podman/podman.sock -i ~/.ssh/podman-machine-default ssh://root@localhost:${port} -o StreamLocalBindUnlink=yes
    fi
}
warmup_podman

Test it

To make sure podman is runnning fine:

podman run -it ubuntu:latest

in a new terminal session:

podman ps
docker ps

You should see similar outputs since docker binary is using podman banckend. docker-compose should also work as normal, using podman backend.

Knowing issue:

For my particular docker-compose version (docker-compose version 1.29.2, build 5becea4c) I had issues building containers that we solved by:

# docker-compose issue https://github.com/containers/podman/issues/11326
export COMPOSE_DOCKER_CLI_BUILD=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment