Quantcast
Channel: Other – Michał Szałkowski – Blog
Viewing all articles
Browse latest Browse all 125

Docker and jboss

$
0
0

First we have to download base wildfly images

to do this, execute this command:

sudo docker pull jboss/wildfly

After few minutes, when image will be on your disk, you can check if base wildfly images works correctly

to do this, execute this command:

sudo docker run -it --rm jboss/wildfly

if everything is OK you can go to browser and open wildfly “open page”
but wait! first you have to check the ip address because the standard address “http://localhost:8080/” doesn’t work

check the wildfly address

first you have to check CONTAINER ID of docker container

sudo docker ps

you should receive something like this:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
8f98b781c568        jboss/wildfly       "/opt/jboss/wildfly/b"   About a minute ago   Up About a minute   8080/tcp            agitated_varahamihira

now, when you have CONTAINER ID, you can run this command

sudo docker inspect 8f98b781c568

to display all interesting information about Docker container in JSON format

{
    "Id": "8f98b781c5680f8cc25254e00e30b620fcb109f8fa042ad03d84a62bd005db64",
    "Created": "2015-10-23T21:32:29.873704281Z",
    "Path": "/opt/jboss/wildfly/bin/standalone.sh",
    "Args": [
        "-b",
        "0.0.0.0"
    ],
    "State": {
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 18026,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2015-10-23T21:32:30.058782686Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "d378c79af2ae12420e5e02d38633f683fd2c87b7661206ecc50b5b122111a340",
    "NetworkSettings": {
        "Bridge": "",
        "EndpointID": "df396473030d3e7cbe615a73e89ab047264249db9407ef8e1cbb9d6970c12361",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "HairpinMode": false,

try to find IP in NetworkSettings – > IPAddress, in my case it is 172.17.0.21

having that information you can open Jboss “Open Page” by “http://172.17.0.21:8080/”

this is not the base way

you have to admit, this is not the base way, if you have to searching to find this IP, you can adjust this by

sudo docker run -it --rm -p 8080:8080 jboss/wildfly

by adding this part -p 8080:8080, now, our jboss server is exposed on http://localhost:8080/ awesome!

time for deploy the app

ok we have jboss running, now we have to deploy something, we can do this by Jboss Administration Console.

On the Jboss “Open Page” you will find the link

ah it doesn’t work

why ? because by default (security reasons) jboss doesn’t has any users, you have to create them

creating new docker image with jboss user

1. create directory for example “docker-image-firs”
2. create file “Dockerfile” in that directory
3. add conntent to file

FROM jboss/wildfly:latest
RUN /opt/jboss/wildfly/bin/add-user.sh USER PASSWORD --silent

4. save the file, and execut this command:

sudo docker build -t szalek/jboss/wildfly/user .

Awesome!, do you know what you just did ?
you have just created new docker image

now you can execute this command:

sudo docker run -it --rm -p 8080:8080 -p 9990:9990 szalek/jboss/wildfly/user /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0

now Jboss Administration Console is available, and you can deploy your app

simple JEE app

1. in your IDE create new Maven project
2. use this pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>btbw.pl</groupId>
    <artifactId>docker-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>docker-test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3. create two java class

/src/main/java/pl/btbw/core/MyApp.java

package pl.btbw.core;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class MyApp extends Application {
}

src/main/java/pl/btbw/web/MyCtrl.java

package pl.btbw.web;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("/")
public class MyCtrl {

	@GET
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	public Map<String, Object> doTest() {

		Map<String, Object> map = new HashMap<>();
		map.put("ID", 1);
		map.put("TITLE", "LOREM");

		return map;
	}
}


Viewing all articles
Browse latest Browse all 125

Trending Articles