github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/docs/source/markdown/podman-generate-systemd.1.md (about) 1 % podman-generate-systemd(1) 2 3 ## NAME 4 podman\-generate\-systemd - Generate systemd unit file(s) for a container or pod 5 6 ## SYNOPSIS 7 **podman generate systemd** [*options*] *container|pod* 8 9 ## DESCRIPTION 10 **podman generate systemd** will create a systemd unit file that can be used to control a container or pod. 11 By default, the command will print the content of the unit files to stdout. 12 13 Generating unit files for a pod requires the pod to be created with an infra container (see `--infra=true`). An infra container runs across the entire lifespan of a pod and is hence required for systemd to manage the life cycle of the pod's main unit. 14 15 _Note: If you use this command with the remote client, including Mac and Windows (excluding WSL2) machines, you would still have to place the generated units on the remote system. Moreover, please make sure that the XDG_RUNTIME_DIR environment variable is set. If unset, you may set it via `export XDG_RUNTIME_DIR=/run/user/$(id -u)`._ 16 17 ## OPTIONS 18 19 #### **--after**=*dependency_name* 20 21 Add the systemd unit after (`After=`) option, that ordering dependencies between the list of dependencies and this service. This option may be specified more than once. 22 23 User-defined dependencies will be appended to the generated unit file, but any existing options such as needed or defined by default (e.g. `online.target`) will **not** be removed or overridden. 24 25 #### **--container-prefix**=*prefix* 26 27 Set the systemd unit name prefix for containers. The default is *container*. 28 29 #### **--files**, **-f** 30 31 Generate files instead of printing to stdout. The generated files are named {container,pod}-{ID,name}.service and will be placed in the current working directory. 32 33 Note: On a system with SELinux enabled, the generated files will inherit contexts from the current working directory. Depending on the SELinux setup, changes to the generated files using `restorecon`, `chcon`, or `semanage` may be required to allow systemd to access these files. Alternatively, use the `-Z` option when running `mv` or `cp`. 34 35 #### **--format**=*format* 36 37 Print the created units in specified format (json). If `--files` is specified the paths to the created files will be printed instead of the unit content. 38 39 #### **--name**, **-n** 40 41 Use the name of the container for the start, stop, and description in the unit file 42 43 #### **--new** 44 45 Using this flag will yield unit files that do not expect containers and pods to exist. Instead, new containers and pods are created based on their configuration files. The unit files are created best effort and may need to be further edited; please review the generated files carefully before using them in production. 46 47 Note that `--new` only works on containers and pods created directly via Podman (i.e., `podman [container] {create,run}` or `podman pod create`). It does not work on containers or pods created via the REST API or via `podman play kube`. 48 49 #### **--no-header** 50 51 Do not generate the header including meta data such as the Podman version and the timestamp. 52 53 #### **--pod-prefix**=*prefix* 54 55 Set the systemd unit name prefix for pods. The default is *pod*. 56 57 #### **--requires**=*dependency_name* 58 59 Set the systemd unit requires (`Requires=`) option. Similar to wants, but declares a stronger requirement dependency. 60 61 #### **--restart-policy**=*policy* 62 63 Set the systemd restart policy. The restart-policy must be one of: "no", "on-success", "on-failure", "on-abnormal", 64 "on-watchdog", "on-abort", or "always". The default policy is *on-failure*. 65 66 #### **--restart-sec**=*time* 67 68 Set the systemd service restartsec value. Configures the time to sleep before restarting a service (as configured with restart-policy). 69 Takes a value in seconds. 70 71 #### **--separator**=*separator* 72 73 Set the systemd unit name separator between the name/id of a container/pod and the prefix. The default is *-*. 74 75 #### **--start-timeout** =*value* 76 77 Override the default start timeout for the container with the given value in seconds. 78 79 #### **--stop-timeout** =*value* 80 81 Override the default stop timeout for the container with the given value in seconds. 82 83 #### **--template** 84 85 Add template specifiers to run multiple services from the systemd unit file. 86 87 Note that if `--new` was not set to true, it is set to true by default. However, if `--new` is set to `false` explicitly the command will fail. 88 89 #### **--wants**=*dependency_name* 90 91 Add the systemd unit wants (`Wants=`) option, that this service is (weak) dependent on. This option may be specified more than once. This option does not influence the order in which services are started or stopped. 92 93 User-defined dependencies will be appended to the generated unit file, but any existing options such as needed or defined by default (e.g. `online.target`) will **not** be removed or overridden. 94 95 ## EXAMPLES 96 97 ### Generate and print a systemd unit file for a container 98 99 Generate a systemd unit file for a container running nginx with an *always* restart policy and 1-second timeout to stdout. Note that the **RequiresMountsFor** option in the **Unit** section ensures that the container storage for both the GraphRoot and the RunRoot are mounted prior to starting the service. For systems with container storage on disks like iSCSI or other remote block protocols, this ensures that Podman is not executed prior to any necessary storage operations coming online. 100 101 ``` 102 $ podman create --name nginx nginx:latest 103 $ podman generate systemd --restart-policy=always -t 1 nginx 104 # container-de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6.service 105 # autogenerated by Podman 1.8.0 106 # Wed Mar 09 09:46:45 CEST 2020 107 108 [Unit] 109 Description=Podman container-de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6.service 110 Documentation=man:podman-generate-systemd(1) 111 Wants=network-online.target 112 After=network-online.target 113 RequiresMountsFor=/var/run/container/storage 114 115 [Service] 116 Restart=always 117 ExecStart=/usr/bin/podman start de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6 118 ExecStop=/usr/bin/podman stop -t 1 de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6 119 KillMode=none 120 Type=forking 121 PIDFile=/run/user/1000/overlay-containers/de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6/userdata/conmon.pid 122 123 [Install] 124 WantedBy=default.target 125 ``` 126 127 ### Generate systemd unit file for a container with `--new` flag 128 129 The `--new` flag generates systemd unit files that create and remove containers at service start and stop commands (see ExecStartPre and ExecStopPost service actions). Such unit files are not tied to a single machine and can easily be shared and used on other machines. 130 131 ``` 132 $ sudo podman generate systemd --new --files --name bb310a0780ae 133 # container-busy_moser.service 134 # autogenerated by Podman 1.8.3 135 # Fri Apr 3 09:40:47 EDT 2020 136 137 [Unit] 138 Description=Podman container-busy_moser.service 139 Documentation=man:podman-generate-systemd(1) 140 Wants=network-online.target 141 After=network-online.target 142 RequiresMountsFor=/var/run/container/storage 143 144 [Service] 145 Environment=PODMAN_SYSTEMD_UNIT=%n 146 Restart=on-failure 147 ExecStartPre=/bin/rm -f %t/%n-pid %t/%n-cid 148 ExecStart=/usr/local/bin/podman run 149 --conmon-pidfile %t/%n-pid 150 --cidfile %t/%n-cid 151 --cgroups=no-conmon 152 -d 153 -dit alpine 154 ExecStop=/usr/local/bin/podman stop --ignore --cidfile %t/%n-cid -t 10 155 ExecStopPost=/usr/local/bin/podman rm --ignore -f --cidfile %t/%n-cid 156 PIDFile=%t/%n-pid 157 KillMode=none 158 Type=forking 159 160 [Install] 161 WantedBy=default.target 162 ``` 163 164 ### Generate systemd unit files for a pod with two simple alpine containers 165 166 Note `systemctl` should only be used on the pod unit and one should not start or stop containers individually via `systemctl`, as they are managed by the pod service along with the internal infra-container. 167 168 You can still use `systemctl status` or `journalctl` to examine container or pod unit files. 169 ``` 170 $ podman pod create --name systemd-pod 171 $ podman create --pod systemd-pod alpine top 172 $ podman create --pod systemd-pod alpine top 173 $ podman generate systemd --files --name systemd-pod 174 /home/user/pod-systemd-pod.service 175 /home/user/container-amazing_chandrasekhar.service 176 /home/user/container-jolly_shtern.service 177 $ cat pod-systemd-pod.service 178 # pod-systemd-pod.service 179 # autogenerated by Podman 1.8.0 180 # Wed Mar 09 09:52:37 CEST 2020 181 182 [Unit] 183 Description=Podman pod-systemd-pod.service 184 Documentation=man:podman-generate-systemd(1) 185 Requires=container-amazing_chandrasekhar.service container-jolly_shtern.service 186 Before=container-amazing_chandrasekhar.service container-jolly_shtern.service 187 Wants=network-online.target 188 After=network-online.target 189 RequiresMountsFor=/var/run/container/storage 190 191 [Service] 192 Restart=on-failure 193 ExecStart=/usr/bin/podman start 77a818221650-infra 194 ExecStop=/usr/bin/podman stop -t 10 77a818221650-infra 195 KillMode=none 196 Type=forking 197 PIDFile=/run/user/1000/overlay-containers/ccfd5c71a088768774ca7bd05888d55cc287698dde06f475c8b02f696a25adcd/userdata/conmon.pid 198 199 [Install] 200 WantedBy=default.target 201 ``` 202 203 ### Installation of generated systemd unit files. 204 205 Podman-generated unit files include an `[Install]` section, which carries installation information for the unit. It is used by the enable and disable commands of systemctl(1) during installation. 206 207 Once you have generated the systemd unit file, you can copy the generated systemd file to ```/etc/systemd/system``` for installing as a root user and to ```$HOME/.config/systemd/user``` for installing it as a non-root user. Enable the copied unit file or files using `systemctl enable`. 208 209 Note: Copying unit files to ```/etc/systemd/system``` and enabling it marks the unit file to be automatically started at boot. And similarly, copying a unit file to ```$HOME/.config/systemd/user``` and enabling it marks the unit file to be automatically started on user login. 210 211 212 ``` 213 # Generated systemd files. 214 $ podman pod create --name systemd-pod 215 $ podman create --pod systemd-pod alpine top 216 $ podman generate systemd --files --name systemd-pod 217 218 # Copy all the generated files. 219 220 $ sudo cp pod-systemd-pod.service container-great_payne.service /etc/systemd/system 221 $ systemctl enable pod-systemd-pod.service 222 Created symlink /etc/systemd/system/multi-user.target.wants/pod-systemd-pod.service → /etc/systemd/system/pod-systemd-pod.service. 223 Created symlink /etc/systemd/system/default.target.wants/pod-systemd-pod.service → /etc/systemd/system/pod-systemd-pod.service. 224 $ systemctl is-enabled pod-systemd-pod.service 225 enabled 226 ``` 227 To run the user services placed in `$HOME/.config/systemd/user` on first login of that user, enable the service with --user flag. 228 229 ``` 230 $ systemctl --user enable <.service> 231 ``` 232 The systemd user instance is killed after the last session for the user is closed. The systemd user instance can be started at boot and kept running even after the user logs out by enabling `lingering` using 233 234 ``` 235 $ loginctl enable-linger <username> 236 ``` 237 ### Use `systemctl` to perform operations on generated installed unit files. 238 239 Create and enable systemd unit files for a pod using the above examples as reference and use `systemctl` to perform operations. 240 241 Since systemctl defaults to using the root user, all the changes using the systemctl can be seen by appending sudo to the podman cli commands. To perform `systemctl` actions as a non-root user use the `--user` flag when interacting with `systemctl`. 242 243 Note: If the previously created containers or pods are using shared resources, such as ports, make sure to remove them before starting the generated systemd units. 244 245 ``` 246 $ systemctl --user start pod-systemd-pod.service 247 $ podman pod ps 248 POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 249 0815c7b8e7f5 systemd-pod Running 29 minutes ago 2 6c5d116f4bbe 250 $ sudo podman ps # 0 Number of pods on root. 251 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 252 $ systemctl stop pod-systemd-pod.service 253 $ podman pod ps 254 POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 255 272d2813c798 systemd-pod Exited 29 minutes ago 2 6c5d116f4bbe 256 ``` 257 258 Create a simple alpine container and generate the systemd unit file with `--new` flag. 259 Enable the service and control operations using the systemctl commands. 260 261 Note: When starting the container using `systemctl start` rather than altering the already running container it spins up a "new" container with similar configuration. 262 263 ``` 264 # Enable the service. 265 266 $ sudo podman ps -a 267 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 268 bb310a0780ae docker.io/library/alpine:latest /bin/sh 2 minutes ago Created busy_moser 269 $ sudo systemctl start container-busy_moser.service 270 $ sudo podman ps -a 271 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 272 772df2f8cf3b docker.io/library/alpine:latest /bin/sh 1 second ago Up 1 second ago distracted_albattani 273 bb310a0780ae docker.io/library/alpine:latest /bin/sh 3 minutes ago Created busy_moser 274 ``` 275 ## SEE ALSO 276 **[podman(1)](podman.1.md)**, **[podman-container(1)](podman-container.1.md)**, **systemctl(1)**, **systemd.unit(5)**, **systemd.service(5)**, **[conmon(8)](https://github.com/containers/conmon/blob/main/docs/conmon.8.md)** 277 278 ## HISTORY 279 April 2020, Updated details and added use case to use generated .service files as root and non-root, by Sujil Shah (sushah at redhat dot com) 280 281 August 2019, Updated with pod support by Valentin Rothberg (rothberg at redhat dot com) 282 283 April 2019, Originally compiled by Brent Baude (bbaude at redhat dot com)