Containerize Your Spring Boot Application


 


docker inspect postgres_db

172.18.0.2

rm -rf CAPEcommerceApplication

mvn install -DskipTests

To remove all Docker images at once, you can use the following command:docker rmi $(docker images -q)


=========================

sudo su -

yum install -y docker

systemctl start docker 

systemctl enable docker

 


curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose



sudo chmod +x /usr/local/bin/docker-compose


sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose


sudo yum update

//sudo yum install maven

==================================================

To install Maven version 3.2.5 on Linux, you can follow these steps:


wget http://mirror.olnevhost.net/pub/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz

tar xzf apache-maven-3.2.5-bin.tar.gz

sudo mv apache-maven-3.2.5 /opt/

export PATH=/opt/apache-maven-3.2.5/bin:$PATH

source ~/.bashrc


==================================================

mvn -version


sudo yum install git

git clone https://github.com/Haneesh55/CAPEcommerceApplication.git

cd CAPEcommerceApplication

mvn clean

mvn install


=================================

vi Dockerfile :

-----------------

 

FROM openjdk:17 

EXPOSE 9090

ADD target/EMS-0.0.1-SNAPSHOT.jar app.jar

ENTRYPOINT ["java", "-jar", "app.jar"]

===================================


=============================

vi docker-compose.yml :

---------------------

version: "3.8"

services:

  postgres:

    image: postgres:latest

    container_name: postgres_db

    restart: always

    environment:

      POSTGRES_USER: postgres

      POSTGRES_PASSWORD: root

      POSTGRES_DB: TEST2

    volumes:

      - postgres-data:/var/lib/postgresql/data

  app:

    build: .

    container_name: myapplication

    depends_on:

      - postgres

    environment:

      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/TEST2

      SPRING_DATASOURCE_USERNAME: postgres

      SPRING_DATASOURCE_PASSWORD: root

    ports:

      - "9090:9090"

volumes:

  postgres-data:

===============================================


docker-compose up -d


docker ps 

===================================================================

O/P:CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                                       NAMES

d28628a514c9   capecommerceapplication_app   "java -jar /app.jar"     21 seconds ago   Up 19 seconds   0.0.0.0:8084->8084/tcp, :::8084->8084/tcp   myapplication

e051a561ed84   postgres:latest               "docker-entrypoint.s…"   21 seconds ago   Up 20 seconds   5432/tcp

===================================================================

docker images


===============================================================

O/P:

REPOSITORY                    TAG       IMAGE ID       CREATED              SIZE

capecommerceapplication_app   latest    5bb91513b40e   About a minute ago   522MB

postgres                      latest    680aba37fd0f   2 weeks ago          379MB

openjdk                       17        5e28ba2b4cdb   10 months ago        471MB

=======================================================================

docker logs myapplication


<===== SPRING BOOT APPLICATION STARTED =====>

Tomcat started on port(s): 8080 (http) with context path ''

2023-02-25 16:44:21.749  INFO 1 --- [main] 

com.cg.ecom.ECOMAppApplication: Started ECOMAppApplication in 17.076 seconds 

<====================== THATS IT ==================>



============================================

Docker commands: 


Running containers : docker ps


All containers : docker ps -a


Pull the image : docker pull [image-name]:[tag/version]


Shows the images : docker images


inspect the image : docker inspect [image-id]


create the container out of image : docker create [image-id]


start the container created : docker start [container-id]


instead of pull the image, create the container and start the container you can directly : docker run [image-name]:[tag/version]

docker run = pull + create + start


Options:

-d = detach mode

-P = assigns a random host port to container port


-p = you can choose the host port

hostport:container-port


docker run -d -p 8080:80 nginx

curl http://[IP-Address]:[host-port]



Enter the terminal of running container : docker exec -it [container-id] bash


Just execute the command no need to enter inside container. : docker exec [container-id] command


remove the container : docker rm [container-id]


-f = force


remove all containers : docker rm -f `docker ps -a -q`


environment variables

docker run -d -e key=value [container-id]

to check docker exec [container-id] env


docker volumes

by default docker removes the data when you remove the container, but you can create any folder in the host machine and map it to docker container using -v.


Now even you remove the container, data will not be removed you can attach it to another container again.

mkdir mysql-data

docker run -d -v ~/mysql-data:/var/lib/mysql mysql





Stopping Containers

docker stop <container-id> - Stop a running container with the specified ID.

docker stop $(docker ps -aq) - Stop all running containers on the host.


Removing Containers

docker rm <container-id> - Remove a stopped container with the specified ID.

docker rm $(docker ps -aq) - Remove all stopped containers on the host.


Force Removing Containers

docker rm -f <container-id> - Forcefully remove a container with the specified ID, even if it's still running.

docker rm -f $(docker ps -aq) - Forcefully remove all containers on the host, including running containers.


Removing Images

docker rmi <image-id> - Remove the image with the specified ID.

docker rmi <image-name> - Remove the image with the specified name and tag.

docker rmi $(docker images -aq) - Remove all images on the host.


Force Removing Images

docker rmi -f <image-id> - Forcefully remove the image with the specified ID, even if it's being used by a running container.

docker rmi -f <image-name> - Forcefully remove the image with the specified name and tag, even if it's being used by a running container.

docker rmi -f $(docker images -aq) - Forcefully remove all images on the host, even if they're being used by running containers.




docker run <image> - Start a new container based on the specified image.

docker run -d <image> - Start a new container in the background (detached mode).

docker run -it <image> - Start a new container in interactive mode (with a shell).

You can also specify additional options when starting a container. Here are some examples:


docker run -p <host-port>:<container-port> <image> - Map a host port to a container port.

docker run -v <host-path>:<container-path> <image> - Mount a host directory as a volume in the container.

docker run --name <name> <image> - Specify a name for the container.

Post a Comment

0 Comments