Este contenido no está disponible en el idioma seleccionado.
Chapter 4. Creating images
Learn how to create your own container images, based on pre-built images that are ready to help you. The process includes learning best practices for writing images, defining metadata for images, testing images, and using a custom builder workflow to create images to use with OpenShift Container Platform. After you create an image, you can push it to the internal registry.
4.1. Learning container best practices Copiar enlaceEnlace copiado en el portapapeles!
When creating container images to run on OpenShift Container Platform there are a number of best practices to consider as an image author to ensure a good experience for consumers of those images. Because images are intended to be immutable and used as-is, the following guidelines help ensure that your images are highly consumable and easy to use on OpenShift Container Platform.
4.1.1. General container image guidelines Copiar enlaceEnlace copiado en el portapapeles!
The following guidelines apply when creating a container image in general, and are independent of whether the images are used on OpenShift Container Platform.
Reuse images
Wherever possible, base your image on an appropriate upstream image using the
FROM
In addition, use tags in the
FROM
rhel:rhel7
latest
latest
Maintain compatibility within tags
When tagging your own images, try to maintain backwards compatibility within a tag. For example, if you provide an image named
foo
1.0
foo:v1
foo:v1
If you later release an incompatible update, then switch to a new tag, for example
foo:v2
foo:latest
Avoid multiple processes
Do not start multiple services, such as a database and
SSHD
This colocation ensures the containers share a network namespace and storage for communication. Updates are also less disruptive as each image can be updated less frequently and independently. Signal handling flows are also clearer with a single process as you do not have to manage routing signals to spawned processes.
Use exec in wrapper scripts
Many images use wrapper scripts to do some setup before starting a process for the software being run. If your image uses such a script, that script uses
exec
exec
If you have a wrapper script that starts a process for some server. You start your container, for example, using
podman run -i
CTRL+C
exec
podman
exec
podman
Also note that your process runs as
PID 1
PID 1
Clean temporary files
Remove all temporary files you create during the build process. This also includes any files added with the
ADD
yum clean
yum install
You can prevent the
yum
RUN
RUN yum -y install mypackage && yum -y install myotherpackage && yum clean all -y
Note that if you instead write:
RUN yum -y install mypackage
RUN yum -y install myotherpackage && yum clean all -y
Then the first
yum
yum clean
The current container build process does not allow a command run in a later layer to shrink the space used by the image when something was removed in an earlier layer. However, this may change in the future. This means that if you perform an
rm
yum clean
In addition, performing multiple commands in a single
RUN
Place instructions in the proper order
The container builder reads the
Dockerfile
Dockerfile
For example, if you are working on a
Dockerfile
ADD
RUN
yum install
ADD
FROM foo
RUN yum -y install mypackage && yum clean all -y
ADD myfile /test/myfile
This way each time you edit
myfile
podman build
docker build
yum
ADD
If instead you wrote the
Dockerfile
FROM foo
ADD myfile /test/myfile
RUN yum -y install mypackage && yum clean all -y
Then each time you changed
myfile
podman build
docker build
ADD
RUN
yum
Mark important ports
The EXPOSE instruction makes a port in the container available to the host system and other containers. While it is possible to specify that a port should be exposed with a
podman run
Dockerfile
-
Exposed ports show up under associated with containers created from your image.
podman ps -
Exposed ports are present in the metadata for your image returned by .
podman inspect - Exposed ports are linked when you link one container to another.
Set environment variables
It is good practice to set environment variables with the
ENV
Dockerfile
JAVA_HOME
Avoid default passwords
Avoid setting default passwords. Many people extend the image and forget to remove or change the default password. This can lead to security issues if a user in production is assigned a well-known password. Passwords are configurable using an environment variable instead.
If you do choose to set a default password, ensure that an appropriate warning message is displayed when the container is started. The message should inform the user of the value of the default password and explain how to change it, such as what environment variable to set.
Avoid sshd
It is best to avoid running
sshd
podman exec
docker exec
oc exec
oc rsh
sshd
Use volumes for persistent data
Images use a volume for persistent data. This way OpenShift Container Platform mounts the network storage to the node running the container, and if the container moves to a new node the storage is reattached to that node. By using the volume for all persistent storage needs, the content is preserved even if the container is restarted or moved. If your image writes data to arbitrary locations within the container, that content could not be preserved.
All data that needs to be preserved even after the container is destroyed must be written to a volume. Container engines support a
readonly
Explicitly defining volumes in your
Dockerfile
See the Kubernetes documentation for more information on how volumes are used in OpenShift Container Platform.
Even with persistent volumes, each instance of your image has its own volume, and the filesystem is not shared between instances. This means the volume cannot be used to share state in a cluster.
4.1.2. OpenShift Container Platform-specific guidelines Copiar enlaceEnlace copiado en el portapapeles!
The following are guidelines that apply when creating container images specifically for use on OpenShift Container Platform.
Enable images for source-to-image (S2I)
For images that are intended to run application code provided by a third party, such as a Ruby image designed to run Ruby code provided by a developer, you can enable your image to work with the Source-to-Image (S2I) build tool. S2I is a framework that makes it easy to write images that take application source code as an input and produce a new image that runs the assembled application as output.
Support arbitrary user ids
By default, OpenShift Container Platform runs containers using an arbitrarily assigned user ID. This provides additional security against processes escaping the container due to a container engine vulnerability and thereby achieving escalated permissions on the host node.
For an image to support running as an arbitrary user, directories and files that are written to by processes in the image must be owned by the root group and be read/writable by that group. Files to be executed must also have group execute permissions.
Adding the following to your Dockerfile sets the directory and file permissions to allow users in the root group to access them in the built image:
RUN chgrp -R 0 /some/directory && \
chmod -R g=u /some/directory
Because the container user is always a member of the root group, the container user can read and write these files.
Care must be taken when altering the directories and file permissions of sensitive areas of a container, which is no different than to a normal system.
If applied to sensitive areas, such as
/etc/passwd
/etc/passwd
In addition, the processes running in the container must not listen on privileged ports, ports below 1024, since they are not running as a privileged user.
If your S2I image does not include a
USER
0
system:serviceaccount:<your-project>:builder
anyuid
Use services for inter-image communication
For cases where your image needs to communicate with a service provided by another image, such as a web front end image that needs to access a database image to store and retrieve data, your image consumes an OpenShift Container Platform service. Services provide a static endpoint for access which does not change as containers are stopped, started, or moved. In addition, services provide load balancing for requests.
Provide common libraries
For images that are intended to run application code provided by a third party, ensure that your image contains commonly used libraries for your platform. In particular, provide database drivers for common databases used with your platform. For example, provide JDBC drivers for MySQL and PostgreSQL if you are creating a Java framework image. Doing so prevents the need for common dependencies to be downloaded during application assembly time, speeding up application image builds. It also simplifies the work required by application developers to ensure all of their dependencies are met.
Use environment variables for configuration
Users of your image are able to configure it without having to create a downstream image based on your image. This means that the runtime configuration is handled using environment variables. For a simple configuration, the running process can consume the environment variables directly. For a more complicated configuration or for runtimes which do not support this, configure the runtime by defining a template configuration file that is processed during startup. During this processing, values supplied using environment variables can be substituted into the configuration file or used to make decisions about what options to set in the configuration file.
It is also possible and recommended to pass secrets such as certificates and keys into the container using environment variables. This ensures that the secret values do not end up committed in an image and leaked into a container image registry.
Providing environment variables allows consumers of your image to customize behavior, such as database settings, passwords, and performance tuning, without having to introduce a new layer on top of your image. Instead, they can simply define environment variable values when defining a pod and change those settings without rebuilding the image.
For extremely complex scenarios, configuration can also be supplied using volumes that would be mounted into the container at runtime. However, if you elect to do it this way you must ensure that your image provides clear error messages on startup when the necessary volume or configuration is not present.
This topic is related to the Using Services for Inter-image Communication topic in that configuration like datasources are defined in terms of environment variables that provide the service endpoint information. This allows an application to dynamically consume a datasource service that is defined in the OpenShift Container Platform environment without modifying the application image.
In addition, tuning is done by inspecting the
cgroups
cgroup
Set image metadata
Defining image metadata helps OpenShift Container Platform better consume your container images, allowing OpenShift Container Platform to create a better experience for developers using your image. For example, you can add metadata to provide helpful descriptions of your image, or offer suggestions on other images that are needed.
Clustering
You must fully understand what it means to run multiple instances of your image. In the simplest case, the load balancing function of a service handles routing traffic to all instances of your image. However, many frameworks must share information to perform leader election or failover state; for example, in session replication.
Consider how your instances accomplish this communication when running in OpenShift Container Platform. Although pods can communicate directly with each other, their IP addresses change anytime the pod starts, stops, or is moved. Therefore, it is important for your clustering scheme to be dynamic.
Logging
It is best to send all logging to standard out. OpenShift Container Platform collects standard out from containers and sends it to the centralized logging service where it can be viewed. If you must separate log content, prefix the output with an appropriate keyword, which makes it possible to filter the messages.
If your image logs to a file, users must use manual operations to enter the running container and retrieve or view the log file.
Liveness and readiness probes
Document example liveness and readiness probes that can be used with your image. These probes allow users to deploy your image with confidence that traffic is not be routed to the container until it is prepared to handle it, and that the container is restarted if the process gets into an unhealthy state.
Templates
Consider providing an example template with your image. A template gives users an easy way to quickly get your image deployed with a working configuration. Your template must include the liveness and readiness probes you documented with the image, for completeness.
4.2. Including metadata in images Copiar enlaceEnlace copiado en el portapapeles!
Defining image metadata helps OpenShift Container Platform better consume your container images, allowing OpenShift Container Platform to create a better experience for developers using your image. For example, you can add metadata to provide helpful descriptions of your image, or offer suggestions on other images that may also be needed.
This topic only defines the metadata needed by the current set of use cases. Additional metadata or use cases may be added in the future.
4.2.1. Defining image metadata Copiar enlaceEnlace copiado en el portapapeles!
You can use the
LABEL
Dockerfile
Docker documentation for more information on the
LABEL
The label names are typically namespaced. The namespace is set accordingly to reflect the project that is going to pick up the labels and use them. For OpenShift Container Platform the namespace is set to
io.openshift
io.k8s
See the Docker custom metadata documentation for details about the format.
| Variable | Description |
|---|---|
|
| This label contains a list of tags represented as a list of comma-separated string values. The tags are the way to categorize the container images into broad areas of functionality. Tags help UI and generation tools to suggest relevant container images during the application creation process.
|
|
| Specifies a list of tags that the generation tools and the UI uses to provide relevant suggestions if you do not have the container images with specified tags already. For example, if the container image wants
|
|
| This label can be used to give the container image consumers more detailed information about the service or functionality this image provides. The UI can then use this description together with the container image name to provide more human friendly information to end users.
|
|
| An image can use this variable to suggest that it does not support scaling. The UI then communicates this to consumers of that image. Being not-scalable means that the value of
|
|
| This label suggests how much resources the container image needs to work properly. The UI can warn the user that deploying this container image may exceed their user quota. The values must be compatible with Kubernetes quantity.
|
4.3. Creating images from source code with source-to-image Copiar enlaceEnlace copiado en el portapapeles!
Source-to-image (S2I) is a framework that makes it easy to write images that take application source code as an input and produce a new image that runs the assembled application as output.
The main advantage of using S2I for building reproducible container images is the ease of use for developers. As a builder image author, you must understand two basic concepts in order for your images to provide the best S2I performance, the build process and S2I scripts.
4.3.1. Understanding the source-to-image build process Copiar enlaceEnlace copiado en el portapapeles!
The build process consists of the following three fundamental elements, which are combined into a final container image:
- Sources
- Source-to-image (S2I) scripts
- Builder image
S2I generates a Dockerfile with the builder image as the first
FROM
4.3.2. How to write source-to-image scripts Copiar enlaceEnlace copiado en el portapapeles!
You can write source-to-image (S2I) scripts in any programming language, as long as the scripts are executable inside the builder image. S2I supports multiple options providing
assemble
run
save-artifacts
- A script specified in the build configuration.
-
A script found in the application source directory.
.s2i/bin -
A script found at the default image URL with the label.
io.openshift.s2i.scripts-url
Both the
io.openshift.s2i.scripts-url
-
: absolute path inside the image to a directory where the S2I scripts are located.
image:///path_to_scripts_dir -
: relative or absolute path to a directory on the host where the S2I scripts are located.
file:///path_to_scripts_dir -
: URL to a directory where the S2I scripts are located.
http(s)://path_to_scripts_dir
| Script | Description |
|---|---|
|
| The
|
|
| The
|
|
| The
These dependencies are gathered into a
|
|
| The
|
|
| The
Note The suggested location to put the test application built by your
|
Example S2I scripts
The following example S2I scripts are written in Bash. Each example assumes its
tar
/tmp/s2i
assemble script:
#!/bin/bash
# restore build artifacts
if [ "$(ls /tmp/s2i/artifacts/ 2>/dev/null)" ]; then
mv /tmp/s2i/artifacts/* $HOME/.
fi
# move the application source
mv /tmp/s2i/src $HOME/src
# build application artifacts
pushd ${HOME}
make all
# install the artifacts
make install
popd
run script:
#!/bin/bash
# run the application
/opt/application/run.sh
save-artifacts script:
#!/bin/bash
pushd ${HOME}
if [ -d deps ]; then
# all deps contents to tar stream
tar cf - deps
fi
popd
usage script:
#!/bin/bash
# inform the user how to use the image
cat <<EOF
This is a S2I sample builder image, to use it, install
https://github.com/openshift/source-to-image
EOF
4.4. About testing source-to-image images Copiar enlaceEnlace copiado en el portapapeles!
As an Source-to-Image (S2I) builder image author, you can test your S2I image locally and use the OpenShift Container Platform build system for automated testing and continuous integration.
S2I requires the
assemble
run
save-artifacts
usage
The goal of testing an S2I image is to make sure that all of these described commands work properly, even if the base container image has changed or the tooling used by the commands was updated.
4.4.1. Understanding testing requirements Copiar enlaceEnlace copiado en el portapapeles!
The standard location for the
test
test/run
The
test/run
$PATH
S2I combines the application source code and builder image, so to test it you need a sample application source to verify that the source successfully transforms into a runnable container image. The sample application should be simple, but it should exercise the crucial steps of
assemble
run
4.4.2. Generating scripts and tools Copiar enlaceEnlace copiado en el portapapeles!
The S2I tooling comes with powerful generation tools to speed up the process of creating a new S2I image. The
s2i create
Makefile
$ s2i create _<image name>_ _<destination directory>_
The generated
test/run
The
test/run
s2i create
test/test-app
4.4.3. Testing locally Copiar enlaceEnlace copiado en el portapapeles!
The easiest way to run the S2I image tests locally is to use the generated
Makefile
If you did not use the
s2i create
Makefile
IMAGE_NAME
Sample Makefile
IMAGE_NAME = openshift/ruby-20-centos7
CONTAINER_ENGINE := $(shell command -v podman 2> /dev/null | echo docker)
build:
${CONTAINER_ENGINE} build -t $(IMAGE_NAME) .
.PHONY: test
test:
${CONTAINER_ENGINE} build -t $(IMAGE_NAME)-candidate .
IMAGE_NAME=$(IMAGE_NAME)-candidate test/run
4.4.4. Basic testing workflow Copiar enlaceEnlace copiado en el portapapeles!
The
test
If you use Podman, run the following command:
$ podman build -t <builder_image_name>If you use Docker, run the following command:
$ docker build -t <builder_image_name>
The following steps describe the default workflow to test S2I image builders:
Verify the
script is working:usageIf you use Podman, run the following command:
$ podman run <builder_image_name> .If you use Docker, run the following command:
$ docker run <builder_image_name> .
Build the image:
$ s2i build file:///path-to-sample-app _<BUILDER_IMAGE_NAME>_ _<OUTPUT_APPLICATION_IMAGE_NAME>_-
Optional: if you support , run step 2 once again to verify that saving and restoring artifacts works properly.
save-artifacts Run the container:
If you use Podman, run the following command:
$ podman run <output_application_image_name>If you use Docker, run the following command:
$ docker run <output_application_image_name>
- Verify the container is running and the application is responding.
Running these steps is generally enough to tell if the builder image is working as expected.
4.4.5. Using OpenShift Container Platform for building the image Copiar enlaceEnlace copiado en el portapapeles!
Once you have a
Dockerfile
If your OpenShift Container Platform instance is hosted on a public IP address, the build can be triggered each time you push into your S2I builder image GitHub repository.
You can also use the
ImageChangeTrigger