[Docker] CENTOS7 Environment JAVA Foundation Docker Mirror Construction __java

Source: Internet
Author: User
Tags docker ps docker cp docker run
It 's written in front .

For the first docker of children's shoes, building a basic image may not be able to do. This article takes a Java example to make a brief record of the process of Docker the underlying image of the architecture.
You can also use Dockerfile to create mirrors. This article focuses on creating a mirror using the most basic customization method. Environmental Requirements: CENTOS7 installation Docker CE version. See here if Docker mirroring download is slow, refer to Docker China's official image to accelerate the building of a Java mirroring environment

Starts a Docker instance with CentOS as the base image. Attach into this environment:

sudo docker run-it-v <your-local-volumn-dir>:<docker-volumn-dir> Centos/bin/bash

The related parameters include:
-I: Represents running a container in interactive mode
-T: Indicates that the container will enter its command line when it starts
-V: Indicates which directory needs to be mounted to the container, in the format-v< host directory >:< container directory >
Assuming that all of our installers (such as the Java installation package, the MAVEN installation package, etc.) are placed in the host's/root/software/directory and now need to be mounted under the container's/mnt/software/directory, you can write the-v section of the above command:
-v/root/software/:/mnt/software/
Now, you can install the software for this container.
Another: Docker transfer file command
Docker CP More.log <docker id>:/tmp/

Installing the Java Environment
Install the JDK.
2.1 Download the required JDK compression package from the Oracle website. Enter the directory where the software is specified at Docker startup, unzip the JDK, and then rename the folder MV jdk1.8.0_73/jdk/
2.2 Configuring environment variables (important)
Vim ~/.BASHRC
Add the following configuration at the end of the file:

Export  java_home=/usr/local/jdk
export  path= $PATH: $JAVA _home

Open/etc/profile with a text editor and add at the end of the profile:

Export JAVA_HOME=/USR/LOCAL/JDK
export jre_home= $JAVA _home/jre
export path= $JAVA _home/bin: $PATH 
Export classpath= $CLASSPATH:.: $JAVA _home/lib: $JAVA _home/jre/lib

Save the file and exit. Use the source command to have the environment variable take effect:

$ source ~/.BASHRC
$ source/etc/profile

Uploading Docker mirrors
Quit the Docker instance you just configured:

$ exit

Use the following command to see the Docker instance just exited (this command is similar to the Linux ps-a to view all Docker instances, including running and stopping):

$ docker Ps-a

You can see that the Docker ID of the Docker instance that you just stopped is assumed to be 89c153ff574e. Use the following command to create a new image based on a docker ID:

Docker commit 89c153ff574e java-base:1.8

The container ID is 89c153ff574e, and the mirror name created is java-base:1.8
Submitted a new mirror you can store the tar package with this image:

Docker-o ~/java-base.tar  Java-base

Docker save-o saved directory mirror name

viewing mirrors
Use the Docker images to see all of the current mirrors and see the mirrors you just submitted.
With Docker push <your-image-name:TAG> you can submit mirrors to Dockerhub. (need to register first)

After you have done this, you will have a CentOS JAVA8 environment for the operating system. You can build your project and project on this basis. Build Your own project

Using the above java8 Docker mirrors to build the project, essentially with the above image as the underlying mirror, package your project jar, startup command (assumed to be shell script) to build a new mirror. You can use Dockerfile in the packaging mode.
This practice may be considered:
Place the project jar package under a mapping path (for example,/root/software/above), in the startup script, copy the jar package to the work path, and then start. This ensures that the base mirror for your project is sure to run (because there is an initial jar package under the working path when the base mirror was originally created), and subsequent updates require that only the project jar bundle under the host's mapping path be updated.
Sample dockerfile:

From <your-repository>/java-base:1.8

workdir/var/project

COPY  /home/docker1/certs/var/prog/ Certs
COPY  /home/docker1/project/mnt/software/project

expose 8080

CMD ["/bin/bash", "./start_ Api.sh "]

start_api.sh Example:

#!/bin/bash
cp/mnt/software/project/*.
/USR/LOCAL/JDK/BIN/JAVA-CP Your-project.jar Com.your.project.Application &

For the simple difference between add and copy in Dockerfile, see starting the project here

Command line start:

Docker run-d-P 10000:8080-v/home/docker1/project:/var/project-v/home/docker1/certs:/var/project/certs < Your-repository>/<your-image-name>/var/project/start_api.sh

Parameter explanation:
-D: Indicates background running. Used to start a service, such as a back-end server, a Web server, and so on.
-P <LOCAL-PORT>:<DOCKER-INSIDE-PORT>: Specifies that the port of the host is mapped to the port within the Docker. This allows access to the corresponding service within the Docker by accessing the port of the host.
The last parameter is the instruction to execute at startup. This assumes that the script was started by a shell script, which is specified to run.
When using a script to start, you need to be aware that if the first line of #!/bin/bash is lost, an error will be made.

Using Docker-compose
A more convenient way to start is to use the Docker-compose command. First create an empty docker-compose.yml file, for example:

Version: ' 2 '
services:
<your-service-name>:
    Image: "<your-repository>/< Your-image-name> "
    Ports:
        -" 10000:8080 "
    volumes:
        -/home/docker1/project:/var/project
        -/ Home/docker1/certs:/var/project/certs

In the path where Docker-compose.yml is located, use the command:

$ docker-compose

You can automatically start the Docker instance according to the configuration in Docker-compose.yml.

If the project needs to add a custom hosts to the Docker, modify the/etc/hosts file.
Command line startup mode:
Use the--add-host <your-host-name>:<your-host-ip> option in the Docker Run command to increase. If there are more than one, write multiple--add-host
Docker-compose Way:
Use Extra_hosts to specify. Example:

Version: ' 2 '
services:
<your-service-name>:
    Image: "<your-repository>/< Your-image-name> "
    ...
    Extra_hosts:
        -"host1:1.2.3.4"
        -"host2:1.2.3.5"
    ...

For project needs, build maven mirrors based on java8 mirrors for packaging. The next article will be recorded in detail.

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.