Step by Step Create Real Docker Sample
Step by Step Create Real Docker Sample
in this tutorial we want to create a simple application with spring-boot, Redis and Postgres related them together and use HAPROXY for load balancing between the application cluster.
Create simple app with spring boot
firstly we write an application for manage two type of DB Postgres and Redis you can find it in github.
after you created your project you should create Dockerfile for it.
FROM maven:3.5.4-jdk-8-alpine as build 
WORKDIR /app
COPY  src /app/src
COPY pom.xml /app 
RUN mvn clean package
FROM openjdk:8-jre-alpine
WORKDIR /app
COPY --from=build /app/target/myapp.jar /app
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/myapp.jar"]
at first section, we use maven for build our application copy src and pom.xml to the container and then create it and at second section we use JRE for running our application because is a smallest appropriate base image for run java application and then copy built jar file to our image and then run it.
Create docker stack
firstly we should create an image from our app in the last section you can see it and push it in your repository because it’s should be accessible from all of our nodes then we should write compose file for our application.
version: "3"
services:
    myredis:
        image: redis:alpine
        networks:
          - myappnet
        volumes:
          - myredisvol:/data
        deploy:
            placement:
                constraints: [node.role == manager]
    mydb:
       image: postgres:10-alpine
       networks:
          - myappnet
       environment:
          POSTGRES_DB: accessdb
          POSTGRES_USER : postgres
          POSTGRES_PASSWORD: postgres
       deploy:
           placement:
               constraints: [node.role == manager]
    myapp:
        image: myapp:1
        ports:
            - 8080
        networks:
          - myappnet
        depends_on:
            - mydb
            - myredis
        environment:
            SERVICE_PORTS: 8080
            POSTGRES_HOST: //mydb/accessdb
            JEDIS_HOST: myredis
        deploy:
            replicas: 2
            update_config:
                parallelism: 1
            restart_policy:
                condition: on-failure
    proxy:
        image: dockercloud/haproxy
        depends_on:
            - myapp
        environment:
           BALANCE: leastconn
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
        ports:
            - 8088:80
        networks:
            - myappnet
        deploy:
            placement:
                constraints: [node.role == manager]
networks:
    myappnet:
volumes:
    myredisvol:
In the first section of our compose file we create service myradis and assign volume (because we want to persist our data) and network to it and then create constraints for it because we want it run in manager node, after that in the second section we create Postgres and set username and password and DB name for that. in the third section, we create our application which depends on other service and set Service ports for using in Haproxy and set 2 replicas for it and then set setting for update. In Haproxy service, we define volumes for it (for work with docker socket) and define expose port 8088 for it and use leastconn algorithm instead of roundrobin because it’s better for recognize error. we use HAPROXY instead of internal load balancing because its better way to handele balancing between application instances and you can configure it. all of our services in the same network because all of them work together.
create and remove stack service
docker stack deploy --compose-file docker-compose.yml myapp
docker stack rm myapp
use full command for work with service
docker stack ls  # for view  
docker service ls # for view services
docker service logs SRVNAME # for view service logs
docker service inspect # show with detail
docker service scale myapp=3