Skip to content

Instantly share code, notes, and snippets.

@juanje
Created June 10, 2020 14:53
Show Gist options
  • Save juanje/de5dd74f2b20190bfe8b0d089e837e4a to your computer and use it in GitHub Desktop.
Save juanje/de5dd74f2b20190bfe8b0d089e837e4a to your computer and use it in GitHub Desktop.
Run Redis with Podman. As a Systemd service and as a Pod.
# This is a simple example of how to run a basic service inside a container with Podman
# Podman
## Pull the Docker image
podman pull docker.io/redis
## Run the container as you would do with Docker
podman run -d --name redis_server -p 6379:6379 redis
# But Podman facilitate some extra ways:
# Systemd
## Generate the unit for Systemd and put it in the user config space
podman generate systemd redis_server -f redis-container.service
mkdir -p ~/.config/systemd/user
cp redis-container.service ~/.config/systemd/user/redis-container.service
## Start the Redis container as an user's service. I don't need 'sudo'.
systemctl --user start redis-container.service
## Check if Redis is running
systemctl --user status redis-container.service
## Check if I can access to Redis. I need nc (netcat) installed.
(printf "PING\r\n";) | nc localhost 6379
### It should respond: '+PONG'
## Stop the service
systemctl --user stop redis-container.service
## NOTE: If you try to run this service as normal system service won't work,
## because the container was created by the user and only the user sees it.
## Podman has no daemon and no centraliced containers.
## My guess is that you should create a user for the service, create the container
## with that user and tell systemd to run it at its userspace.
# Pod
## Generate a pod definition in K8s format
podman generate kube redis_server -f redis_server.yml
## Make sure the container doesn't exist anymore
podman rm -f redis_server
## Run the new pod with the container with Redis inside.
podman play kube redis_server.yml
## Check if I can access to Redis. I need nc (netcat) installed.
(printf "PING\r\n";) | nc localhost 6379
### It should respond: '+PONG'
# NOTE: The other files are the ones generated with the previous commands.
# The 'redis-container.service' one won't be the same at your system, because the hash
# for the container will be different.
# container-b6c3519d0335c5290e363698016f8d87b9ed868d6e3bd7ffbfa057b5accb9e7b.service
# autogenerated by Podman 1.9.3
# Wed Jun 10 00:53:55 WEST 2020
[Unit]
Description=Podman container-b6c3519d0335c5290e363698016f8d87b9ed868d6e3bd7ffbfa057b5accb9e7b.service
Documentation=man:podman-generate-systemd(1)
Wants=network.target
After=network-online.target
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
ExecStart=/usr/bin/podman start b6c3519d0335c5290e363698016f8d87b9ed868d6e3bd7ffbfa057b5accb9e7b
ExecStop=/usr/bin/podman stop -t 10 b6c3519d0335c5290e363698016f8d87b9ed868d6e3bd7ffbfa057b5accb9e7b
PIDFile=/run/user/1000/containers/overlay-containers/b6c3519d0335c5290e363698016f8d87b9ed868d6e3bd7ffbfa057b5accb9e7b/userdata/conmon.pid
KillMode=none
Type=forking
[Install]
WantedBy=multi-user.target default.target
# Generation of Kubernetes YAML is still under development!
#
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-1.9.3
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2020-06-10T14:22:20Z"
labels:
app: redisserver
name: redisserver
spec:
containers:
- command:
- redis-server
env:
- name: PATH
value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: TERM
value: xterm
- name: HOSTNAME
- name: container
value: podman
- name: REDIS_VERSION
value: 6.0.4
- name: REDIS_DOWNLOAD_URL
value: http://download.redis.io/releases/redis-6.0.4.tar.gz
- name: REDIS_DOWNLOAD_SHA
value: 3337005a1e0c3aa293c87c313467ea8ac11984921fab08807998ba765c9943de
- name: GOSU_VERSION
value: "1.12"
image: docker.io/library/redis:latest
name: redisserver
ports:
- containerPort: 6379
hostPort: 6379
protocol: TCP
resources: {}
securityContext:
allowPrivilegeEscalation: true
capabilities: {}
privileged: false
readOnlyRootFilesystem: false
seLinuxOptions: {}
workingDir: /data
status: {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment