[JAVA] 2. Create Docker image and register Registry

Introduction

This article is a continuation of 1 below.

  1. Quickly run Java web module with Google Kubernetes Engine (GKE)
  2. Create Docker image and register registry
  3. Create a database to access from the Web module
  4. Create Manifest and Run Web Module (https://qiita.com/Turtle-child-No2/items/23982059d188e44618df)

2-1. Creating a docker file

Step 2-1.1

GKE gets the Docker image registered in the Container Registry and runs it on the node created in 1 (previous article) (Web module runs). In this article, we will create a Docker image that expands the web module obtained from Github to tomcat. The Docker image is built based on the docker file. Now let's create a dockerfile and build a docker image.

First connect to your Kubernetes cluster. 014.png

Step 2-1.2

The details screen will be displayed. Click the square on the upper right to copy the command to the clipboard. Then click Run in Cloud Shell. 015.png

Step 2-1.3

The following screen may appear when it is executed for the first time. In that case, click "Start CLOUD SHELL". 016.png Google Cloud Shell is a tool that provides command line access to computing resources hosted on Google Cloud Platform. It will be terminated if you close the Cloud Shell session or do nothing for more than an hour, but the home directory is persistent so the next time you launch Cloud Shell, your work will remain.

Step 2-1.4

When the screen below is displayed, press the "Enter" key once to execute the command that has already been entered (the command to obtain the authentication information required to interact with the Kubernetes cluster). 017.png

The command will be executed and the status will be as follows.

Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to [project_id].
Use “gcloud config set project [project_id]” to change to a different project.
[userid]@cloudshell:~ ([projectid])$ gcloud beta container clusters get-credentials standard-cluster-1 --region us-central1 --project [project_id]
Fetching cluster endpoint and auth data.
kubeconfig entry generated for standard-cluster-1.
[userid]@cloudshell:~ ([project_id])$

Step 2-1.5

Next, start vi and create a docker file with the following contents.

[userid]@cloudshell:~ ([project_id])$ vi dockerfile
FROM centos:7
EXPOSE 8080
RUN yum install -y git
RUN yum install -y install unzip
WORKDIR /work
RUN git clone https://github.com/Turtle-child-No2/k8s.git
ADD openjdk-11.0.2_linux-x64_bin.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk-11.0.2
ENV PATH=$PATH:$JAVA_HOME/bin
ADD gradle-5.2.1-bin.tar.gz /usr/local
ENV GRADLE_HOME=/usr/local/gradle-5.2.1
ENV PATH=$GRADLE_HOME/bin:$PATH
RUN mv -T ./k8s/ ./sample-app/
WORKDIR ./sample-app
RUN gradle build --stacktrace
ADD apache-tomcat-9.0.17.tar.gz /opt/
RUN rm -Rf /opt/apache-tomcat-9.0.17/webapps/ROOT/
RUN rm -Rf /opt/apache-tomcat-9.0.17/webapps/docs/
RUN rm -Rf /opt/apache-tomcat-9.0.17/webapps/examples/
RUN rm -Rf /opt/apache-tomcat-9.0.17/webapps/host-manager/
RUN rm -Rf /opt/apache-tomcat-9.0.17/webapps/manager/
RUN mkdir -m 750 /opt/apache-tomcat-9.0.17/webapps/sample-app
RUN unzip -d /opt/apache-tomcat-9.0.17/webapps/sample-app/ /work/sample-app/build/libs/sample-app.war
RUN find /opt/apache-tomcat-9.0.17/webapps/sample-app/ -type d -exec chmod 750 \{\} \;
RUN find /opt/apache-tomcat-9.0.17/webapps/sample-app/ -type f -exec chmod 640 \{\} \;
CMD ["/opt/apache-tomcat-9.0.17/bin/catalina.sh", "run"]

To briefly explain the contents of the dockerfile, first, centos7 is specified as the base image. Next, install the port number to listen to when starting the container and the packages required to get and compile the java source. Then get the java source from github and compile it with gradle. Finally, unzip tomcat, delete unnecessary files, change the permissions of the folder, start tomcat and finish.

Step 2-1.6

Next, prepare the following three files required to build the Docker image.

Step 2-1.7

Download openjdk-11.0.2_linux-x64_bin.tar.gz.

[userid]@cloudshell:~ ([project_id])$ curl -O https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  178M  100  178M    0     0  9686k      0  0:00:18  0:00:18 --:--:-- 9827k

Step 2-1.8

Download apache-tomcat-9.0.17.tar.gz.

[userid]@cloudshell:~ ([project_id])$ curl -O http://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-9/v9.0.17/bin/apache-tomcat-9.0.17.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10.2M  100 10.2M    0     0  2726k      0  0:00:03  0:00:03 --:--:-- 2727k

Step 2-1.9

gradle-5.2.1-bin.tar.gz downloads gradle-5.2.1-bin.zip to use Docker's ADD command, unzips it, and then archives it in tar.gz.

[userid]@cloudshell:~ ([project_id])$ curl -OL https://services.gradle.org/distributions/gradle-5.2.1-bin.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 83.3M  100 83.3M    0     0  81.6M      0  0:00:01  0:00:01 --:--:--  105M
[userid]@cloudshell:~ ([project_id])$ unzip gradle-5.2.1-bin.zip
   creating: gradle-5.2.1/
  inflating: gradle-5.2.1/getting-started.html
  inflating: gradle-5.2.1/LICENSE
   creating: gradle-5.2.1/media/
  inflating: gradle-5.2.1/media/gradle-icon-16x16.png
・
・
・
  inflating: gradle-5.2.1/lib/plugins/junit-platform-commons-1.3.1.jar
  inflating: gradle-5.2.1/lib/plugins/apiguardian-api-1.0.0.jar
  inflating: gradle-5.2.1/lib/plugins/opentest4j-1.1.1.jar
[userid]@cloudshell:~ ([project_id])$ tar -zcvf gradle-5.2.1-bin.tar.gz gradle-5.2.1
gradle-5.2.1/
gradle-5.2.1/NOTICE
gradle-5.2.1/bin/
gradle-5.2.1/bin/gradle.bat
・
・
・
gradle-5.2.1/media/gradle-icon-64x64.png
gradle-5.2.1/media/gradle-icon-128x128.png
gradle-5.2.1/init.d/
gradle-5.2.1/init.d/readme.txt
gradle-5.2.1/getting-started.html

Step 2-1.10

Let's check if the following 4 files are created properly with the ls command.

[userid]@cloudshell:~ ([project_id])$ ls -Fal
total 364340
drwxr-xr-x 7 [userid] [userid]      4096 Mar  9 17:50 ./
drwxr-xr-x 4 root    root         4096 Mar  9 14:38 ../
-rw-r--r-- 1 [userid] [userid]  10783080 Mar  9 17:36 apache-tomcat-9.0.17.tar.gz
-rw------- 1 [userid] [userid]       489 Mar  9 17:51 .bash_history
-rw-r--r-- 1 [userid] [userid]       220 May 16  2017 .bash_logout
-rw-r--r-- 1 [userid] [userid]      3706 Mar  9 14:38 .bashrc
drwxr-xr-x 3 [userid] [userid]      4096 Mar  9 14:38 .config/
drwx------ 2 [userid] [userid]      4096 Mar  9 14:38 .docker/
-rw-r--r-- 1 [userid] [userid]      1168 Mar  9 16:21 dockerfile
drwxr-xr-x 6 [userid] [userid]      4096 Feb  8 20:03 gradle-5.2.1/
-rw-r--r-- 1 [userid] [userid]  87295238 Mar  9 17:51 gradle-5.2.1-bin.tar.gz
-rw-r--r-- 1 [userid] [userid]  87430521 Mar  9 17:39 gradle-5.2.1-bin.zip
drwxr-xr-x 2 [userid] [userid]      4096 Mar  9 14:44 .kube/
-rw-r--r-- 1 [userid] [userid] 187513052 Mar  9 17:33 openjdk-11.0.2_linux-x64_bin.tar.gz
-rw-r--r-- 1 [userid] [userid]       675 May 16  2017 .profile
lrwxrwxrwx 1 [userid] [userid]        38 Mar  9 14:38 README-cloudshell.txt -> /google/devshell/README-cloudshell.txt
drwxr-xr-x 2 [userid] root         4096 Mar  9 14:38 .theia/
-rw------- 1 [userid] [userid]      1006 Mar  9 16:21 .viminfo

2-2. Build docker image

Step 2-2.1

Execute the following command in the directory containing the four files created earlier to create a Docker image, but before that, set the project ID in the environment variable.

[userid]@cloudshell:~ ([project_id])$ export PROJECT_ID="$(gcloud config get-value project -q)"
Your active configuration is: [cloudshell-21915]

Step 2-2.2

Build the Docker image.

[userid]@cloudshell:~ ([project_id])$ docker build --no-cache --rm -t gcr.io/${PROJECT_ID}/sample-app:latest .
Sending build context to Docker daemon  473.3MB
・
・
・
Step 26/26 : CMD ["/opt/apache-tomcat-9.0.17/bin/catalina.sh", "run"]
 ---> Running in 6b1c1d3d8d1f
Removing intermediate container 6b1c1d3d8d1f
 ---> 69ff4ed40a45
Successfully built 69ff4ed40a45
Successfully tagged gcr.io/[project_id]/sample-app:latest

Step 2-2.3

Check the Docker image created by the following command.

[userid]@cloudshell:~ ([project_id])$ docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
gcr.io/[project_id]/sample-app   latest              69ff4ed40a45        32 minutes ago      865MB

2-3. Registry registration of Docker image

Step 2-3.1

GKE gets a Docker image registered in the Container Registry. Register the Docker image created earlier in the Container Registry.

[userid]@cloudshell:~ ([project_id])$ gcloud docker -- push gcr.io/${PROJECT_ID}/sample-app:latest
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.

As an alternative, use `gcloud auth configure-docker` to configure `docker` to
use `gcloud` as a credential helper, then use `docker` as you would for non-GCR
registries, e.g. `docker pull gcr.io/project-id/my-image`. Add
`--verbosity=error` to silence this warning: `gcloud docker
--verbosity=error -- pull gcr.io/project-id/my-image`.

See: https://cloud.google.com/container-registry/docs/support/deprecation-notices#gcloud-docker

The push refers to repository [gcr.io/[project_id]/sample-app]
165d18932f79: Pushed
3903f8c5dac4: Pushed
・
・
・
8294101b3641: Pushed
be99d3b0a3bf: Pushed
0e4c9a7eac59: Pushed
99a6cf5e838e: Pushed
5309f6df6f0b: Pushed
071d8bd76517: Layer already exists
latest: digest: sha256:082443b29ff78220569c1f558442346465fa700ac938f4754e8e506457eacd09 size: 4302

Step 2-3.

Make sure you are registered in the Container Registry. 018.png 019.png that's all

Recommended Posts

2. Create Docker image and register Registry
Create a Docker Image for redoc-cli and register it on Docker Hub
Maybe it works! Create an image with Docker and share it!
Create a lightweight STNS Docker image
Use Jenkins to build inside Docker and then create a Docker image.
Publish Docker Image on GitHub Package Registry
Docker Compact Manual (4: Create a custom image)
Install Docker and create Java runtime environment
Docker image and oleore certificate built-in recipe part 2
Learn more about docker image and Dockerfile FROM
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
CI/CD pipeline and Docker
Steps to push Docker image to GitHub Container Registry (ghcr.io)
Docker terms and commands
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
Delete unused docker image
Until you run Quarkus and run docker image on Amazon ECS
Create a Docker image with the Oracle JDK installed (yum
Register your own Docker image with ECR using AWS CLI
Build Docker Image lightweight and fast with CodeBuild with Santa Banner
Create Rails5 and postgresql environment with Docker and make pgadmin available
Docker settings and this and that
Overview of Docker and containers
[Docker] Create Elasticsearch, Kibana environment!
Docker Compose basics and commands
Install yarn in docker image
Latest docker installation (Ubuntu 20.04 and Ubuntu 20.10)
Extract files from Docker Image
Create table and add columns
Installing and building Docker (memo)
Create a private repository in Amazon ECR and push/pull the image
Docker Image creation for Keycloak extensions and performance testing with Gatling
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Minimal Workflow to push Docker image to Github Container Registry with Github Actions
Image Spring Boot app using jib-maven-plugin and start it with Docker
[GCP] Until you push the local Docker image to the Container Registry
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image