After reading the previous blog post in this series, "Containers, Kubernetes, microservices: Start here", you're now ready to build your first "Hello World" application and run it in a container. For this, we'll be using Java with Spring Boot.
Buildah, Podman, or docker
Which method you use to build and run your container is based on your operating system and tool selection. Because it is safer (it does not require root access), I'm going to use Podman to build and run my container, knowing that the commands used are 100 percent compatible with the docker
command. In fact, you could run alias docker=podman
and you'd not know the difference. So, if you are not using podman, simply use the command docker
in place of every podman
in the following.
Parts For Building
You need the code you're going to run, a file to configure/manage the build process, and the tool (i.e. Podman).
Build Configuration/Management
We'll create a file called "Dockerfile" that contains the steps and information needed to build an image. The build process is done in layers, with the starting point typically being an operating system or, more likely, an OS and framework combination. For example, you need maven and the Java JDK installed. So it's pretty common to start from that point. You can -- and there might be good reasons for this -- start with the operating system and then install the framework all inside your image as you build it. You can also do just that -- start with an OS and add a framework -- and save that image and use it as your base for other images. Yes, like any IT technology, you can make this as simple or as complicated as you like. We'll start with an OS+Framework combination to keep things simple. We'll then copy our code in the image, then run mvn clean package
to build our application. Finally, we'll give the image a command to be executed when someone runs the image in a container. The following file, "Dockerfile" (which I lifted from my colleague Doug Tidwell's Coderland serverless project), does those things: FROM maven:3.5-jdk-8-alpine as builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
FROM openjdk:8-jre-alpine
COPY --from=builder /app/target/helloWorld-*.jar /helloWorld.jar
ENV PORT 3333
CMD ["java","-Dserver.port=${PORT}","-jar","/helloWorld.jar"]
Let's Get Some Code
- Fork or clone the github repository at https://github.com/donschenck/path-to-kubernetes.
- Move into the directory src/java/helloworld.
Let's Build And Run
podman build -t hello-world-java .
podman run -p 3333:3333 hello-world-java
curl http://localhost:3333
The Cycle
- Create the source code
- Create a Dockerfile file
- Build the image
- Run the image in a container
About that Dockerfile
The file “Dockerfile” is used to guide the construction of your image. Here’s a short, step-by-step breakdown:
maven:3.5-jdk-8-alpine as builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
FROM openjdk:8-jre-alpine
COPY --from=builder /app/target/helloWorld-*.jar /helloWorld.jar
EXPOSE 3333
CMD ["java","-Dserver.port=${PORT}","-jar","/helloWorld.jar"]
podman run
or docker run
)Running In A Container
Running the podman run -p 3333:3333 hello-world-java
command starts the image in a container. It code uses port 3333, and it is mapped to the local port 3333. Feel free to experiment with this. It will be attached to your command line; that is, it ties up your terminal while it’s running. You can eliminate this by using the --detach
option in your command. In that case, the container runs in the background. You can see the results of the code by running the curl
command or opening your browser to http://localhost:3333
.
Containerize All The Things
So you know have all the knowledge and tool necessary to run your Java code in a Linux container. Expanding this knowledge to include multiple instances of an application, and/or multiple applications in a cluster, is for the next blog post.
Last updated: June 10, 2019