42_Docker容器編排

鸟叔书發表於2024-03-13
  1. 下載安裝 docker-compose
curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# curl -L "https://get.daocloud.io/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

# 解除安裝
rm -rf /usr/local/bin/docker-compose
  1. 常用命令
Compose常用命令
docker-compose -h                           # 檢視幫助
docker-compose up                           # 啟動所有docker-compose服務
docker-compose up -d                        # 啟動所有docker-compose服務並後臺執行
docker-compose down                         # 停止並刪除容器、網路、卷、映象.
docker-compose exec  yml裡面的服務id         # 進入容器例項內部  docker-compose exec docker-compose.yml檔案中寫的服務id /bin/bash
docker-compose ps                           # 展示當前docker-compose編排過的執行的所有容器
docker-compose top                          # 展示當前docker-compose編排過的容器程序
docker-compose logs  yml裡面的服務id         # 檢視容器輸出日誌
docker-compose config     # 檢查配置
docker-compose config -q  # 檢查配置, 有問題才有輸出
docker-compose restart    # 重啟服務
docker-compose start      # 啟動服務
docker-compose stop       # 停止服務

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove resources
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show version information and quit
  1. 建立使用者表
CREATE TABLE `t_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL DEFAULT '' COMMENT '使用者名稱',
  `password` varchar(50) NOT NULL DEFAULT '' COMMENT '密碼',
  `sex` tinyint(4) NOT NULL DEFAULT '0' COMMENT '性別 0=女 1=男 ',
  `deleted` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '刪除標誌, 預設0不刪除, 1刪除',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='使用者表'
  1. docker-compose.yml 模板
version: "3"
 
services:
  microService:
    image: zzyy_docker:1.6
    container_name: ms01
    ports:
      - "6001:6001"
    volumes:
      - /app/microService:/data
    networks:
      - atguigu_net
    depends_on: # 依賴於這兩個先啟動
      - redis
      - mysql
 
  redis:
    image: redis:6.0.8
    ports:
      - "6379:6379"
    volumes:
      - /app/redis/redis.conf:/etc/redis/redis.conf
      - /app/redis/data:/data
    networks:
      - atguigu_net
    command: redis-server /etc/redis/redis.conf
 
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'db2021'
      MYSQL_USER: 'zzyy'
      MYSQL_PASSWORD: 'zzyy123'
    ports:
       - "3306:3306"
    volumes:
       - /app/mysql/db:/var/lib/mysql
       - /app/mysql/conf/my.cnf:/etc/my.cnf
       - /app/mysql/init:/docker-entrypoint-initdb.d
    networks:
      - atguigu_net
    command: --default-authentication-plugin=mysql_native_password #解決外部無法訪問
 
networks:
   atguigu_net:

相關文章