The application developed is one from Crud Javaweb with Tomcat and Postgres.
We use only one person's entity as domain.
It is a simple application for didactic use, so they were not addressed
security concepts and data validation for example.
We focus only on creating a Java application container and a container for the
Application database with Docker.
Conceived for resolution of activity of the discipline of POS
Systems Analysis and Development Course
IFPB CAMPUS CAJAZERIAS
Discipline Professor Ricardo Job
First of all get the Docker 
Docker download
But as we are using Docker for deployment you can use only one text editor like Sublime or Notepad ++ ...





If you like the style of letting IDE help you complete the code you can use Netbeans or Eclipse. 

Dockerfile file of the database Within your project Create a directory with the name postgres , and within create a Dockerfile named file, along with two more Create.sql and insert.sql files, we'll talk about them and their content soon.
The dockerfile file had the following content:
From postgres
Env postgres_user postgres
Env postgres_password 12345
Env postgres_db post-client
Copy Create.sql /docker-entrypoint-initdb.d/
Copy insert.sql /docker-entrypoint-initdb.d/
As we realize in the file above, we are setting up Postgres
indicating the user, password and the name of the bank that will be created to receive the data
Application.
In the last two lines we are informing the docker, which after it creates the database
It should read the contents of the two create.sql files that will create the table and insert.sql
that will insert in the post-client bank.
Sensational not?
Create Table Pessoa (
serial id,
Name Character Varying (80) Not null,
CPF Character Varying (14) Not null,
Primary Key (ID)
);
INSERT INTO PERSON (Name, CPF) VALUES ('Kiko', '123.132.121-31');
INSERT INTO PERSON (Name, CPF) VALUES ('Keys', '123.132.121-31');
INSERT INTO PERSON (Name, CPF) VALUES ('Chiquinha', '123.132.121-31');
INSERT INTO PERSON (Name, CPF) VALUES ('MADRUGA', '123.132.121-31');
INSERT INTO PERSON (Name, CPF) VALUES ('Florinda', '123.132.121-31');
docker build -t elefante/banco ./postgres
-t : what tag we will assign to this image
./postgres
* elefante/banco : Image name we have attributed
After you run the command above, if you don't have the image
From Postgres, the docker will arrange for you automatically, of course
This is because we describe this way on dockerfile.
docker image ls
or
docker images
docker run -p 5433:5432 -d --name banco elefante/banco
-p : Bind between the local host door with the container door -d : The container will be executed in a background not obstructing the terminal --name : The Name of the banco Container: Container name
Above we configured the post from Postgres to 5433, which in this case was the port
That I configured in my java application, the door after : is the container door we created.
Dockerfile file FROM tomcat
COPY target/Aplicacao.war ${CATALINA_HOME}/webapps
FROM : Say what image we need
COPY : Say the way from where to copy the .war files for deployment
${CATALINA_HOME}/webapps : place where we will store the glorious files
This Dockerfile file must be within the root directory of your project.
It is noteworthy that the name Aplicacao was the finance I gave to the application
inside the pom.xml.
It is by this name that we will call the system in the browser.
<build>
<finalName>Aplicacao</finalName>
</build>
And of course inside the WEB-INF folder we have to have another directory called lib
that must contain the jstl.jar and standart.jar libraries, otherwise we will have
Problems when carreaging our system in the browser.
docker build -t imagem-da-aplicacao-java .
-t : what tag we will assign to this image
. : relative (or absolute) path to the dockerfile file
After you run the command above, if you don't have the image
from Tomcat, the docker will arrange for you automatically, of course,
This is because we describe this action on the project dockerfile in question.
From Tomcat
Copy Target/Application.War $ {Catalina_Home}/Webapps
docker image ls
or
docker images
docker run -p 8080:8080 -d --name app --link banco:host-banco imagem-da-aplicacao-java
-p : Bind between the local host door with the container door
-d : The container will be executed in background* not obstructing the terminal
--name : The name of the container
--link : For the docker to link the Bank of the Conteiner to the host-tile that referenced in our Java project in the Dbutil.java file
Now go to the browser to open your project: http: // localhost: 8080/application
Above we configured the Tomcat port to 8082 remember?
In my case as I am still using Docker Toolbox on Windows I open the application at http://192.168.99.100:8080/application.war/
To streamline the development process we will create two .sh files:
Run.sh
The Run.sh file must contain the following content:
Docker Build -T Elephant/Bank ./postgres
Docker Run -P 5433: 5432 -D -Name Bank Elephant/Bank
MVN CLEAN PACKAGE
Docker Build -t image-of-the-aja-java.
Docker Run -P 8080: 8080 -D--Name App-Link Bank: Host-Banco Image-Da-A Application-Java
nonrun.sh
docker stop app
Docker Kill App
Docker RM App
Docker RMI -F Image-of-the-Application-Java
docker stop bank
Docker Kill Bank
docker rm bank
Docker RMI -F Elephant/Bank
So since you already have the images and containers created,
It is no longer necessary to type the commands of creating the database image every time,
create the contact container, and then create the image of the web application, create the
The container after each update of your project.
Simply Open Type at Docker:
SH RUN.SH
Will do everything at once:
SH NONRUN.SH
Will do everything at once:
The docker will stop the application container
Docker will kill the container
Remove the application container
You will remove the image from the docker application
The docker will stop the Banco Elefante container
Docker will kill the container
Remove the bank container
Remove the bank image
Maven will clean the project

docker container ls
docker ps -a
docker stop <container_id | container_name>
Docker References
We use Git.