shane0 / cheatsheets

linux, python, js, postgres and some other snippets...
https://shane0.github.io/cheatsheets/
0 stars 0 forks source link

docker #15

Open shane0 opened 1 year ago

shane0 commented 1 year ago

react mongo

In this example, we have two services defined: react-app and mongo-db.

The react-app service:

Builds the React app image using the Dockerfile in the current directory (denoted by .) Maps port 3000 in the container to port 3000 on the host Mounts the current directory as a volume in the container (this allows live reloading of code changes without having to rebuild the image) Sets the NODE_ENV environment variable to development Specifies that it depends on the mongo-db service (so that it won't start until the MongoDB service is running) The mongo-db service:

Uses the mongo image from Docker Hub Maps port 27017 in the container to port 27017 on the host Mounts the ./data directory as a volume in the container (this is where the database files will be stored) Sets the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment variables (these will be used to create a superuser for the database) To run this setup, navigate to the directory containing the docker-compose.yml file and run docker-compose up. This will start both services and you should be able to access the React app at http://localhost:3000.

mongodb://mongo:27017/mydatabase

version: "3"

services:
  react-app:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    environment:
      - NODE_ENV=development
    depends_on:
      - mongo-db

  mongo-db:
    image: mongo:latest
    container_name: mongo-db
    ports:
      - "27017:27017"
    volumes:
      - ./data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=secret
shane0 commented 1 year ago

express

version: '3'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - MONGO_URI=mongodb://db:27017/myapp
    depends_on:
      - db
  db:
    image: mongo:latest
    volumes:
      - ./data:/data/db
shane0 commented 1 year ago

treafik

version: '3.8'

services:
  traefik:
    image: traefik:v2.5
    container_name: traefik
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik:/etc/traefik
    labels:
      - traefik.enable=true
      - traefik.http.routers.traefik.rule=Host(`traefik.yourdomain.com`)
      - traefik.http.routers.traefik.entrypoints=http,https
      - traefik.http.routers.traefik.middlewares=redirect-to-https@file
      - traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https
      - traefik.http.routers.traefik.tls.certresolver=letsencrypt

  app:
    build: .
    container_name: app
    restart: always
    environment:
      - NODE_ENV=production
    labels:
      - traefik.enable=true
      - traefik.http.routers.app.rule=Host(`app.yourdomain.com`)
      - traefik.http.routers.app.entrypoints=https
      - traefik.http.routers.app.tls=true
      - traefik.http.routers.app.tls.certresolver=letsencrypt
      - traefik.http.services.app.loadbalancer.server.port=3000
shane0 commented 1 year ago

treafik kitchen sink

docker exec -it wpcli /bin/bash

version: '3'

services:

  treafik:
    image: traefik:v2.5
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    command:
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --certificatesresolvers.myresolver.acme.tlschallenge=true
      - --certificatesresolvers.myresolver.acme.email=youremail@example.com
      - --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt

  wpcli:
    image: wordpress:cli
    restart: unless-stopped
    volumes:
      - ./wordpress:/var/www/html
    working_dir: /var/www/html

  wordpress:
    image: wordpress:latest
    restart: unless-stopped
    depends_on:
      - mysql
    environment:
      - WORDPRESS_DB_NAME=wordpress
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_HOST=mysql:3306
    volumes:
      - ./wordpress:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.rule=Host(`yourdomain.com`)"
      - "traefik.http.routers.wordpress.entrypoints=websecure"
      - "traefik.http.routers.wordpress.tls=true"
      - "traefik.http.routers.wordpress.tls.certresolver=myresolver"

  mysql:
    image: mysql:5.7
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    volumes:
      - ./mysql:/var/lib/mysql

  express:
    image: node:latest
    restart: unless-stopped
    volumes:
      - ./express:/app
    working_dir: /app
    command: npm start
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.express.rule=Host(`express.yourdomain.com`)"
      - "traefik.http.routers.express.entrypoints=websecure"
      - "traefik.http.routers.express.tls=true"
      - "traefik.http.routers.express.tls.certresolver=myresolver"

  mkdocs1:
    image: squidfunk/mkdocs-material
    restart: unless-stopped
    volumes:
      - ./mkdocs1:/docs
    command: mkdocs serve
    working_dir: /docs
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.mkdocs1.rule=Host(`mkdocs1.yourdomain.com`)"
      - "traefik.http.routers.mkdocs1.entrypoints=websecure"
      - "traefik.http.routers.mkdocs1.tls=true"
      - "traefik.http.routers.mkdocs1.tls.certresolver=myresolver"
shane0 commented 1 year ago

django

version: '3'

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    environment:
      - DJANGO_DEBUG=false
      - DJANGO_SECRET_KEY=secret
      - DJANGO_ALLOWED_HOSTS=example.com,www.example.com
    volumes:
      - .:/code
    expose:
      - 8000
    labels:
      - traefik.enable=true
      - traefik.http.routers.example.rule=Host(`example.com`) || Host(`www.example.com`)
      - traefik.http.routers.example.entrypoints=websecure
      - traefik.http.routers.example.tls=true
      - traefik.http.routers.example.tls.certresolver=le
      - traefik.http.services.example.loadbalancer.server.port=8000
    depends_on:
      - db
    networks:
      - web

  db:
    image: postgres:13-alpine
    environment:
      - POSTGRES_USER=myprojectuser
      - POSTGRES_PASSWORD=myprojectpassword
      - POSTGRES_DB=myproject
    volumes:
      - dbdata:/var/lib/postgresql/data/
    networks:
      - web

  traefik:
    image: traefik:v2.5
    command:
      - "--log.level=INFO"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.le.acme.email=youremail@example.com"
      - "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"
      - "--certificatesresolvers.le.acme.tlschallenge=true"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt
    networks:
      - web

volumes:
  dbdata:

networks:
  web:
shane0 commented 1 year ago

django treafik sqlite

docker exec -it django_container_name sh

version: '3'

services:

  db:
    image: sqlite
    volumes:
      - db-data:/var/lib/sqlite
    networks:
      - traefik

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./app:/app
    environment:
      - DJANGO_SETTINGS_MODULE=app.settings
    labels:
      - "traefik.http.routers.myapp.rule=Host(`myapp.example.com`)"
      - "traefik.http.services.myapp.loadbalancer.server.port=8000"
      - "traefik.enable=true"
      - "traefik.docker.network=traefik"
      - "traefik.http.routers.myapp.tls=true"
      - "traefik.http.routers.myapp.tls.certresolver=lets-encrypt"
    depends_on:
      - db
    networks:
      - traefik

networks:
  traefik:
    external:
      name: traefik
volumes:
  db-data:
    driver: local