How to build Linux containers with Docker

How to build Linux containers with Docker

Feb 9, 2023
Dmitry Chuyko

A containerization tutorial based on Alpaquita Stream

Linux containers are a powerful solution for

  • Software standardization;
  • Acceleration of development and testing;
  • Effective resource management throughout the whole lifecycle of an application.

We have already discussed the intricacies behind the technology, its advantages, and differences from virtualization in our previous article dedicated to JVM in Linux containers.

Here, we will provide a step-by-step guide to building Linux containers with applications intended for Cloud deployment. As an example, we will use BellSoft’s open-source Alpaquita Stream containers hosted on the Docker Hub Container Image Library.

To complete the guide, you must have the docker daemon installed and running. We will explain how to pull a selected container image from the repository and run it with Java, Python, GCC-based applications, and native images. You can use your own app or create a sample project as shown below.

Linux containers for Java applications

Pull a Docker image

BellSoft provides an extensive selection of Alpaquita Linux images: consult the overview to select an appropriate solution for your application. 

To create a container, run

$ docker image pull docker bellsoft/<repository>:<image_tag>

Start the container with

$ docker run -it --rm bellsoft/<repository>:<image_tag>

Alternatively, utilize a combined command:

$ docker container run --rm -it bellsoft/<repository>:<image_tag>

Suppose you have chosen a Liberica Runtime Container, which includes musl-based Alpaquita Stream (a glibc-based option is also available) and Liberica JDK Lite 11. The command will be as follows: 

$ docker container run --rm -it bellsoft/liberica-runtime-container:jdk-11.0.17-musl

Note that pulling an image is not obligatory, but recommended if you don’t want to pull an image every time you repeat the build process. 

Write a Dockerfile and run the app

By using a JRE image instead of a full JDK to run a Java application, you will reduce the container size by approx. 50%.

So in the Dockerfile for our application, we will specify two images — one for building an app, and another for running it. We will use a standard Spring Petclinic project as a sample.

Write the following Dockerfile:

FROM bellsoft/liberica-runtime-container:jdk-17-stream-musl as builder
WORKDIR /home/myapp
RUN apk add git
RUN git clone
RUN cd spring-petclinic && ./mvnw package
FROM bellsoft/liberica-runtime-container:jre-17-stream-musl
WORKDIR /home/myapp
COPY --from=builder /home/myapp/spring-petclinic/target .
CMD ["java", "-jar", "spring-petclinic-3.0.0-SNAPSHOT.jar"]

Build the app image:

$ docker build --progress plain -t javaappimage .

Now. run the the image:

docker run -p 8080:8080 javaappimage

Done! Open the browser at http://localhost:8080/ to access the Petclinic application.

Linux containers for Native Image

Pull a Docker image

We provide images with Liberica Native Image Kit (NIK), an open-source GraalVM-based utility for converting JVM applications into native executables. Developers who work with native images or consider integrating the technology into their project can pull the following image:

$ docker container run --rm -it bellsoft/liberica-native-image-kit-container:jdk-11-nik-21.3.3-stream-musl

Or any image you deem appropriate depending on the JDK and NIK version and libc implementation (BellSoft’s optimized musl or glibc).

Create a native image

First, let’s write a simple application to be converted into a native executable. Alternatively, use your own project.

public class Example {
    public static void main(String[] args) {
        String str = "Native Image is awesome";
        String reversed = reverseString(str);
        System.out.println("The reversed string is: " + reversed);
    public static String reverseString(String str) {
        if (str.isEmpty())
            return str;
        return reverseString(str.substring(1)) + str.charAt(0);

The Dockerfile must contain the following information:

FROM bellsoft/liberica-native-image-kit-container:jdk-11-nik-21.3.3-stream-musl
WORKDIR /home/myapp
COPY /home/myapp
RUN javac
RUN native-image Example
FROM bellsoft/alpaquita-linux-base:stream-musl
WORKDIR /home/myapp
COPY --from=0 /home/myapp/example .
CMD ["./example"]

Go to the application directory and run

$ docker build .

[+] Building 370.9s (14/14) FINISHED

 => [internal] load build definition from Dockerfile                                                                                   0.1s

 => => transferring dockerfile: 357B                                                                                                   0.0s

 => [internal] load .dockerignore                                                                                                      0.0s

 => => transferring context: 2B                                                                                                        0.0s

 => [internal] load metadata for                                                   0.0s

 => [internal] load metadata for                  4.8s

 => [internal] load build context                                                                                                      0.1s

 => => transferring context: 454B                                                                                                      0.0s

 => [stage-1 1/3] FROM                                                             0.1s

 => [stage-0 1/5] FROM  293.4s

 => => resolve  0.0s

 => => sha256:3c5a09abb2559cf04e7527b81bfa123ca52592ef2bce1512d837fbd0336440e9 951B / 951B                                             0.0s

 => => sha256:79998423fd609bb42881f8eb6721aa22451cdb46363b4fc5f49fc50390759138 2.72kB / 2.72kB                                         0.0s

 => => sha256:4f4293381c77f4d905cfa2034b4fe3adff4515508d08b0093a8db16728ba4879 3.34MB / 3.34MB                                         7.1s

 => => sha256:c3263c13b6f5c1ba5a386dc02047af54fdeefc8a92538ffc973915cdb0de9881 532.42kB / 532.42kB                                     7.0s

 => => sha256:f6c678d36153293dc6b01a73696b4ef60a9ecb44e62da72fd96b31e679f1ed2d 323.95MB / 323.95MB                                   284.4s

 => => extracting sha256:4f4293381c77f4d905cfa2034b4fe3adff4515508d08b0093a8db16728ba4879                                              0.1s

 => => extracting sha256:c3263c13b6f5c1ba5a386dc02047af54fdeefc8a92538ffc973915cdb0de9881                                              0.0s

 => => extracting sha256:f6c678d36153293dc6b01a73696b4ef60a9ecb44e62da72fd96b31e679f1ed2d                                              8.8s

 => [stage-1 2/3] WORKDIR /home/myapp                                                                                                  0.0s

 => [stage-0 2/5] WORKDIR /home/myapp                                                                                                  0.5s

 => [stage-0 3/5] COPY /home/myapp                                                                                        0.0s

 => [stage-0 4/5] RUN javac                                                                                               1.0s

 => [stage-0 5/5] RUN native-image Example                                                                                            70.6s

 => [stage-1 3/3] COPY --from=0 /home/myapp/example .                                                                                  0.1s

 => exporting to image                                                                                                                 0.1s

 => => exporting layers                                                                                                                0.1s

 => => writing image sha256:f03df73515790f3ffe210fc139d5a0752c088fd242f571e19d111377f7703c6a                                           0.0s

Verify the image was created.

$ docker images
REPOSITORY     TAG            IMAGE ID       CREATED              SIZE
<none>         <none>         f03df7351579   About a minute ago   19MB

Tag the newly built image with a meaningful name.

$ docker tag f03df7351579 bellsoft-nik:example-musl
$ docker run -it --rm f03df7351579
The reversed string is: emosewa si egamI evitaN

Linux containers for Python

BellSoft provides Alpaquita Linux images with Python 3.10 and basic Python utilities (pip, setuptools, wheel). Two libc options (BellSoft’s musl perf and glibc) are available.

First, let’s write a simple flask application:

$ cat requirements.txt

$ cat
from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello, Docker!'

Now, write a Dockerfile with the following contents:

FROM bellsoft/alpaquita-linux-python:3.10-stream-musl
WORKDIR /myapp
COPY requirements.txt .
RUN pip3 install -r requirements.txt
ENV PATH=/root/.local:$PATH
CMD ["python3", "-m" , "flask", "--app", "./", "run", "--host="]

Finally, build a Docker image with this Dockerfile:

$ docker build -t flaskimage .

[+] Building 1.3s (10/10) FINISHED

 => [internal] load build definition from Dockerfile                                                                                   0.0s

 => => transferring dockerfile: 38B                                                                                                    0.0s

 => [internal] load .dockerignore                                                                                                      0.0s

 => => transferring context: 2B                                                                                                        0.0s

 => [internal] load metadata for                                            1.2s

 => [internal] load build context                                                                                                      0.0s

 => => transferring context: 63B                                                                                                       0.0s

 => [1/5] FROM  0.0s

 => CACHED [2/5] WORKDIR /myapp                                                                                                        0.0s

 => CACHED [3/5] COPY requirements.txt .                                                                                               0.0s

 => CACHED [4/5] RUN pip3 install -r requirements.txt                                                                                  0.0s

 => CACHED [5/5] COPY .                                                                                                        0.0s

 => exporting to image                                                                                                                 0.0s

 => => exporting layers                                                                                                                0.0s

 => => writing image sha256:9f9335b2d7339a656e1b860e0d0d293c528e06aa21d0be41ee46b67fbb9e9f12                                           0.0s

 => => naming to                                                                                          0.0s

You can now run your Python application in Docker:

$ docker run -d -p 6000:5000 flaskimage
$ curl
Hello, Docker!

Linux containers for GCC

BellSoft provides Alpaquita Linux images with the GCC compiler version 12.2, tools, and libraries for development in C/C++. Two libc options (BellSoft’s musl perf and glibc) are available.

We will use a C++ example to show that our gcc image could be used for building both C++ and C projects.

Our gcc images include the following packages that are not obligatory, but could be useful for building.

autoconf automake bash binutils bzip2 bzip2-dev curl curl-dev diffutils file findutils fortify-headers g++ gawk gcc gdb git grep hexdump jpeg-dev krb5-dev libevent-dev libffi-dev libjpeg-turbo-dev libpng-dev libtool libxml2-dev libxslt-dev make ncurses-dev openssl-dev patch pkgconf readline-dev sed subversion unzip xz xz-dev yaml-dev zlib-dev

First, let’s write a simple C++ program:

$ cat hello.cpp
#include <iostream>
using namespace std;
int main()
    cout << "Hello World" << endl;
    return 0;

In the Dockerfile, specify an Alpaquita image for building an app, and a base image, which will be used for working with the container further on:

FROM bellsoft/alpaquita-linux-gcc:12.2-stream-musl as builder
WORKDIR /home/myapp
COPY hello.cpp .
RUN g++ hello.cpp -o hello -static

FROM bellsoft/alpaquita-linux-base:stream-musl
WORKDIR /home/myapp
COPY --from=builder /home/myapp/hello .
CMD ["./hello"]

Now, build the Docker container image:

$ docker build -t cppappimage .

Run it with the following command:

$ docker run --rm cppappimage
Hello World 


In this article, we learned how to construct Linux containers with Java, Python, C/C++ applications, and Native Image. We used Alpaquita Stream as a base Linux distribution.

As you can see, containerization with Alpaquita is fast and effortless, and in the end, you get a performant microcontainer that can be used as is or further configured for specific purposes.

If you need to add additional Linux packages, take a look at a detailed APK guide. All Alpaquita packages are verified by BellSoft regarding clean licenses to eliminate the risk of license violation, meaning that Alpaquita is legally safe for enterprise use.

Alpaquita Stream is free to use, but BellSoft also provides commercial support for Alpaquita Cloud Native Platform, — a solution based on Alpaquita and Liberica JDK Lite, — which includes

  • Alpaquita LTS versions with minimum four years of bug fixes and updates;
  • 24/7 service;
  • Emergency patches and fixes;
  • Support both for Linux and Java from one vendor.

Click on the button below to learn more about the offer.

Contact us

Subcribe to our newsletter


Read the industry news, receive solutions to your problems, and find the ways to save money.

Further reading