|
| 1 | +--- |
| 2 | +title: Use Podman in Builds |
| 3 | +layout: en |
| 4 | +--- |
| 5 | + |
| 6 | +Travis CI builds can run and build container images with Podman, and can also push images to container repositories or other remote storage. Podman provides a Docker-compatible command-line interface, making it a suitable alternative that doesn't require a daemon to run. |
| 7 | + |
| 8 | +To use Podman add the following settings to your `.travis.yml`: |
| 9 | + |
| 10 | +```yaml |
| 11 | +services: |
| 12 | + - podman |
| 13 | +``` |
| 14 | +{: data-file=".travis.yml"} |
| 15 | +
|
| 16 | +Then you can add `- podman` commands to your build as shown in the following examples. |
| 17 | + |
| 18 | +> We do not currently support use of Podman on macOS. |
| 19 | + |
| 20 | +> For information on how to use Podman on Travis CI Enterprise, check out [Enabling Container Builds](/user/enterprise/build-images/#enabling-container-builds). |
| 21 | + |
| 22 | +## Use a Container Image from a Repository |
| 23 | + |
| 24 | +This example runs two container instances built from the same image: |
| 25 | + |
| 26 | +- a web application |
| 27 | +- the application test suite |
| 28 | + |
| 29 | +After specifying in the `.travis.yml` to use Podman (with `services: - podman`) and your language of choice, the `before_install` build step pulls a container image then runs it with appropriate parameters. |
| 30 | + |
| 31 | +The full `.travis.yml` might look like this: |
| 32 | + |
| 33 | +```yaml |
| 34 | +language: ruby |
| 35 | +
|
| 36 | +services: |
| 37 | + - podman |
| 38 | +
|
| 39 | +before_install: |
| 40 | +- podman pull docker.io/nginx:latest |
| 41 | +- podman run -d -p 127.0.0.1:80:8080 docker.io/nginx:latest |
| 42 | +- podman ps -a |
| 43 | +- podman run --rm docker.io/nginx:latest /bin/sh -c "nginx -v" |
| 44 | +
|
| 45 | +script: |
| 46 | +- bundle exec rake test |
| 47 | +``` |
| 48 | +{: data-file=".travis.yml"} |
| 49 | + |
| 50 | +## Build a Container Image from a Containerfile |
| 51 | + |
| 52 | +Instead of downloading the container image from a registry, you can build it directly from a Containerfile (equivalent to a Dockerfile) in your repository. |
| 53 | + |
| 54 | +To build the Containerfile in the current directory and give it a label, use the following command: |
| 55 | + |
| 56 | +```bash |
| 57 | +podman build -t myusername/myapp . |
| 58 | +``` |
| 59 | + |
| 60 | +The full `.travis.yml` would look like this: |
| 61 | + |
| 62 | +```yaml |
| 63 | +language: ruby |
| 64 | +
|
| 65 | +services: |
| 66 | + - podman |
| 67 | +
|
| 68 | +before_install: |
| 69 | + - podman build -t myusername/myapp . |
| 70 | + - podman run -d -p 127.0.0.1:80:8080 myusername/myapp /bin/sh -c "cd /app; ./start.sh;" |
| 71 | + - podman ps -a |
| 72 | + - podman run myusername/myapp /bin/sh -c "cd /app; ./run_tests.sh" |
| 73 | +
|
| 74 | +script: |
| 75 | + - bundle exec rake test |
| 76 | +``` |
| 77 | +{: data-file=".travis.yml"} |
| 78 | + |
| 79 | +## Push a Container Image to a Registry |
| 80 | + |
| 81 | +To push an image to a container registry, one must first authenticate via `podman login`. The username and password used for login should be stored in the repository settings environment variables, which may be set up through the repository settings web page or locally via the Travis CLI, e.g.: |
| 82 | + |
| 83 | +```bash |
| 84 | +travis env set REGISTRY_USERNAME myusername |
| 85 | +travis env set REGISTRY_PASSWORD secretsecret |
| 86 | +``` |
| 87 | + |
| 88 | +Be sure to [encrypt environment variables](/user/environment-variables/#encrypting-environment-variables) using the travis gem. |
| 89 | + |
| 90 | +Within your `.travis.yml` prior to attempting a `podman push` or perhaps before `podman pull` of a private image, e.g.: |
| 91 | + |
| 92 | +```bash |
| 93 | +echo "$REGISTRY_PASSWORD" | podman login -u "$REGISTRY_USERNAME" --password-stdin quay.io |
| 94 | +``` |
| 95 | + |
| 96 | +### Branch-Based Registry Pushes |
| 97 | + |
| 98 | +To push a particular branch of your repository to a remote registry, use the custom deploy section of your `.travis.yml`: |
| 99 | + |
| 100 | +```yaml |
| 101 | +deploy: |
| 102 | + provider: script |
| 103 | + script: bash container_push |
| 104 | + on: |
| 105 | + branch: master |
| 106 | +``` |
| 107 | +{: data-file=".travis.yml"} |
| 108 | + |
| 109 | +Where `container_push` is a script in your repository containing: |
| 110 | + |
| 111 | +```bash |
| 112 | +#!/bin/bash |
| 113 | +echo "$REGISTRY_PASSWORD" | podman login -u "$REGISTRY_USERNAME" --password-stdin quay.io |
| 114 | +podman push myusername/myapp |
| 115 | +``` |
| 116 | +{: data-file="container_push"} |
| 117 | + |
| 118 | +### Private Registry Login |
| 119 | + |
| 120 | +When pushing to a private registry, be sure to specify the hostname in the `podman login` command, e.g.: |
| 121 | + |
| 122 | +```bash |
| 123 | +echo "$REGISTRY_PASSWORD" | podman login -u "$REGISTRY_USERNAME" --password-stdin registry.example.com |
| 124 | +``` |
| 125 | + |
| 126 | +## Use Podman Compose |
| 127 | + |
| 128 | +The Podman Compose tool allows you to run multi-container applications. You can install it in your build environment by adding the following to your `.travis.yml`: |
| 129 | + |
| 130 | +```yaml |
| 131 | +before_install: |
| 132 | + - pip install podman-compose |
| 133 | +``` |
| 134 | +{: data-file=".travis.yml"} |
| 135 | + |
| 136 | +## Install a specific Podman version |
| 137 | + |
| 138 | +You can install a specific version of Podman by manually updating it in the `before_install` step of your `.travis.yml`: |
| 139 | + |
| 140 | +```yaml |
| 141 | +before_install: |
| 142 | + - source /etc/os-release |
| 143 | + - echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list |
| 144 | + - curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key" | sudo apt-key add - |
| 145 | + - sudo apt-get update |
| 146 | + - sudo apt-get -y install podman=<specific-version> |
| 147 | +``` |
| 148 | +{: data-file=".travis.yml"} |
| 149 | + |
| 150 | +> Check what version of Podman you're running with `podman --version` |
| 151 | + |
| 152 | +## Examples |
| 153 | + |
| 154 | +- [example/podman-web-app](https://github.com/example/podman-web-app/blob/master/.travis.yml) (A web application using Podman for container management) |
| 155 | +- [example/podman-backup](https://github.com/example/podman-backup/blob/master/.travis.yml) (A cron job that backs up databases running in Podman containers) |
0 commit comments