环境:MAC OS X 10.12.3 + Docker 1.13.1 + S2I 1.1.5
本文构建一个 lighttpd builder image,比较简单,旨在了解构建 S2I Builder Image 的主要步骤和原理。
以下操作均在 MAC 机器上执行。
1. 下载并安装 S2I 工具
下载地址:https://github.com/openshift/source-to-image/releases
下载介质:source-to-image-v1.1.5-4dd7721-darwin-amd64.tar.gz
安装
tar zxvf source-to-image-v1.1.5-4dd7721-darwin-amd64.tar.gz
mv s2i sti /usr/local/bin/
2. 创建 S2I 目录结构
s2i create lighttpd-centos7 s2i-lighttpd
说明:这里 lighttpd-centos7 是 builder image 名称,s2i-lighttpd 是目录名称。
在 s2i-lighttpd 目录下有:
(1)Dockerfile – 定义 builder image
(2)Makefile – 包含构建 builder image 和测试 builder image 的脚本
(3)test/
(3.1)run – 测试脚本
(3.2)test-app/ – 要测试应用
(4).s2i/bin/
(4.1)assemble – 构建应用的脚本
(4.2)run – 运行应用的脚本
(4.3)save-artifacts – 增量构建的脚本
(4.4)usage – 如何使用 builder image 的帮助脚本
3. 定义 Docker file
内容如下:
# We are basing our builder image on openshift base-centos7 image
FROM openshift/base-centos7
# Inform users who's the maintainer of this builder image
MAINTAINER Ma Ping <pma@redhat.com>
# Inform about software versions being used inside the builder
ENV LIGHTTPD_VERSION=1.4.45
# Set labels used in OpenShift to describe the builder images
LABEL io.k8s.description="Platform for serving static HTML files" \
io.k8s.display-name="Lighttpd 1.4.45" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,html,lighttpd"
# Install the required software, namely Lighttpd and
COPY CentOS7-Base-163.repo /etc/yum.repos.d/CentOS7-Base-163.repo
RUN yum install -y epel-release && \
yum install -y lighttpd && \
# clean yum cache files, as they are not needed and will only make the image bigger in the end
yum clean all -y
# Defines the location of the S2I
# Although this is defined in openshift/base-centos7 image it's repeated here
# to make it clear why the following COPY operation is happening
LABEL io.openshift.s2i.scripts-url=image:///usr/local/s2i
# Copy the S2I scripts from ./.s2i/bin/ to /usr/local/s2i when making the builder image
COPY ./.s2i/bin/ /usr/local/s2i
# Copy the lighttpd configuration file
COPY ./etc/ /opt/app-root/etc
# Drop the root user and make the content of /opt/app-root owned by user 1001
RUN chown -R 1001:1001 /opt/app-root
# Set the default user for the image, the user itself was created in the base image
USER 1001
# Specify the ports the final image will expose
EXPOSE 8080
# Set the default CMD to print the usage of the image, if somebody does docker run
CMD ["usage"]
4. 定义 s2i/bin/assemble
内容如下:
#!/bin/bash -e
#
# S2I assemble script for the 'lighttpd-centos7' image.
# The 'assemble' script builds your application source ready to run.
#
# For more information refer to the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
echo "---> Installing application source"
cp -Rf /tmp/src/. ./
说明:默认情况下,s2i 构建过程会把应用源代码放在 /tmp/src 目录下。
如果要修改源代码放置目录,可以指定 io.openshift.s2i.destination label 或者在命令行增加 --destination 参数。
这里的当前目录 ./ 表示的是 openshift/base-centos7 的工作目录,即 /opt/app-root/src。
5. 定义 s2i/bin/run
内容如下:
#!/bin/bash -e
#
# S2I run script for the 'lighttpd-centos7' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
exec lighttpd -D -f /opt/app-root/etc/lighttpd.conf
6. 定义 s2i/bin/usage
内容如下:
#!/bin/bash -e
cat <<EOF
This is the lighttpd-centos7 S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image
Sample invocation:
s2i build https://github.com/soltysh/sti-lighttpd.git --context-dir=test/test-app/ lighttpd-centos7 sample-app
You can then run the resulting image via:
docker run -p 8080:8080 sample-app
EOF
7. 创建 etc/lighttpd.conf,Dockerfile 中要复制此文件
内容如下:
# directory where the documents will be served from
server.document-root = "/opt/app-root/src"
# port the server listens on
server.port = 8080
# default file if none is provided in the URL
index-file.names = ( "index.html" )
# configure specific mimetypes, otherwise application/octet-stream will be used for every file
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)
说明:这是一个很简单的 lighttpd 配置文件,配置了如下信息:
(1)服务内容路径:/opt/app-root/src
(2)监听端口:8080
(3)默认欢迎文件:index.html
(4)支持的 mimetype 映射类型
8. 定义 test/test-app/index.html
内容如下:
<!doctype html>
<html>
<head>
<title>test-app</title>
</head>
<body>
<h1>Hello from lighttpd served index.html!</h1>
</body>
</html>
9. 为了能够安装 lighttpd,下载 CentOS7 repo
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
10. 构建 lighttpd builder image
make build
或者
docker build -t lighttpd-centos7 .
11. 使用 lighttpd builder image 构建应用镜像
s2i build test/test-app/ lighttpd-centos7 sample-app
12. 启动应用镜像
docker run -p 8080:8080 sample-app
访问 http://localhost:8080,显示 Hello from lighttpd served index.html!
至此,说明 lighttpd builder image 构建成功!
参考文献:
1. https://blog.openshift.com/create-s2i-builder-image/
本文构建一个 lighttpd builder image,比较简单,旨在了解构建 S2I Builder Image 的主要步骤和原理。
以下操作均在 MAC 机器上执行。
1. 下载并安装 S2I 工具
下载地址:https://github.com/openshift/source-to-image/releases
下载介质:source-to-image-v1.1.5-4dd7721-darwin-amd64.tar.gz
安装
tar zxvf source-to-image-v1.1.5-4dd7721-darwin-amd64.tar.gz
mv s2i sti /usr/local/bin/
2. 创建 S2I 目录结构
s2i create lighttpd-centos7 s2i-lighttpd
说明:这里 lighttpd-centos7 是 builder image 名称,s2i-lighttpd 是目录名称。
在 s2i-lighttpd 目录下有:
(1)Dockerfile – 定义 builder image
(2)Makefile – 包含构建 builder image 和测试 builder image 的脚本
(3)test/
(3.1)run – 测试脚本
(3.2)test-app/ – 要测试应用
(4).s2i/bin/
(4.1)assemble – 构建应用的脚本
(4.2)run – 运行应用的脚本
(4.3)save-artifacts – 增量构建的脚本
(4.4)usage – 如何使用 builder image 的帮助脚本
3. 定义 Docker file
内容如下:
# We are basing our builder image on openshift base-centos7 image
FROM openshift/base-centos7
# Inform users who's the maintainer of this builder image
MAINTAINER Ma Ping <pma@redhat.com>
# Inform about software versions being used inside the builder
ENV LIGHTTPD_VERSION=1.4.45
# Set labels used in OpenShift to describe the builder images
LABEL io.k8s.description="Platform for serving static HTML files" \
io.k8s.display-name="Lighttpd 1.4.45" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,html,lighttpd"
# Install the required software, namely Lighttpd and
COPY CentOS7-Base-163.repo /etc/yum.repos.d/CentOS7-Base-163.repo
RUN yum install -y epel-release && \
yum install -y lighttpd && \
# clean yum cache files, as they are not needed and will only make the image bigger in the end
yum clean all -y
# Defines the location of the S2I
# Although this is defined in openshift/base-centos7 image it's repeated here
# to make it clear why the following COPY operation is happening
LABEL io.openshift.s2i.scripts-url=image:///usr/local/s2i
# Copy the S2I scripts from ./.s2i/bin/ to /usr/local/s2i when making the builder image
COPY ./.s2i/bin/ /usr/local/s2i
# Copy the lighttpd configuration file
COPY ./etc/ /opt/app-root/etc
# Drop the root user and make the content of /opt/app-root owned by user 1001
RUN chown -R 1001:1001 /opt/app-root
# Set the default user for the image, the user itself was created in the base image
USER 1001
# Specify the ports the final image will expose
EXPOSE 8080
# Set the default CMD to print the usage of the image, if somebody does docker run
CMD ["usage"]
4. 定义 s2i/bin/assemble
内容如下:
#!/bin/bash -e
#
# S2I assemble script for the 'lighttpd-centos7' image.
# The 'assemble' script builds your application source ready to run.
#
# For more information refer to the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
echo "---> Installing application source"
cp -Rf /tmp/src/. ./
说明:默认情况下,s2i 构建过程会把应用源代码放在 /tmp/src 目录下。
如果要修改源代码放置目录,可以指定 io.openshift.s2i.destination label 或者在命令行增加 --destination 参数。
这里的当前目录 ./ 表示的是 openshift/base-centos7 的工作目录,即 /opt/app-root/src。
5. 定义 s2i/bin/run
内容如下:
#!/bin/bash -e
#
# S2I run script for the 'lighttpd-centos7' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
exec lighttpd -D -f /opt/app-root/etc/lighttpd.conf
6. 定义 s2i/bin/usage
内容如下:
#!/bin/bash -e
cat <<EOF
This is the lighttpd-centos7 S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image
Sample invocation:
s2i build https://github.com/soltysh/sti-lighttpd.git --context-dir=test/test-app/ lighttpd-centos7 sample-app
You can then run the resulting image via:
docker run -p 8080:8080 sample-app
EOF
7. 创建 etc/lighttpd.conf,Dockerfile 中要复制此文件
内容如下:
# directory where the documents will be served from
server.document-root = "/opt/app-root/src"
# port the server listens on
server.port = 8080
# default file if none is provided in the URL
index-file.names = ( "index.html" )
# configure specific mimetypes, otherwise application/octet-stream will be used for every file
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)
说明:这是一个很简单的 lighttpd 配置文件,配置了如下信息:
(1)服务内容路径:/opt/app-root/src
(2)监听端口:8080
(3)默认欢迎文件:index.html
(4)支持的 mimetype 映射类型
8. 定义 test/test-app/index.html
内容如下:
<!doctype html>
<html>
<head>
<title>test-app</title>
</head>
<body>
<h1>Hello from lighttpd served index.html!</h1>
</body>
</html>
9. 为了能够安装 lighttpd,下载 CentOS7 repo
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
10. 构建 lighttpd builder image
make build
或者
docker build -t lighttpd-centos7 .
11. 使用 lighttpd builder image 构建应用镜像
s2i build test/test-app/ lighttpd-centos7 sample-app
12. 启动应用镜像
docker run -p 8080:8080 sample-app
访问 http://localhost:8080,显示 Hello from lighttpd served index.html!
至此,说明 lighttpd builder image 构建成功!
参考文献:
1. https://blog.openshift.com/create-s2i-builder-image/
没有评论:
发表评论