Use Docker to build a Java Web development runtime Environment from scratch __java

Source: Internet
Author: User
Tags docker run
Overview

We talked about some basic concepts and operations of Docker, today we use a simple Java Web example to describe the application of Docker in daily work, this article mainly discusses the following parts: Creating JDK mirrors, creating resin mirrors, launching Web projects. Because the content in this article is based on Dockerfile to create, for readers who are not very familiar with Dockerfile can first familiarize yourself with dockerfile knowledge: https://docs.docker.com/reference/builder/
Creating JDK mirrors

First we'll create a basic image that contains JDK, where we put all the files in the Docker directory and create different directories for different functions.

$ sudo mkdir docker/java
$ sudo cd Docker/java
Next we create a JDK dockerfile file in this directory, as follows:

# OPENJDK 6
# version 1.0 from

ubuntu:14.04
maintainer mhy ' mhy2011@gmail.com '

RUN apt-get update
RUN apt-get install-y-Q openjdk-7-jdk

workdir/

ENV java_home/usr/lib/jvm/java-7-openjdk-amd64
CMD ["/ Bin/bash "]
After creating the Dockerfile, we'll build a mirror of the JDK.

$ sudo docker build-t Pobaby/java.

You can see that a mirror named Pobaby/java has been generated, and then we'll check if the mirror is working properly, start a container from the mirror, and see if the Java command can execute.

$ sudo docker run-it Pobaby/java

You can see the Java command running properly in the boot container, which means that our first JDK has no problems mirroring, and then we're going to create a mirror that contains resin.

Creating resin mirrors

The webserver we use here are resin, and without Tomcat, two mirrors are created in the same way. Resin, like Tomcat, relies on the underlying JDK at startup, so we have two choices when creating a resin mirror: 1, using the mirror image of the Pobaby/java we just created, or 2, using an infrastructure image like Ubuntu to create it. Personal advice is to use the first way to ensure the reuse of resources and avoid unnecessary waste. The specific dockerfile contents are as follows:

# resin
# version 1.0 from

Pobaby/java
maintainer mhy ' mhy2011@gmail.com '

RUN apt-get update
ADD resin-3.1.14.tar.gz/opt/

workdir/opt/

RUN mv resin-3.1.14 resin

expose 8080 entrypoint

["/opt/resin /bin/httpd.sh "]
There's one line about this dockerfile.

ADD resin-3.1.14.tar.gz/opt/
is to add the current directory resin-3.1.14.tar.gz to the container's/opt/directory and extract, of course, can also specify from the network offline (because the company speed is too slow, this is the way to use this).

Next we regenerate into a mirror that contains resin.

$ sudo docker build-t Pobaby/resin.

As you can see, here we have generated a mirror named Pobaby/resin. Next we start a container through the mirror and see if the environment is normal.

$ sudo docker run-d-P 80:8080--name web001 Pobaby/resin

Here we start a container, then map the native 80 port to the container's 8080 port, and then we'll visit the container.

We can see that the resin can start normally, and then it's our most important step. How to deploy a well developed Java Web program into a resin container.
start a Web project

For our web program, because you often need to update your program and package it, the recommended way to do this is to create a basic image that contains webserver, and then launch a container that mounts our Web applications when you start the container, as follows:

$ sudo docker run-d-P 8881:8080--name web-demo-v $PWD/webapps:/opt/resin/webapps pobaby/resin

Here we start a container named Web-demo, and mount the item Web-demo under WebApps under the container/opt/resin/webapps (where we only mount the application and, of course, we can mount the different directories using multiple-v parameters. such as resin configuration file, etc.). Then we visit the application to see if it can be accessed, the following results:


As you can see from above, we have implemented a simple Java Web program based on the Docker container, this program is simpler, there is no database connectivity, and so on, we will improve, and then build a more functional application.

About using Docker to build a Java simple Web development environment here, in view of the limited capacity, if there is a better welcome to correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.