github.com/containers/podman/v5@v5.1.0-rc1/docs/source/markdown/podman-generate-systemd.1.md (about)

     1  % podman-generate-systemd 1
     2  
     3  ## NAME
     4  podman\-generate\-systemd - [DEPRECATED] Generate systemd unit file(s) for a container or pod
     5  
     6  ## SYNOPSIS
     7  **podman generate systemd** [*options*] *container|pod*
     8  
     9  ## DESCRIPTION
    10  DEPRECATED:
    11  Note: **podman generate systemd** is deprecated. We recommend using [Quadlet](podman-systemd.unit.5.md)
    12  files when running Podman containers or pods under systemd.  There are no plans to remove the command.
    13  It will receive urgent bug fixes but no new features.
    14  
    15  **podman generate systemd** creates a systemd unit file that can be used to control a container or pod.
    16  By default, the command prints the content of the unit files to stdout.
    17  
    18  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.
    19  
    20  - Note: When using this command with the remote client, including Mac and Windows (excluding WSL2) machines, place the generated units on the remote system.  Moreover, make sure that the `XDG_RUNTIME_DIR` environment variable is set.  If unset, set it via `export XDG_RUNTIME_DIR=/run/user/$(id -u)`._
    21  
    22  - Note: The generated `podman run` command contains an `--sdnotify` option with the value taken from the container.
    23  If the container does not have any explicitly set value or the value is set to __ignore__, the value __conmon__ is used.
    24  The reason for overriding the default value __container__ is that almost no container workloads send notify messages.
    25  Systemd waits for a ready message that never comes, if the value __container__ is used for a container
    26  that does not send notify messages. The use of the default value might have been unintentional by the user,
    27  therefore the overridden default value._
    28  
    29  ### Kubernetes Integration
    30  
    31  A Kubernetes YAML can be executed in systemd via the `podman-kube@.service` systemd template.  The template's argument is the path to the YAML file.  Given a `workload.yaml` file in the home directory, it can be executed as follows:
    32  
    33  ```
    34  $ escaped=$(systemd-escape ~/workload.yaml)
    35  $ systemctl --user start podman-kube@$escaped.service
    36  $ systemctl --user is-active podman-kube@$escaped.service
    37  active
    38  ```
    39  
    40  ## OPTIONS
    41  
    42  #### **--after**=*dependency_name*
    43  
    44  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.
    45  
    46  User-defined dependencies are appended to the generated unit file, but any existing options needed or defined by default (e.g., `online.target`) are **not** removed or overridden.
    47  
    48  #### **--container-prefix**=*prefix*
    49  
    50  Set the systemd unit name prefix for containers. The default is *container*.
    51  
    52  #### **--env**, **-e**=*env*
    53  
    54  Set environment variables to the systemd unit files.
    55  
    56  If an environment variable is specified without a value, Podman checks the host environment for a value and sets the variable only if it is set on the host. As a special case, if an environment variable ending in __*__ is specified without a value, Podman searches the host environment for variables starting with the prefix and adds those variables to the systemd unit files.
    57  
    58  #### **--files**, **-f**
    59  
    60  Generate files instead of printing to stdout.  The generated files are named {container,pod}-{ID,name}.service and are placed in the current working directory.
    61  
    62  Note: On a system with SELinux enabled, the generated files inherits 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`.
    63  
    64  #### **--format**=*format*
    65  
    66  Print the created units in the specified format (json). If `--files` is specified, the paths to the created files are printed instead of the unit content.
    67  
    68  #### **--name**, **-n**
    69  
    70  Use the name of the container for the start, stop, and description in the unit file
    71  
    72  #### **--new**
    73  
    74  This option yields 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 further editing; please review the generated files carefully before using them in production.
    75  
    76  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 kube play`.  For `podman kube play`, use the `podman-kube@.service` systemd template instead.
    77  
    78  #### **--no-header**
    79  
    80  Do not generate the header including meta data such as the Podman version and the timestamp.
    81  
    82  #### **--pod-prefix**=*prefix*
    83  
    84  Set the systemd unit name prefix for pods. The default is *pod*.
    85  
    86  #### **--requires**=*dependency_name*
    87  
    88  Set the systemd unit requires (`Requires=`) option. Similar to wants, but declares a stronger requirement dependency.
    89  
    90  #### **--restart-policy**=*policy*
    91  
    92  Set the systemd restart policy.  The restart-policy must be one of: "no", "on-success", "on-failure", "on-abnormal",
    93  "on-watchdog", "on-abort", or "always".  The default policy is *on-failure* unless the container was created with a custom restart policy.
    94  
    95  Note that generating a unit without `--new` on a container with a custom restart policy can lead to issues on shutdown; systemd attempts to stop the unit while Podman tries to restart it.  Creating the container without `--restart` and using the `--restart-policy` option when generating the unit file is recommended.
    96  
    97  #### **--restart-sec**=*time*
    98  
    99  Set the systemd service restartsec value. Configures the time to sleep before restarting a service (as configured with restart-policy).
   100  Takes a value in seconds.
   101  
   102  #### **--separator**=*separator*
   103  
   104  Set the systemd unit name separator between the name/id of a container/pod and the prefix. The default is *-*.
   105  
   106  #### **--start-timeout**=*value*
   107  
   108  Override the default start timeout for the container with the given value in seconds.
   109  
   110  #### **--stop-timeout**=*value*
   111  
   112  Override the default stop timeout for the container with the given value in seconds.
   113  
   114  #### **--template**
   115  
   116  Add template specifiers to run multiple services from the systemd unit file.
   117  
   118  Note that if `--new` was not set to true, it is set to true by default. However, the command fails if `--new` is set to `false` explicitly.
   119  
   120  #### **--wants**=*dependency_name*
   121  
   122  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.
   123  
   124  User-defined dependencies are appended to the generated unit file, but any existing options needed or defined by default (e.g., `online.target`) are **not** removed or overridden.
   125  
   126  ## EXAMPLES
   127  
   128  ### Generate and print a systemd unit file for a container
   129  
   130  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.
   131  
   132  ```
   133  $ podman create --name nginx nginx:latest
   134  $ podman generate systemd --restart-policy=always -t 1 nginx
   135  # container-de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6.service
   136  # autogenerated by Podman 1.8.0
   137  # Wed Mar 09 09:46:45 CEST 2020
   138  
   139  [Unit]
   140  Description=Podman container-de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6.service
   141  Documentation=man:podman-generate-systemd(1)
   142  Wants=network-online.target
   143  After=network-online.target
   144  RequiresMountsFor=/var/run/container/storage
   145  
   146  [Service]
   147  Restart=always
   148  ExecStart=/usr/bin/podman start de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6
   149  ExecStop=/usr/bin/podman stop \
   150          -t 1 de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6
   151  KillMode=none
   152  Type=forking
   153  PIDFile=/run/user/1000/overlay-containers/de1e3223b1b888bc02d0962dd6cb5855eb00734061013ffdd3479d225abacdc6/userdata/conmon.pid
   154  
   155  [Install]
   156  WantedBy=default.target
   157  ```
   158  
   159  ### Generate systemd unit file for a container with `--new` flag
   160  
   161   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.
   162  
   163  ```
   164  $ sudo podman generate systemd --new --files --name bb310a0780ae
   165  # container-busy_moser.service
   166  # autogenerated by Podman 1.8.3
   167  # Fri Apr  3 09:40:47 EDT 2020
   168  
   169  [Unit]
   170  Description=Podman container-busy_moser.service
   171  Documentation=man:podman-generate-systemd(1)
   172  Wants=network-online.target
   173  After=network-online.target
   174  RequiresMountsFor=/var/run/container/storage
   175  
   176  [Service]
   177  Environment=PODMAN_SYSTEMD_UNIT=%n
   178  Restart=on-failure
   179  ExecStartPre=/bin/rm -f %t/%n-pid %t/%n-cid
   180  ExecStart=/usr/local/bin/podman run \
   181          --conmon-pidfile %t/%n-pid \
   182  	--cidfile %t/%n-cid \
   183  	--cgroups=no-conmon \
   184  	-d \
   185  	-dit alpine
   186  ExecStop=/usr/local/bin/podman stop \
   187          --ignore \
   188          --cidfile %t/%n-cid -t 10
   189  ExecStopPost=/usr/local/bin/podman rm \
   190          --ignore \
   191          -f \
   192  	--cidfile %t/%n-cid
   193  PIDFile=%t/%n-pid
   194  KillMode=none
   195  Type=forking
   196  
   197  [Install]
   198  WantedBy=default.target
   199  ```
   200  
   201  ### Generate systemd unit files for a pod with two simple alpine containers
   202  
   203  Note `systemctl` must only be used on the pod unit and not used to start or stop containers individually. The containers are managed by the pod service along with the internal infra-container.
   204  
   205  Use `systemctl status` or `journalctl` to examine container or pod unit files.
   206  ```
   207  $ podman pod create --name systemd-pod
   208  $ podman create --pod systemd-pod alpine top
   209  $ podman create --pod systemd-pod alpine top
   210  $ podman generate systemd --files --name systemd-pod
   211  /home/user/pod-systemd-pod.service
   212  /home/user/container-amazing_chandrasekhar.service
   213  /home/user/container-jolly_shtern.service
   214  $ cat pod-systemd-pod.service
   215  # pod-systemd-pod.service
   216  # autogenerated by Podman 1.8.0
   217  # Wed Mar 09 09:52:37 CEST 2020
   218  
   219  [Unit]
   220  Description=Podman pod-systemd-pod.service
   221  Documentation=man:podman-generate-systemd(1)
   222  Requires=container-amazing_chandrasekhar.service container-jolly_shtern.service
   223  Before=container-amazing_chandrasekhar.service container-jolly_shtern.service
   224  Wants=network-online.target
   225  After=network-online.target
   226  RequiresMountsFor=/var/run/container/storage
   227  
   228  [Service]
   229  Restart=on-failure
   230  ExecStart=/usr/bin/podman start 77a818221650-infra
   231  ExecStop=/usr/bin/podman stop \
   232          -t 10 77a818221650-infra
   233  KillMode=none
   234  Type=forking
   235  PIDFile=/run/user/1000/overlay-containers/ccfd5c71a088768774ca7bd05888d55cc287698dde06f475c8b02f696a25adcd/userdata/conmon.pid
   236  
   237  [Install]
   238  WantedBy=default.target
   239  ```
   240  
   241  ### Installation of generated systemd unit files.
   242  
   243  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.
   244  
   245  Once the systemd unit file is generated, install it to */etc/systemd/system* to be run by the root user or to *$HOME/.config/systemd/user* for installing it as a non-root user. Enable the copied unit file or files using `systemctl enable`.
   246  
   247  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.
   248  
   249  
   250  ```
   251  # Generated systemd files.
   252  $ podman pod create --name systemd-pod
   253  $ podman create --pod systemd-pod alpine top
   254  $ podman generate systemd --files --name systemd-pod
   255  
   256  # Copy all the generated files.
   257  
   258  $ sudo cp pod-systemd-pod.service container-great_payne.service /etc/systemd/system
   259  $ systemctl enable pod-systemd-pod.service
   260  Created symlink /etc/systemd/system/multi-user.target.wants/pod-systemd-pod.service → /etc/systemd/system/pod-systemd-pod.service.
   261  Created symlink /etc/systemd/system/default.target.wants/pod-systemd-pod.service → /etc/systemd/system/pod-systemd-pod.service.
   262  $ systemctl is-enabled pod-systemd-pod.service
   263  enabled
   264  ```
   265  To run the user services placed in `$HOME/.config/systemd/user` on first login of that user, enable the service with --user flag.
   266  
   267  ```
   268  $ systemctl --user enable <.service>
   269  ```
   270  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
   271  
   272  ```
   273  $ loginctl enable-linger <username>
   274  ```
   275  ### Use `systemctl` to perform operations on generated installed unit files.
   276  
   277  Create and enable systemd unit files for a pod using the above examples as reference and use `systemctl` to perform operations.
   278  
   279  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`.
   280  
   281  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.
   282  
   283  ```
   284  $ systemctl --user start pod-systemd-pod.service
   285  $ podman pod ps
   286  POD ID         NAME          STATUS    CREATED          # OF CONTAINERS   INFRA ID
   287  0815c7b8e7f5   systemd-pod   Running   29 minutes ago   2                 6c5d116f4bbe
   288  $ sudo podman ps # 0 Number of pods on root.
   289  CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES
   290  $ systemctl stop pod-systemd-pod.service
   291  $ podman pod ps
   292  POD ID         NAME          STATUS   CREATED          # OF CONTAINERS   INFRA ID
   293  272d2813c798   systemd-pod   Exited   29 minutes ago   2                 6c5d116f4bbe
   294  ```
   295  
   296  Create a simple alpine container and generate the systemd unit file with `--new` flag.
   297  Enable the service and control operations using the systemctl commands.
   298  
   299  Note: When starting the container using `systemctl start` rather than altering the already running container it spins up a "new" container with similar configuration.
   300  
   301  ```
   302  # Enable the service.
   303  
   304  $ sudo podman ps -a
   305  CONTAINER ID  IMAGE                            COMMAND  CREATED        STATUS     PORTS  NAMES
   306  bb310a0780ae  docker.io/library/alpine:latest  /bin/sh  2 minutes ago  Created           busy_moser
   307  $ sudo systemctl start container-busy_moser.service
   308  $ sudo podman ps -a
   309  CONTAINER ID  IMAGE                            COMMAND  CREATED        STATUS        PORTS      NAMES
   310  772df2f8cf3b  docker.io/library/alpine:latest  /bin/sh  1 second ago   Up 1 second              distracted_albattani
   311  bb310a0780ae  docker.io/library/alpine:latest  /bin/sh  3 minutes ago  Created                  busy_moser
   312  ```
   313  ## SEE ALSO
   314  **[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)**, **[podman-systemd.unit(5)](podman-systemd.unit.5.md)**
   315  
   316  ## HISTORY
   317  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)
   318  
   319  August 2019, Updated with pod support by Valentin Rothberg (rothberg at redhat dot com)
   320  
   321  April 2019, Originally compiled by Brent Baude (bbaude at redhat dot com)