Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active February 21, 2024 07:44
Show Gist options
  • Save kuanyui/d2294798f4f314f49c4b344c1a4d42c3 to your computer and use it in GitHub Desktop.
Save kuanyui/d2294798f4f314f49c4b344c1a4d42c3 to your computer and use it in GitHub Desktop.
Podman run Debian Bookworm

Beginner's note for Podman

# This is the ~/.bashrc inside the container
PS1='\[\e[1;32m\]\u@\h\[\e[m\]:\[\e[1;34m\]\W\[\e[1;33m\]\$\[\e[m\] '
alias sz="source ~/.bashrc"
alias ll='ls -al --color=auto '
alias ls='ls --color=auto '
alias sc="sudo systemctl"
alias jc="sudo journalctl"
alias jc-clear="sudo journalctl --vacuum-time=5d" # Delete journal logs which are older than 5 days
alias nvv='nvm ls'
alias nv='nvm version'
alias ..='cd ..'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'
export HISTCONTROL=ignorespace
# Zsh eqivalent:
PS1='%B%F{green}%n@%m%f:%F{blue}%~%F{yellow}%#%f%b '

Search images on registry

# --compatible means printing results as Docker format, containing more information than podman
podman search --compatible docker.io/library/debian
NAME                                           DESCRIPTION                                      STARS       OFFICIAL    AUTOMATED
docker.io/library/ubuntu                       Ubuntu is a Debian-based Linux operating sys...  16875       [OK]        
docker.io/library/debian                       Debian is a Linux distribution that's compos...  4943        [OK]        
docker.io/library/neurodebian                  NeuroDebian provides neuroscience research s...  106         [OK]        
docker.io/bitnami/debian-base-buildpack        Debian base compilation image                    2                       [OK]

Now we want to get tags of docker.io/library/debian, but podman search --list-tags cannot get full list, but it's still hard to find out which one is what we want with --limit 99999:

# list
podman search --compatible --list-tags --limit 99999 docker.io/library/debian

Therefore the best way is still searching with their web UI: https://hub.docker.com/_/debian

Download image

This step can be skipped because podman run also automatically do this.

podman image pull docker.io/library/debian:bookworm 

Start container

# Initialize an image as a container
podman run --name MY_CONTAINER_BASE --detach --tty -p 58080:8080/tcp docker.io/library/debian:bookworm

# Open the shell of a running container
podman exec --interactive --tty MY_CONTAINER_BASE /bin/bash

# Boot an inactive container
podman start MY_CONTAINER_BASE
podman run [--publish HOST_PORT:CONTAINER_PORT] image
  --publish, ‐p=[[ip:][hostPort]:]containerPort[/protocol]
  --volume, ‐v=[[SOURCE‐VOLUME|HOST‐DIR:]CONTAINER‐DIR[:OPTIONS]]

Install Packages

Elementary

apt update
apt install --no-install-recommends iputils-ping less curl wget make git graphicsmagick
# Install CA otherwise `wget` may returns error like `ERROR: The certificate of 'github.com' is not trusted.`
apt install ca-certificates

NodeJS

Don't install nodejs via apt, because we want to install node of specified version via nvm.

For safety, run this as user instead of root.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

ImageMagick

Don't install imagemagick via apt, because Debian 12/Ubuntu 23.10 are still providing imagemagick 6.9 released in 2016, which is lacks of magick commands available since 7.x. We will install official AppImage binary of imagemagick from Github https://github.com/ImageMagick/ImageMagick/releases .

apt install libfontconfig1 libharfbuzz0b libfribidi0

# Example of .bashrc
PATH=/home/user/appimages/imagemagick/usr/bin/:${PATH}

Create new user

# don't use root if possible, so create user and open a shell with user permission.
useradd --create-home --shell /bin/bash user
exit
# open a tty as newly-created `user` permission.
podman exec --interactive --tty --workdir /home/user --user user MY_CONTAINER_BASE /bin/bash
# Create a new folder in ~/ for later use
cd
mkdir MY_PROJECT
cd MY_PROJECT

Confirm the uid/gid of newly-created user

user@4737a93f8989:MY_PROJECT$ id
uid=1000(user) gid=1000(user) groups=1000(user)

Commit the current Container as Image (so that we can mount host folders via --volume)

# podman commit [options] container [image]
podman commit --author [email protected] --format oci --squash NEW_CONTAINER MY_IMAGE_2024_0221
# 亂猜的,亂加 tag ???
podman commit --author [email protected] --format oci NEW_CONTAINER MY_IMAGE:2024_0221

mount-bind host directory to container

Before doing this, you need to podman commit MY_CONTAINER_BASE MY_IMAGE to create a new image.

Host Container
Port 58001 8001
UID $UID 1000

podman run --name MY_CONTAINER --detach --tty --publish 58001:8001/tcp --userns=keep-id:uid=1000,gid=1000 --volume ${HOST_DIR_PATH}:/home/user/MY_PROJECT localhost/MY_CONTAINER_BASE:latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment