### Initialization Then Application Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Execute setup scripts before starting the main application. The sequence involves running cont-init scripts, starting user services, then running the CMD, and exiting when the CMD exits. ```dockerfile FROM ubuntu:latest # ... install s6-overlay ... # Create init and service directories RUN mkdir -p /etc/cont-init.d /etc/s6-overlay/s6-rc.d/user/contents.d # Initialization scripts COPY init-scripts/ /etc/cont-init.d/ # Service definitions COPY services/ /etc/s6-overlay/s6-rc.d/ ENTRYPOINT ["/init"] CMD ["myapp"] ``` -------------------------------- ### Configure Dockerfile for s6-overlay Source: https://github.com/just-containers/s6-overlay/wiki/Home Example Dockerfile setup to install s6-overlay and configure the entrypoint. ```dockerfile FROM busybox ADD https://github.com/just-containers/s6-overlay-builder/releases/download/v1.8.5/s6-overlay-portable-amd64.tar.gz /tmp/ RUN tar xzf /tmp/s6-overlay-portable-amd64.tar.gz -C / ENTRYPOINT ["/init"] ``` -------------------------------- ### Example Dockerfile for s6-overlay Installation Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt This is a typical Dockerfile snippet demonstrating how to install s6-overlay. Ensure you adapt the version and installation method to your specific needs. ```dockerfile FROM debian:bullseye-slim RUN apt-get update && apt-get install -y --no-install-recommends s6-overlay COPY --chown=root:root s6-overlay-release/ /usr/local/bin/ COPY --chown=root:root s6-overlay-release/ /etc/s6-overlay/ ENTRYPOINT ["/usr/bin/s6-overlay-entrypoint"] ``` -------------------------------- ### Multi-Service s6-overlay Dockerfile Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md An example Dockerfile for a multi-service setup using s6-overlay on an Ubuntu base image. It installs dependencies, s6-overlay, and configures services. ```dockerfile FROM ubuntu:20.04 RUN apt-get update && apt-get install -y \ xz-utils \ nginx \ postgresql-client \ curl ARG S6_OVERLAY_VERSION=3.2.3.0 ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz # Create service directories RUN mkdir -p /etc/s6-overlay/s6-rc.d/user/contents.d # Copy service definitions COPY rootfs/etc /etc COPY rootfs/opt /opt ENV S6_LOGGING=1 ENV S6_VERBOSITY=1 ENTRYPOINT ["/init"] ``` -------------------------------- ### Minimal s6-overlay Dockerfile Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md A basic Dockerfile demonstrating how to install s6-overlay on a busybox image by downloading and extracting the necessary tarballs. ```dockerfile FROM busybox:latest RUN mkdir -p /tmp/s6 && cd /tmp/s6 && \ wget -q https://github.com/just-containers/s6-overlay/releases/download/v3.2.3.0/s6-overlay-noarch.tar.xz && \ tar -C / -Jxpf s6-overlay-noarch.tar.xz && \ wget -q https://github.com/just-containers/s6-overlay/releases/download/v3.2.3.0/s6-overlay-x86_64.tar.xz && \ tar -C / -Jxpf s6-overlay-x86_64.tar.xz && \ rm -rf /tmp/s6 ENTRYPOINT ["/init"] ``` -------------------------------- ### Oneshot Startup Script Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of an 'up' script for a oneshot service using execline commands for directory creation and permission setting. ```shell /etc/s6-overlay/s6-rc.d/create-dirs/up if { mkdir -p /var/log/myapp } if { chown nobody:nogroup /var/log/myapp } chmod 0755 /var/log/myapp ``` -------------------------------- ### Logger Service Setup Script Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md A cont-init script example to set up the necessary directory and permissions for a logger service in s6-overlay. Ensures the log directory is created and owned by 'nobody'. ```sh #!/bin/sh # /etc/cont-init.d/01-create-myservice-log mkdir -p /var/log/myservice chown nobody:nogroup /var/log/myservice chmod 0755 /var/log/myservice ``` -------------------------------- ### Example of Service Dependencies Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Demonstrates how to define dependencies between services. This ensures that services start in the correct order, which is crucial for applications with interdependencies. ```shell # /etc/s6-rc.d/my-service/dependencies # This file lists the services that 'my-service' depends on. ``` -------------------------------- ### Dockerfile for s6-overlay Source: https://github.com/just-containers/s6-overlay/wiki/Home Example Dockerfile configuration to install s6-overlay and set the entrypoint for process supervision. ```dockerfile FROM ubuntu ADD https://github.com/just-containers/s6-overlay-builder/releases/download/v1.8.5/s6-overlay-portable-amd64.tar.gz /tmp/ RUN tar xzf /tmp/s6-overlay-portable-amd64.tar.gz -C / RUN apt-get update && \ apt-get install -y nginx && \ echo "daemon off;" >> /etc/nginx/nginx.conf ENTRYPOINT ["/init"] CMD ["nginx"] ``` -------------------------------- ### Initialization script for application setup Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md This script performs initial setup tasks for the application, including setting permissions for the log directory and running database migrations. It exits immediately if any command fails. ```sh #!/bin/sh -e chown -R appuser:appuser /var/log/app chmod 0755 /var/log/app /opt/app/bin/migrate-db ``` -------------------------------- ### User2 Bundle Late-Stage Setup Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Configure a late-stage bundle for services that must start after legacy services. This requires an explicit dependency on legacy services and explicit inclusion of the service within the user2 bundle. ```shell /etc/s6-overlay/s6-rc.d/late-service/dependencies.d/legacy-services # Empty file /etc/s6-overlay/s6-rc.d/user2/contents.d/late-service # Empty file ``` -------------------------------- ### Example of Service Readiness Check Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Shows how to define a readiness check for a service. This ensures that a service is fully operational before other services that depend on it are started. ```shell # /etc/s6-rc.d/my-service/ready # This script checks if 'my-service' is ready to be used. ``` -------------------------------- ### S6-RC Service Dependencies Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Define service dependencies by creating empty files in the dependencies.d directory. A service will not start until all listed dependencies are ready. ```bash dependencies.d/ ├── base # Depend on base bundle ├── database # Depend on database service └── network-ready # Depend on network-ready service ``` -------------------------------- ### Oneshot Startup Script with Execline Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of an 'up' script for a oneshot service using execline commands for database migration. ```shell /etc/s6-overlay/s6-rc.d/db-migrate/up /usr/local/bin/migrate-script ``` -------------------------------- ### User Bundle Standard Setup Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Configure the main application bundle by defining its type as 'bundle'. This setup includes directories for user-defined services like applications, loggers, and database connectors. ```shell /etc/s6-overlay/s6-rc.d/user/type bundle /etc/s6-overlay/s6-rc.d/user/contents.d/ ├── myapp # Your main application ├── mylogger # Logger service └── mydb-connector # Helper service ``` -------------------------------- ### Execute One-Time Initialization Script with confd Source: https://github.com/just-containers/s6-overlay/blob/master/README.md This script is an example of a one-time initialization task executed during container startup. It uses `confd` to process configuration templates based on environment variables and etcd data. Ensure `confd` is installed and configured. ```execline #!/command/execlineb -P with-contenv s6-envuidgid nginx multisubstitute { import -u -D0 UID import -u -D0 GID import -u CONFD_PREFIX define CONFD_CHECK_CMD "/usr/sbin/nginx -t -c {{ .src }}" } confd --onetime --prefix="${CONFD_PREFIX}" --tmpl-uid="${UID}" --tmpl-gid="${GID}" --tmpl-src="/etc/nginx/nginx.conf.tmpl" --tmpl-dest="/etc/nginx/nginx.conf" --tmpl-check-cmd="${CONFD_CHECK_CMD}" etcd ``` -------------------------------- ### Example of a CI/CD Build Target Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Illustrates a typical build target within a CI/CD pipeline for s6-overlay. This example focuses on building a release artifact. ```shell docker build -t my-s6-overlay-image:latest . ``` -------------------------------- ### Preinit Notice Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/initialization-lifecycle.md This example shows a typical notice message from the preinit script when it detects and fixes an issue with the /var/run symlink. It is displayed when S6_VERBOSITY is set to 2 or higher. ```shell preinit: notice: /var/run is not a symlink to /run, fixing it ``` -------------------------------- ### Service Dependency Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of defining service dependencies by creating empty files in the 'dependencies.d/' directory. 'base' dependency ensures system readiness. ```shell /etc/s6-overlay/s6-rc.d/myservice/dependencies.d/base ``` -------------------------------- ### Bundle Contents Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of defining a bundle service by creating an empty 'type' file with 'bundle' content and listing included services as empty files in 'contents.d/'. ```shell /etc/s6-overlay/s6-rc.d/user/type bundle /etc/s6-overlay/s6-rc.d/user/contents.d/ ├── myservice # Empty file ├── mylogger # Empty file └── mydb # Empty file ``` -------------------------------- ### Initialize using Execline Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md An example of an initialization script written in execline, which uses `with-contenv` to access environment variables and `backtick` to capture output. ```execline #!/command/execlineb -P with-contenv backtick dburl { printcontenv DATABASE_URL } /usr/local/bin/migrate-db ${dburl} ``` -------------------------------- ### Oneshot Startup Script with External Script Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of an 'up' script for a oneshot service that calls an external shell script for complex logic. ```shell /etc/s6-overlay/s6-rc.d/init-service/up /usr/local/bin/init-service.sh ``` -------------------------------- ### Environment Variable Setup Comparison Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md Compares the v2 approach of using `export` in cont-init scripts with the recommended v3 approaches: `ENV` in Dockerfile or using `command/with-contenv sh`. ```sh # In cont-init script export MY_VAR=value ``` ```dockerfile ENV MY_VAR=value ``` ```sh # MY_VAR available from environment exec myapp --var="$MY_VAR" ``` -------------------------------- ### Service Field Examples Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/api-summary.md Provides examples for various service fields, including type, scripts, dependencies, and configuration. ```plaintext type | Yes | "longrun" | "longrun" ``` ```shell #!/bin/sh exec myapp ``` ```execline mkdir -p /var/log ``` ```execline rm -rf /tmp/cache ``` ```shell #!/bin/sh echo $1 > /exit-code ``` ```plaintext base, other-service ``` ```plaintext 3 ``` ```plaintext 30000 ``` ```plaintext mylogger ``` ```plaintext myapp ``` ```plaintext myapp-pipeline ``` -------------------------------- ### Install s6-overlay in Dockerfile Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Installs both the noarch scripts and the architecture-specific binaries for s6-overlay. Ensure xz-utils is installed first. ```dockerfile FROM ubuntu:latest ARG S6_OVERLAY_VERSION=3.2.3.0 RUN apt-get update && apt-get install -y xz-utils # Install s6-overlay scripts (required) ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz # Install s6 ecosystem binaries (required) ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz # Optional: /usr/bin symlinks for backwards compatibility # ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-symlinks-noarch.tar.xz /tmp # RUN tar -C / -Jxpf /tmp/s6-overlay-symlinks-noarch.tar.xz ENTRYPOINT ["/init"] ``` -------------------------------- ### Readiness Notification FD Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of configuring readiness notification by specifying a file descriptor number in the 'notification-fd' file. The service must write to this FD when ready. ```text 3 ``` -------------------------------- ### Up script for initialization service Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Specifies the script '/etc/s6-overlay/scripts/init-app' to be executed when the 'init-service' starts. ```sh /etc/s6-overlay/scripts/init-app ``` -------------------------------- ### Example of logutil-service Command Usage Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Demonstrates the basic usage of the logutil-service command, typically used for managing logging services. Refer to the documentation for advanced configurations. ```shell logutil-service logutil-service my-log-service ``` -------------------------------- ### Example of a Service Bundle Definition Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt This illustrates how services can be bundled together. Bundles allow for managing groups of related services as a single unit. ```shell # /etc/s6-rc.d/my-bundle/bundle.sh # This script defines the services that belong to the 'my-bundle' bundle. ``` -------------------------------- ### Fix File Ownership and Permissions Example Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Example of a fix-attrs configuration file to set ownership and permissions for MySQL data directories. ```text /var/lib/mysql true mysql 0600 0700 ``` -------------------------------- ### Dockerfile Setup for s6-overlay Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Use this Dockerfile snippet to add s6-overlay to your image. It downloads and extracts the overlay archives and sets the entrypoint. Ensure the S6_OVERLAY_VERSION variable is set. ```dockerfile FROM busybox ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz ENTRYPOINT ["/init"] ``` -------------------------------- ### Run Container with Environment Variables Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Start a Docker container and set environment variables for the application. ```bash docker run \ -e APP_PORT=3000 \ -e DATABASE_URL=postgresql://db:5432/mydb \ myimage ``` -------------------------------- ### Finish Script Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of a 'finish' script, executed when a service (longrun or oneshot) exits or is terminated. It receives exit code and signal as arguments. ```shell /etc/s6-overlay/s6-rc.d/myservice/finish #!/bin/sh EXIT_CODE=$1 SIGNAL=$2 # Cleanup ``` -------------------------------- ### Verify Build Tools Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/build-system.md Run these commands to ensure the required build tools are installed and meet the minimum version requirements. ```bash make --version tar --version | grep -i xz sed --version ``` -------------------------------- ### Rollback Example: s6-overlay v2 to v3 Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md Demonstrates how to stop a v3 container and restart a v2 container for rollback purposes. ```bash # v2 still works docker run myapp:v2.2.0.3 # v3 new version docker run myapp:v3.2.3.0 # Rollback if needed docker stop myapp:v3.2.3.0 docker run myapp:v2.2.0.3 ``` -------------------------------- ### Build s6-overlay with local toolchain Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/build-system.md Uses a locally installed cross-compilation toolchain for building s6-overlay for aarch64. ```bash make TOOLCHAIN_PATH=/opt/musl-cross/aarch64-linux-musl all ``` -------------------------------- ### Producer-Consumer Pipeline Setup Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Define a producer-consumer pipeline where one service's stdout is piped to another's stdin. This setup involves defining service types, run scripts, and specifying the producer/consumer relationships and pipeline names. ```shell /etc/s6-overlay/s6-rc.d/myapp/type longrun /etc/s6-overlay/s6-rc.d/myapp/run #!/bin/sh exec 2>&1 exec myapp /etc/s6-overlay/s6-rc.d/myapp/producer-for myapp /etc/s6-overlay/s6-rc.d/mylogger/type longrun /etc/s6-overlay/s6-rc.d/mylogger/run #!/bin/sh exec logutil-service /var/log/myapp /etc/s6-overlay/s6-rc.d/mylogger/consumer-for myapp /etc/s6-overlay/s6-rc.d/mylogger/pipeline-name myapp-pipeline /etc/s6-overlay/s6-rc.d/user/contents.d/myapp-pipeline # Empty file to include pipeline ``` -------------------------------- ### s6-overlay Package Layout Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/overview.md Illustrates the directory structure within the s6-overlay package installation. ```bash /package/admin/s6-overlay-${VERSION}/ ├── command/ # User-facing scripts ├── libexec/ # Internal scripts ├── etc/s6-linux-init/skel/ # Init scripts └── etc/s6-rc/ # s6-rc service definitions ``` -------------------------------- ### Nginx Service 'run' Script Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md An example 'run' script for an Nginx service using the legacy s6-overlay format. It redirects stderr to stdout and uses 'daemon off;' to keep Nginx in the foreground. ```sh #!/bin/sh exec 2>&1 exec nginx -g "daemon off;" ``` -------------------------------- ### Notification File Descriptor Example with Execline Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Demonstrates using the 'notification-fd' feature with an 'execlineb' script. The service signals readiness using 's6-notifywhenup' on file descriptor 5. ```sh #!/command/execlineb -P s6-notifywhenup -n 5 myservice ``` -------------------------------- ### Example of logutil-newfifo Command Usage Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Shows how to use logutil-newfifo to create a new FIFO pipe for logging. This is a fundamental part of setting up custom logging streams. ```shell logutil-newfifo /path/to/my.fifo ``` -------------------------------- ### Pure s6 Service Script Example Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Define a supervised service using a pure s6 service directory. Place this script in /etc/services.d//run. It requires an executable 'run' file. ```bash #!/command/execlineb -P nginx -g "daemon off;" ``` -------------------------------- ### Example of a Service Pipeline Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Illustrates the concept of a service pipeline, where services are executed in a sequence. This is useful for complex startup routines or data processing chains. ```shell # /etc/s6-rc.d/my-pipeline/pipeline.sh # This script defines the execution order for services in the pipeline. ``` -------------------------------- ### Example of Build System Variable Usage Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Shows how build variables are used within the s6-overlay build system. These variables control various aspects of the build process. ```shell make BUILD_TYPE=release ``` -------------------------------- ### S6-RC Producer-Consumer Pipeline Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Establish a pipeline between two services using 'producer-for' and 'consumer-for' files. The producer's stdout is automatically piped to the consumer's stdin. ```text /etc/s6-overlay/s6-rc.d/producer-service/producer-for producer-service /etc/s6-overlay/s6-rc.d/consumer-service/consumer-for consumer-service ``` -------------------------------- ### Dockerfile for Application with Database Migration Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md This Dockerfile prepares an application environment with s6-overlay, including a database migration script. It installs PostgreSQL client, sets up the migration script, and configures s6-overlay behavior. ```dockerfile FROM ubuntu:20.04 RUN apt-get update && apt-get install -y xz-utils postgresql-client ARG S6_OVERLAY_VERSION=3.2.3.0 ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz RUN mkdir -p /etc/cont-init.d COPY migrate.sh /etc/cont-init.d/01-migrate COPY app /opt/app ENV S6_LOGGING=1 ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2 ENTRYPOINT ["/init"] CMD ["/opt/app"] ``` -------------------------------- ### S6-RC Service Startup Timeout Configuration Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Configure the startup timeout for a service by creating a 'timeout-up' file with the timeout duration in milliseconds. If the service does not start within this period, it is considered failed. ```text 30000 ``` -------------------------------- ### Troubleshoot '/init: not found' Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Resolve '/init: not found' errors at runtime by ensuring correct tarball extraction, base image architecture, and executable bit. ```bash tar -Jxpf ... -p ``` ```bash RUN ls -la /init ``` ```bash file /command/execlineb ``` -------------------------------- ### Example of Environment Variable Mapping Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Demonstrates how environment variables can be mapped or transformed. This is particularly useful during migration from v2 to v3 to handle changes in variable names or formats. ```shell # Example: Mapping OLD_VAR to NEW_VAR export NEW_VAR=${OLD_VAR:-default_value} ``` -------------------------------- ### Example of with-contenv Command Usage Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt Shows how to use the with-contenv command to execute a command with specific environment variables set. This is helpful for isolating environment configurations for specific tasks. ```shell with-contenv MY_VAR=value another-command ``` -------------------------------- ### Simple Oneshot Up Script Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Use for basic service startup tasks. Executed as an execline command. ```execline mkdir -p /var/log/myapp ``` -------------------------------- ### Container CMD Exit Code Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/initialization-lifecycle.md Illustrates the Dockerfile setup where the container's exit code is determined by the CMD process. ```dockerfile ENTRYPOINT ["/init"] CMD ["myapp"] ``` -------------------------------- ### Legacy Service Definition Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt This snippet shows the legacy format for defining services in s6-overlay, typically found in /etc/services.d. It's important for understanding older configurations or for migration purposes. ```shell #!/bin/sh # /etc/services.d/my-service/run exec /path/to/my/executable --arg1 --arg2 ``` -------------------------------- ### Logger Service 'log/run' Script Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md An example 'log/run' script for a legacy s6-overlay service, using 'logutil-service' to manage logs for a specific service. Requires specific permissions and redirection setup. ```sh #!/bin/sh exec logutil-service /var/log/myservice ``` -------------------------------- ### Configure and Build s6-overlay with Custom Variables Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/api-summary.md This command demonstrates how to configure the build process for s6-overlay by setting various variables such as architecture, version, output directory, and toolchain paths. It then proceeds to build all components. ```bash make \ ARCH=x86_64-linux-musl \ VERSION=3.2.3.0 \ OUTPUT=output \ SHEBANGDIR=/command \ TOOLCHAIN_PATH= \ TOOLCHAIN_VERSION=15.2.0 \ DL_CMD=wget \ all ``` -------------------------------- ### Oneshot Shutdown Script Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Example of a 'down' script for a oneshot service, used for cleanup tasks when the service stops. ```shell /etc/s6-overlay/s6-rc.d/myservice/down redirfd -w 1 /var/log/myservice.done s6-echo "Service stopped" ``` -------------------------------- ### Install Architecture-Specific s6-overlay Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Installs the s6-overlay binaries tailored for the specific Docker build architecture using the `TARGETARCH` build argument. ```dockerfile FROM ubuntu:latest ARG TARGETARCH RUN apt-get update && apt-get install -y xz-utils ARG S6_OVERLAY_VERSION=3.2.3.0 ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-${TARGETARCH}.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-${TARGETARCH}.tar.xz ``` -------------------------------- ### Oneshot Up Script with Conditionals Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Combine commands with conditional checks for more robust startup. Ensures directories are created before changing ownership. ```execline if { mkdir -p /var/log/myapp } chown appuser:appuser /var/log/myapp ``` -------------------------------- ### Initialize with Environment Variables Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md This script demonstrates how to use environment variables within initialization scripts. It checks for DATABASE_URL and exits with an error if not set. ```sh #!/command/with-contenv sh if [ -z "$DATABASE_URL" ]; then echo "ERROR: DATABASE_URL not set" >&2 exit 1 fi /usr/local/bin/migrate-db "$DATABASE_URL" ``` -------------------------------- ### Modern s6-rc Service Definition Example Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/INDEX.txt An example of a modern s6-rc service definition. This format is more structured and is the recommended approach for new service definitions. ```shell # /etc/s6-rc.d/my-service/run #!/bin/sh exec /path/to/my/executable --arg1 --arg2 ``` -------------------------------- ### Build s6-overlay for multiple architectures Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/build-system.md Builds s6-overlay for x86_64, aarch64, and arm-linux-musleabihf architectures sequentially. ```bash make ARCH=x86_64-linux-musl all make ARCH=aarch64-linux-musl all make ARCH=arm-linux-musleabihf all ``` -------------------------------- ### Declare base dependency for user services Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md Ensure user services start correctly in v3 by adding the 'dependencies.d/base' declaration. This is a common fix for services not starting. ```dockerfile RUN echo > /etc/s6-overlay/s6-rc.d/myservice/dependencies.d/base ``` -------------------------------- ### Set Version and Build Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/build-system.md Set the VERSION variable to a specific version and then clean and build the project. This is typically the first step before building for distribution. ```bash make VERSION=3.2.4.0 distclean all ``` -------------------------------- ### Migrate Simple Single-Service Container Dockerfile Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md Update the base image and installation method for s6-overlay when migrating a simple single-service container. Ensure xz-utils is installed for .tar.xz extraction. ```dockerfile FROM ubuntu:18.04 RUN tar xzf s6-overlay-amd64.tar.gz -C / ENTRYPOINT ["/init"] CMD ["myapp"] ``` ```dockerfile FROM ubuntu:20.04 RUN apt-get install xz-utils ADD s6-overlay-noarch.tar.xz /tmp && tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ADD s6-overlay-x86_64.tar.xz /tmp && tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz ENTRYPOINT ["/init"] CMD ["myapp"] ``` -------------------------------- ### Fix Log Directory Ownership and Permissions Example Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Example of fix-attrs configuration files to set ownership and permissions for various MySQL log directories, using a fallback UID/GID. ```text /var/log/mysql-error-logs true nobody,32768:32768 0644 2700 /var/log/mysql-general-logs true nobody,32768:32768 0644 2700 /var/log/mysql-slow-query-logs true nobody,32768:32768 0644 2700 ``` -------------------------------- ### s6-overlay Stage0 Configuration Analysis Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/api-summary.md This internal command analyzes the configuration and creates the /run/s6/basedir. It is called by preinit and subsequently execs into init. ```shell /package/admin/s6-overlay-3.2.3.0/libexec/stage0 ``` -------------------------------- ### Enable Waiting for Services Before CMD Execution Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/configuration.md When set to a non-zero value, the container's CMD will not start until all legacy services in `/etc/services.d` have become ready. This ensures services are operational before the main application starts. ```dockerfile ENV S6_CMD_WAIT_FOR_SERVICES=1 ``` -------------------------------- ### Dockerfile for Nginx with Health Check Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md This Dockerfile sets up an Nginx server with s6-overlay, including a health check. It installs Nginx and curl, downloads and installs s6-overlay, copies the Nginx configuration, and sets the entrypoint to /init. ```dockerfile FROM ubuntu:20.04 RUN apt-get update && apt-get install -y xz-utils nginx curl ARG S6_OVERLAY_VERSION=3.2.3.0 ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz COPY nginx.conf /etc/nginx/nginx.conf HEALTHCHECK --interval=30s CMD curl -f http://localhost/ || exit 1 ENV S6_LOGGING=1 ENTRYPOINT ["/init"] ``` -------------------------------- ### Verify /init and /package/admin existence Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md Check if essential files exist after extraction, which is crucial for v3 startup. Use these commands in your Dockerfile. ```dockerfile RUN ls -la /init RUN ls -la /package/admin/ ``` -------------------------------- ### Build for All Architectures Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/build-system.md Iterate through a list of target architectures, setting the ARCH and VERSION variables, and then build the project for each. This ensures compatibility across different systems. ```bash for arch in x86_64-linux-musl aarch64-linux-musl arm-linux-musleabihf; do make ARCH=$arch VERSION=3.2.4.0 all done ``` -------------------------------- ### Build and Run s6-overlay Container Source: https://github.com/just-containers/s6-overlay/wiki/Home Commands to build the image, run the container, verify process supervision, and test the service. ```bash docker-host $ docker build -t demo . docker-host $ docker run --name s6demo -d -p 80:80 demo docker-host $ docker top s6demo acxf PID TTY STAT TIME COMMAND 3788 ? Ss 0:00 \_ s6-svscan 3827 ? S 0:00 | \_ foreground 3834 ? S 0:00 | | \_ foreground 3879 ? S 0:00 | | \_ nginx 3880 ? S 0:00 | | \_ nginx 3881 ? S 0:00 | | \_ nginx 3882 ? S 0:00 | | \_ nginx 3883 ? S 0:00 | | \_ nginx 3828 ? S 0:00 | \_ s6-supervise 3829 ? S 0:00 | \_ s6-supervise 3830 ? Ss 0:00 | \_ s6-log docker-host $ curl --head http://127.0.0.1/ HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Date: Thu, 26 Mar 2015 14:57:34 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT Connection: keep-alive ETag: "5315bd25-264" Accept-Ranges: bytes ``` -------------------------------- ### Debug Services with Verbosity Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Run a container interactively or with increased verbosity to debug why services are not starting. ```bash docker run -it --entrypoint=/bin/sh myimage # Inside container: /init & sleep 2 ps aux cat /var/log/s6-uncaught-logs/current ``` ```bash docker run \ -e S6_VERBOSITY=5 \ -e S6_LOGGING=1 \ myimage ``` -------------------------------- ### Run Interactive Shell in Container Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/installation.md Start a Docker container and open an interactive shell session. ```bash docker run -it myimage /bin/sh ``` -------------------------------- ### Drop privileges in execline Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Example of how to drop privileges to the 'daemon' user before executing 'myservice' using execline. ```execline #!/command/execlineb -P s6-setuidgid daemon myservice ``` -------------------------------- ### Create Log Directories with Permissions Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Use this script to create and set ownership/permissions for application log directories during container initialization. ```sh #!/bin/sh mkdir -p /var/log/myapp chown myuser:myuser /var/log/myapp chmod 0755 /var/log/myapp ``` -------------------------------- ### Oneshot Up Script with Environment Variables Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Dynamically create directories and initialize applications using environment variables. The `backtick` command captures output for use in subsequent commands. ```execline backtick appdir { printcontenv APP_DIR } if { mkdir -p ${appdir}/data } if { mkdir -p ${appdir}/logs } /usr/local/bin/init-app ${appdir} ``` -------------------------------- ### Drop privileges in sh Source: https://github.com/just-containers/s6-overlay/blob/master/README.md Example of how to drop privileges to the 'daemon' user before executing 'myservice' using a standard shell script. ```sh #!/bin/sh exec s6-setuidgid daemon myservice ``` -------------------------------- ### Oneshot Down Script for Cleanup Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/hooks-and-scripts.md Use for service shutdown tasks. This example removes a lock file and runs a cleanup script. ```execline if { s6-rmrf /tmp/myapp-lock } /usr/local/bin/cleanup-myapp ``` -------------------------------- ### Service 'run' Script with Environment Variables Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md Demonstrates how to use environment variables within a legacy s6-overlay service 'run' script, utilizing '/command/with-contenv' for proper shell expansion. ```sh #!/command/with-contenv sh exec 2>&1 exec myapp --config="${CONFIG_FILE}" ``` -------------------------------- ### s6-overlay Init Entrypoint Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/api-summary.md The primary entry point for s6-overlay, set as the container's ENTRYPOINT. It triggers the preinit and stage0 sequences. ```shell /init ``` -------------------------------- ### Keep Legacy Services During Migration Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/migration-guide.md This Dockerfile snippet shows how to install s6-overlay v3 while keeping legacy services from `/etc/services.d` functional for a safer migration. ```dockerfile # Install s6-overlay v3 # Copy legacy services (will still work) COPY rootfs/etc/services.d /etc/services.d ENTRYPOINT ["/init"] ``` -------------------------------- ### Synchronize Startup with S6_WAIT_FOR_CLOSING_FD Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/configuration.md Configure s6-overlay to wait for EOF on a specified file descriptor before proceeding with startup. This is useful for synchronization with external systems like Docker or systemd. ```dockerfile ENV S6_WAIT_FOR_CLOSING_FD=3 ``` -------------------------------- ### Finish Script to Halt Container Source: https://github.com/just-containers/s6-overlay/blob/master/_autodocs/service-definitions.md A 'finish' script example for a legacy s6-overlay service that halts the container by writing the exit code and calling '/run/s6/basedir/bin/halt'. ```sh #!/bin/sh EXIT_CODE=$1 SIGNAL=$2 echo $1 > /run/s6-linux-init-container-results/exitcode /run/s6/basedir/bin/halt ```