Apple Container Walkthrough/Example

4 months ago 7
  • Your have Apple Silicon Mac
  • Your macOS is alteast on version 15 and updated.

Install container

Github link for .pkg file : https://github.com/apple/container/releases

For my machine I’m using 0.1.0 the first version available as of when this article was written : container-0.1.0-installer-signed.pkg

Double-click the .pkg file and follow the installation prompts.

From macOS terminal

A. Start the Service

$ container system start

Verifying apiserver is running...
Installing base container filesystem...
No default kernel configured.
Install the recommended default kernel from [https://github.com/kata-containers/kata-containers/releases/download/3.17.0/kata-static-3.17.0-arm64.tar.xz]? [Y/n]: Y
Installing kernel...

B. Check the list of containers
It should return no results

$ container ls -a

ID IMAGE OS ARCH STATE ADDR

A. Create directory

mkdir postgresql
cd postgresql/

B. Create a Dockerfile

vim Dockerfile
FROM arm64v8/postgres:15.6
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=mypasswd1234
ENV POSTGRES_DB=myappdb
ENV POSTGRES_LISTEN_ADDRESS=0.0.0.0
CMD ["postgres"]

C. Build the Image

container build --tag my-apple-postgresql --file Dockerfile .

D. List Your Image

container image list

NAME TAG DIGEST
arm64v8/postgres 15.6 0526cc72d34b102c7dc6b57e...
my-apple-postgresql latest c6416c9a65895c2c5b14a110...

E. Run Your Container

# Launch your container
container run -d --name my-apple-postgresql my-apple-postgresql

# Check if the container is running
container ls

ID IMAGE OS ARCH STATE ADDR
my-apple-postgresql my-apple-postgresql:latest linux arm64 running 192.168.64.3
buildkit ghcr.io/apple/container-builder-shim/builder:0.1.0 linux arm64 running 192.168.64.2

-d: Runs the container in the background

F. Interact with your Container

container exec my-apple-postgresql df -h

Filesystem Size Used Avail Use% Mounted on
/dev/vdb 504G 490M 504G 1% /
none 496M 0 496M 0% /dev
tmpfs 64M 1.1M 63M 2% /dev/shm

G. Connect to Shell inside your PostgreSQL container

container exec -it my-apple-postgresql /bin/bash

### >>Shell Starts here
root@my-apple-postgresql:/# echo $SHELL
/bin/bash

root@my-apple-postgresql:/# psql -h 192.168.64.3 -p 5432 -U postgres -d myappdb
Password for user postgres:
psql (15.6 (Debian 15.6-1.pgdg120+2))
Type "help" for help.

myappdb=# SELECT version();
---------------------------------------------------------------------------------------------------------------------------
PostgreSQL 15.6 (Debian 15.6-1.pgdg120+2) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)

myappdb=# \q
root@my-apple-postgresql:/# exit
exit
### >>Shell Ends here

-it Opens interactive terminal session

H. Connect DB Client from your Local Machine to Container

ImportantNote:To connect to the Postgres running inside the container from the host machine you can use socat to do port forwarding. Currently container does not allow -plocal-to-remote port binding.

# Install socat
brew install socat

# Forward the postgresql port traffic from the gateway to the host
socat TCP-LISTEN:5432,fork,bind=127.0.0.1 TCP:192.168.64.3:5432

192.168.64.3 -- the IP of your container when you run 'container ls'
127.0.0.1 -- localhost
TCP-LISTEN:5432 -- Listening on postgres port 5432 on local machine
5432 -- Forward to container port 5432

# From another terminal session check for port open on local machine
netstat -an | grep 5432
tcp4 0 0 127.0.0.1.5432 *.* LISTEN

telnet localhost 5432
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

I can now connect from a Database client tool like DBeaver running on my machine into the Postgres container.

Log In to your Docker Registry

container registry login docker.io

Provide registry username docker.io: shadabshaukat
Provide registry password:
Login succeeded

Tag your Image for Publishing

container images tag my-apple-postgresql docker.io/shadabshaukat/my-apple-postgresql:latest

## Image my-apple-postgresql tagged as docker.io/shadabshaukat/my-apple-postgresql:latest

Push the Image:

container images push docker.io/shadabshaukat/my-apple-postgresql:latest

It will prompt you to allow registry and ask for your admin password. Enter password and proceed

⠴ Pushing image docker.io/shadabshaukat/my-apple-postgresql:latest 29% (14 of 16 blobs, 42.2/142.6 MB) [1m 1s]

Once the push is completed, your image is available for others to pull and run, on any OCI-compliant platform including Docker, Podman or Kubernetes.

# List of all container commands
container --help

# Control amount of memory and cpu's to allocate to container
container builder start --cpus 4--memory 8g

# Build images for both Apple Silicon (arm64) and Intel (amd64)
container build --arch arm64 --arch amd64 --tag my-postgres-arch-image .

#Inspect the container (Gives JSON output of the container configuration)
container inspect --debug my-apple-postgresql

# Set alias of docker
alias docker='container'

docker ls

ID IMAGE OS ARCH STATE ADDR
my-apple-postgresql my-apple-postgresql:latest linux arm64 running 192.168.64.3
buildkit ghcr.io/apple/container-builder-shim/builder:0.1.0 linux arm64 running 192.168.64.2

# Check container log
container logs -f my-apple-postgresql

#Stop Container
container stop my-apple-postgresql

#List Images
container images ls

Why This Matters ?
With this move, Apple might just be nudging Docker and Podman off the macOS container developer throne. And because it plays nicely with any OCI-compliant platform, your containers are still portable — just faster, leaner, and more “Mac-like” than ever.

Apple just handed you a sleek new tool. Time to kick the tires.

👉 If you’re a macOS power user, Swift dev, or container nerd — this one’s worth a serious look.

Read Entire Article