You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Blubber/Tutorial/HelloWorldRedux

From Wikitech-static
< Blubber‎ | Tutorial
Revision as of 22:00, 9 January 2019 by imported>Thcipriani (→‎Build Image and Run Container)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The previous example was extremely minimal as a means of giving a general introduction using Blubber to build Docker images that can be easily run on the command line. This example is meant to be ever so slightly more complicated as a means of introducing new Blubber concepts.

Blubberfile

A slightly more complicated Hello, World! Blubberfile might look like:

version: v3
base: docker-registry.wikimedia.org/wikimedia-stretch
apt:
  packages:
    - figlet

runs:
  environment: { HELLO_WORLD: "Hello, world!" }

variants:
  hello:
    entrypoint: [sh, -c, 'figlet $HELLO_WORLD']

Above we've installed the figlet package from apt and we've also introduced an environment variable to hold our output.

Dockerfile

The above Blubberfile produces the following Dockerfile on stdout (the output you see may vary depending on your version of blubber):

FROM docker-registry.wikimedia.org/wikimedia-stretch
USER "root"
ENV HOME="/root"
ENV DEBIAN_FRONTEND="noninteractive"
RUN apt-get update && apt-get install -y "figlet" && rm -rf /var/lib/apt/lists/*
RUN groupadd -o -g "65533" -r "somebody" && useradd -o -m -d "/home/somebody" -r -g "somebody" -u "65533" "somebody" && mkdir -p "/srv/app" && chown "65533":"65533" "/srv/app" && mkdir -p "/opt/lib" && chown "65533":"65533" "/opt/lib"
RUN groupadd -o -g "900" -r "runuser" && useradd -o -m -d "/home/runuser" -r -g "runuser" -u "900" "runuser"
USER "somebody"
ENV HOME="/home/somebody"
WORKDIR "/srv/app"
ENV HELLO_WORLD="Hello, world!"
COPY --chown=65533:65533 [".", "."]
USER "runuser"
ENV HOME="/home/runuser"
ENTRYPOINT ["sh", "-c", "figlet $HELLO_WORLD"]
LABEL blubber.variant="hello" blubber.version="0.4.0+60add2d"

You'll notice that the apt: {packages: [figlet]} declaration in the Blubberfile installed the figlet program as the root user on the commandline. You can specify packages to install from Debian at the top-level of a Blubberfile and under each individual variant.

Also notice that we've set an environment variable for our output message. You can set environment variables at the top level of a Blubberfile and under each individual variant.

Build Image and Run Container

We use docker build to generate a Docker image which is then run with docker run. This produces an ever so slightly more interesting result on the commandline:

developer@laptop:~/blubber-tutorial$ blubber hello-blubber-redux.yaml hello | docker build --tag blubber-tutorial-hello-world-redux --file - .
developer@laptop:~/blubber-tutorial$ docker run --rm --interactive --tty blubber-tutorial-hello-world-redux:latest
 _   _      _ _        __        __         _     _ _
| | | | ___| | | ___   \ \      / /__  _ __| | __| | |
| |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` | |
|  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |_|
|_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_(_)

Since we used an environment variable to set our message, we can override that variable on the command line using the --env flag:

developer@laptop:~/blubber-tutorial$ docker run --env 'HELLO_WORLD="BEST TUTORIAL EVA!"' --rm -it blubber-tutorial-hello-world-redux
 _ _ ____  _____ ____ _____   _____ _   _ _____ ___  ____  ___    _    _     
( | ) __ )| ____/ ___|_   _| |_   _| | | |_   _/ _ \|  _ \|_ _|  / \  | |    
 V V|  _ \|  _| \___ \ | |     | | | | | | | || | | | |_) || |  / _ \ | |    
    | |_) | |___ ___) || |     | | | |_| | | || |_| |  _ < | | / ___ \| |___ 
    |____/|_____|____/ |_|     |_|  \___/  |_| \___/|_| \_\___/_/   \_\_____|
                                                                             
 _______     ___    _ _ _ 
| ____\ \   / / \  | ( | )
|  _|  \ \ / / _ \ | |V V 
| |___  \ V / ___ \|_|    
|_____|  \_/_/   \_(_)