This article is a continuation of 1 below.
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.
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.
The following screen may appear when it is executed for the first time. In that case, click "Start CLOUD SHELL".
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.
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).
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])$
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.
Next, prepare the following three files required to build the Docker image.
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
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
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
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
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]
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
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
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
Make sure you are registered in the Container Registry.
that's all
Recommended Posts