Code Monkey home page Code Monkey logo

socket-activate-httpd's Introduction

socket-activate-httpd

A demo of how to socket activate an httpd container with Podman.

When using socket activation, there are some changes regarding how to run podman run:

Requirements

  • curl
  • podman version 3.4.0 (released September 2021) or newer
  • container-selinux version 2.183.0 (released April 2022) or newer

(If you are using an older version of container-selinux and it does not work, add --security-opt label=disable to podman run)

About the container image

The container image ghcr.io/eriksjolund/socket-activate-httpd is built by the GitHub Actions workflow .github/workflows/publish_container_image.yml from the file ./Containerfile.

Socket activate an httpd systemd user service

  1. Start the httpd socket unit

    git clone https://github.com/eriksjolund/socket-activate-httpd.git
    mkdir -p ~/.config/systemd/user
    cp -r socket-activate-httpd/systemd/httpd* ~/.config/systemd/user
    systemctl --user daemon-reload
    systemctl --user start httpd.socket
    

    The user service httpd.service will be started as soon as a client connects to the listening socket.

  2. Run curl on the host to download a webpage from httpd in the container.

    $ curl -s localhost:8080 | head -6
    <!doctype html>
    <html>
      <head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Test Page for the HTTP Server on Fedora</title>
    $
    
  3. Try to establish an outgoing connection by running curl in the container

    $ podman exec -t httpd curl https://podman.io
    curl: (6) Could not resolve host: podman.io
    $
    

    (The command-line option --network=none was added to podman run to prevent the container from establishing outgoing connections)

Socket activate httpd with systemd-socket-activate

If you just ran the previous example, first run systemctl --user stop httpd.service and systemctl --user stop httpd.socket. The TCP port 8080 needs to be available for this example.

  1. Socket activate the httpd server

    $ systemd-socket-activate -l 8080 podman run --rm --name httpd2 --network=none ghcr.io/eriksjolund/socket-activate-httpd
    
  2. In another shell

    $ curl -s localhost:8080 | head -6
    <!doctype html>
    <html>
      <head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Test Page for the HTTP Server on Fedora</title>
    $
    
  3. Try establishing an outgoing connection

    $ podman exec -t httpd2 curl https://podman.io
    curl: (6) Could not resolve host: podman.io
    $
    

httpd socket activation configuration

The passed in sockets need to match corresponding Listen directives in the httpd configuration. For example, here the port number 8080 needs to used both in the file httpd.conf and in the socket unit httpd.socket.

    $ grep 8080 systemd/httpd.socket
    ListenStream=127.0.0.1:8080
    $ grep 8080 Containerfile
    RUN sed -i "s/Listen 80/Listen 127.0.0.1:8080/g" /etc/httpd/conf/httpd.conf
    $

Troubleshooting

The container takes long time to start

Pulling a container image may take long time. This delay can be avoided by pulling the container image beforehand and adding the command-line option --pull=never to podman run.

A good way to diagnose problems is to look in the journald log for the service:

journalctl -xe --user -u httpd.service

socket-activate-httpd's People

Contributors

eriksjolund avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

socket-activate-httpd's Issues

Migrate the example to use Quadlet

The command podman generate systemd is deprecated since this PR:

The command was used to generate this file contents

[Unit]
Description=socket-activate-httpd
Wants=network-online.target
After=network-online.target
RequiresMountsFor=%t/containers
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStartPre=/bin/rm -f %t/%n.ctr-id
ExecStart=/usr/bin/podman run \
--cidfile=%t/%n.ctr-id \
--cgroups=no-conmon \
--rm \
--sdnotify=conmon \
--replace \
--name httpd \
--detach \
--network none \
ghcr.io/eriksjolund/socket-activate-httpd
ExecStop=/usr/bin/podman stop --ignore --cidfile=%t/%n.ctr-id
ExecStopPost=/usr/bin/podman rm -f --ignore --cidfile=%t/%n.ctr-id
Type=notify
NotifyAccess=all
[Install]
WantedBy=default.target

Convert the example to use Quadlet instead.
See https://docs.podman.io/en/latest/markdown/podman-systemd.unit.5.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.