Skip to content

thanawat1303/swarm02

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 

Repository files navigation

swarm02 fastapi

Ref awaresome-compose

Wakatime project

Url fastapi app

Step on Work

  1. Create folder swarm02 and Open folder on VScode

  2. Create folder app

  3. Create Image from Dockerfile

  4. Create docker-compose.yml spcn19fastapi

    Show code
    version: '3.3' #version compose must than 3 
    services: 
      api: #name application
        image: thanawat1303/fastapi-main:v1 #image service on dockerhub
        networks: #network in service
        - webproxy #network traefik
        environment: #environment application
          PORT: 8000 
        logging:
          driver: json-file #type file 
        volumes: #mount data volume of container
          - /var/run/docker.sock:/var/run/docker.sock
          - app:/app #"path data on host" : "path data on container"
        restart: 'no'
        deploy: #set deploy for swarm
          replicas: 1 #set amount worker want deploy container
          labels: #set labels application connect Traefik
            - traefik.docker.network=webproxy #name network of Traefik
            - traefik.enable=true #status of connect
            - traefik.constraint-label=webproxy #select traefik want container working
            - traefik.http.routers.spcn19fastapi-https.entrypoints=websecure #set position when have request to traefik
            - traefik.http.routers.spcn19fastapi-https.rule=Host("spcn19fastapi.xops.ipv9.xyz") #set domain access to application
            - traefik.http.routers.spcn19fastapi-https.tls.certresolver=default #set certresolver
            - traefik.http.services.spcn19fastapi.loadbalancer.server.port=8000 #set balance when request to port on container
            - traefik.http.routers.spcn19fastapi-https.tls=true #set status Protocal TLS
          resources: #set space that want of Container
            reservations: #set low space
              cpus: '0.1' 
              memory: 10M
            limits: #set high space
              cpus: '0.4'
              memory: 250M
              
    networks: #set networks outside container
      webproxy: #service network revert proxy on cluster
        external: true
    volumes: #volumes on host of Docker
      app:
  5. Show file structure in work
    |__.app
        |__.Dockerfile
        |__.main.py
        |__.requirements.txt
    |__.docker-compose.yml
  6. Push file to github swarm02

  7. Open https://portainer.ipv9.me/

  1. Click Cluster Xopx.ipv9.xyz on Portainer
  1. Click menu Stack on Cluster Xopx.ipv9.xyz
  1. Click button Add Stack
  1. Click Build medthod is Repository
  • Name = name Stack
  • Repository URL = https://github.com/thanawat1303/swarm02
  • Repository reference = refs/heads/main
  • Compose path = name Compose file
  • Automatic updates = enable
    • Fetch interval = time check change on compose file from github
  1. Click button Deploy the stack

Create Image from Dockerfile

  1. Create main.py in /app

    Show code
    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    def hello_world():
        return {"message": "ผมรักวิชานี้ SPCN19"}
  2. Create requirements.txt in /app

    Show code
    fastapi
    uvicorn
  3. Create Dockerfile in /app

    Show code
    FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim AS builder #image container
    
    WORKDIR . #Set path working command on container
    
    COPY requirements.txt ./ #Copy file on host to container
    RUN --mount=type=cache,target=/root/.cache/pip \
        pip install -r requirements.txt #run command on container
    
    COPY . ./app/ #Copy file on host to container
    
    FROM builder as dev-envs
    
    RUN <<EOF
    apt-get update
    apt-get install -y --no-install-recommends git
    EOF
    
    RUN <<EOF
    useradd -s /bin/bash -m vscode
    groupadd docker
    usermod -aG docker vscode
    EOF
    # install Docker tools (cli, buildx, compose)
    COPY --from=gloursdocker/docker / /
  4. Build image from Dockerfile

    docker build <path dockerfile> -t <usernameDockerHub>/<repo>:<tag> #thanawat1303/fastapi-main:v1
    
  5. Push image to DockerHub

    docker push <image ID> <usernameDockerHub>/<repo>:<tag> #thanawat1303/fastapi-main:v1