Openshift - can I run docker client commands (like

2019-02-19 11:51发布

Working with Docker is easy when you have a docker host. You connect the Docker client with a Docker host (engine). Then the process of building and deploying a (complex) docker image is like this series of commands on a Jenkins build server:

  • Maven clean install => builds your WAR file
  • Docker build => creates a Wildfly image with the WAR application file in it
  • Docker tag => tags the new image
  • Docker push => pushes the image to a docker hub
  • Docker run ==> installs and runs the docker image on the docker host.

Can Openshift Starter work like this? The answer: yes, this Container As A Service (CAAS) option is possible.

COMPLETE SOLUTION:

In the steps below I create a project consisting of 2 images: MySql and SpringBoot/Angular/Hibernate hosted by a Wildfly server. This works of course also for a Spring Boot JAR application.

You start with a local running Docker daemon. I do this via Docker Quickstart Terminal. I use minishift only for local testing - so not in this case.

Step 1: create a project and 1 MySql application.

This can be done without docker via the Openshift web console. You can also use the oc new-proect command.

Step 2: login to the openshift project. In the online console click on the question mark on the top bar (on the right). Select the "command line tools". You can copy the login command to your clipboard via the icon to the right.

$ oc login etc ... (first clipboard-icon, paste the entire command).

Step 3: Login to your docker registry. In this case check the openshift online console.

$ docker login -u `oc whoami` -p `oc whoami --show-token` registry.pro-us-east-1.openshift.com

NOTICE: don't use the port number as the suffix openshift:443 !!

Step 4: Build and tag the image locally or on the build server (with Jenkins).

$ mvn clean install -- which creates the war file. You can call it 'ROOT.war'.

$ docker build -t myproject/mynewapplication:latest .

$ docker tag myproject/mynewapplication registry.pro-us-east-1.openshift.com/myproject/mynewapplication

If you write the name not correct, in a moment you will not be able to push the image. So don't (!) write pro-us-east1. It is pro-us-east-1, etc.

The Dockerfile is in the Maven project folder. The dockerfile could look like:

FROM jboss/wildfly
COPY target/ROOT.war /opt/jboss/wildfly/standalone/deployments/
# CMD - use the default wildfly default start command

A much more memory efficient way is:

FROM openjdk:8-jdk-alpine
ENV JAVA_APP_JAR your.jar
ENV AB_OFF true
EXPOSE 8080
ADD target/$JAVA_APP_JAR /deployments/
CMD ["java","-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-jar","/deployments/your.jar"]

The last settings will improve/balance your memory usage tremenduously. In my case reducing the running container from 600MB+ to around 300MB running fine!

Step 5: Push the image to the internal Openshift repository

$ docker push registry.pro-us-east-1.openshift.com/myproject/mynewapplication

Step 6-A.1: Create an application from an existing docker image via the new-app command. Do this only the first time while creating the application.

$ oc new-app mynewapplication

--> Found image 1233123223 (About an hour old) in image stream "myproject/mynewapplication" under tag "latest" for "mynewapplication"
... This image will be deployed in deployment config "mynewapplication"
--> Creating resources ...
    deploymentconfig "mynewapplication" created
    service "mynewapplication" created
--> Success
    Run 'oc status' to view your app. 

Step 6-A.2: See below for initializing the settings. When deploying an application / web server, create a 'route' so that the client can access the application. Because this has to be done once, setting it up via the console (Applications > Routes) is a good alternative. Example: make your website available only for https with the standard certificate: (1) use target port 8080 (where your http server is running), (2) tick the Secure route option, (3) keep the TLS Termination to Edge. (4) for insecure traffic choose the Redirect option and ... create your route. Wait a while and you have a HTTPS site.

Step 6-B: When updating the image: When you push an existing image, then there will not be an automatic redeploy. You can start a new deploy via the online console or via the command:

$ oc rollout latest dc/mynewapplication-n myproject

To check the results of the deployment:

$ oc status

To check whether e.g. the MySql container is running:

$ oc get pods

Get the name of the Mysql container.

$ oc rsh container-name

And you will see whether you can really access the database.

Remarks: Remark for using Openshift Starter/Original: It is nice that Openshift allows us to experiment with the free Starter version. There are of course a set of restrictions on the usage of the resources. Take care to set the Deploy strategy to 'Recreate'. The 'Rolling' strategy is of course better, but consumes a lot more resources. I set the resource size for the MySql image typically to 420Mi and the Wildfly image to 600Mi. Suggestions are welcome!

2条回答
Luminary・发光体
2楼-- · 2019-02-19 12:14

You can login to the internal image registry of OpenShift and push your image directly to it. You need to tag the image first so it matches the name of the project where pushing it. For more details see:

Once pushed to the internal image registry you can then deploy from that image using oc new-app or the web console.

查看更多
登录 后发表回答