1 docker-compose.yaml file

version: '2'
services:

  xxl-job:
    container_name: xxl-job
    image: xuxueli/xxl-job-admin:2.3.1
    ports:
      - 8080:8080
    links:
      - mysql
    depends_on:
      - mysql
    environment:
      PARAMS: '--spring.datasource.url=jdbc:mysql://mysql:3306/xxl_job?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=root --xxl.job.accessToken=dasj2nd81jf9asjdkl5jk234'
    networks:
      - xxl_job_net

  mysql:
    container_name: mysql
    image: mysql
    restart: always
    ports:
      - 13306:3306
    volumes:
      - .:/docker-entrypoint-initdb.d
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: xxl_job
    networks:
      - xxl_job_net

networks:
  xxl_job_net:
    driver: bridge

>>> docker compose up

2 mysql container import sql file
>>> docker exec -i mysql mysql -uroot -proot xxl_job < tables_xxl_job.sql

部署完成之后,查看日志,报错
xxljob docker compose deploy: could not create connection to database server
image.png


解决方案:
1 查询docker mysql ip address

>>> docker inspect mysql | grep IPAddress
# 172.18.0.2

2 替换docker-compose.yaml文件中 PARAMS spring.datasource.url值

--spring.datasource.url=jdbc:mysql://mysql:3306/xxl_job
替换为
--spring.datasource.url=jdbc:mysql://172.18.0.2:3306/xxl_job

3 docker compose up 连接成功

为什么使用容器名mysql报错呢?
https://stackoverflow.com/questions/31149501/how-to-reach-docker-containers-by-name-instead-of-ip-address
参考这个:当使用docker compose时,会在容器名前增加一个前缀,导致无法连接mysql容器
基于此, 我们重新修改yml文件中PARAMS mysql -> xxljob-mysql-1

      PARAMS: '--spring.datasource.url=jdbc:mysql://xxljob-mysql-1:3306/xxl_job?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=root --xxl.job.accessToken=dasj2nd81jf9asjdkl5jk234'

运行命令:docker compose -p xxljob up 通过 -p 指定COMPOSE_PROJECT_NAME
访问 http://localhost:8080/xxl-job-admin/ 成功


时二少
1 声望0 粉丝

犹豫一次是思考


引用和评论

0 条评论