服务器开发学习笔记(七)—— docker容器搭建使用

1、安装docker

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine
#安装一些必要的系统工具:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
#添加软件源信息:

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新 yum 缓存:

sudo yum makecache fast
#安装 Docker-ce:

sudo yum -y install docker-ce
#启动 Docker 后台服务

sudo systemctl start docker
#测试运行 hello-world

docker run hello-world

2、安装docker-compose(curl方法网速有点慢……)

#安装pip
yum install openssl
yum install openssl-devel
yum -y install epel-release
yum -y install python-pip
#确认版本
pip --version
#更新pip
pip install --upgrade pip
sudo pip install --ignore-installed requests
#安装docker-compose
pip install docker-compose 
#查看版本
docker-compose version

2.1 pip 安装异常导致docker-compose异常的原因和解决方案

采用手工安装后通过docker-compose version 显示没有安装(这是由于网络原因导致下周失败)

pip安装后报“ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out”这是由于下载延迟导致,可以采用“pip --default-timeout=200 install -U docker-compose”。

pip安装报类似” pkg_resources.DistributionNotFound: backports.ssl-match-hostname>=3.5”错误,执行“pip install --upgrade backports.ssl_match_hostname”即可完成backports.ssl-match-hostname的更新。

pip安装报类似” Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall” 错误,请” sudo pip install --ignore-installed requests”更新模块,.

pip安装报类似” ipapython 4.5.4 has requirement dnspython>=1.15, but you'll have dnspython 1.12.0 which is incompatible” 错误,请” pip install psutil”

pip安装报类似” Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-Y6TDnU/psutil/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-pVb4Xe/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-Y6TDnU/psutil/”错误,请” yum install python-devel”

出现问题:

File "/usr/lib/python2.7/site-packages/paramiko/ssh_gss.py", line 55, in <module>

GSS_EXCEPTIONS = (gssapi.GSSException,)

AttributeError: 'module' object has no attribute 'GSSException'

解决方法:

ssh_gss.py,53,54行改成:

53 import gssapi.error

54 GSS_EXCEPTIONS = (gssapi.error.GSSException,)

3、整合Springboot+nginx+mysql

为了方便设置, 本次整合为一个容器启动三个springboot程序,因此只有一个dockerfile文件。

项目目录结构如图所示

3.1、Dockerfile文件

因为只是简单使用docker打包为镜像运行,因此Dockerfile文件内容为一个基本镜像,包含maven和jdk

FROM maven:3.5-jdk-8

3.2、docker-compose.yml

Ps:以后复制粘贴的时候记得yml的格式是否正确

version: '3'
services:
  nginx:
    container_name: v-nginx
    image: nginx:1.13
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d

  mysqldb:
    container_name: v-mysql
    image: mysql/mysql-server:5.7
    environment:
      TZ: CST-8
      MYSQL_DATABASE: uka
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_HOST: '%'

    ports:
      - "3306:3306"
    restart: always
   #下面命令设置mysql启动的编码为utf-8
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 


  uka:
    build: .
    working_dir: /uka
    volumes:
      - .:/uka
      - ~/.m2:/root/.m2 #这里是将宿主机的仓库映射到容器当中,及容器使用的仓库于宿主机是同一个仓库
    expose: #暴露端口,但不映射到宿主机,只被连接的服务访问
      - 8002
      - 8003
      - 8004
    ports:
      - 8002:8002
    depends_on:
      - nginx
      - mysql
    command: #此处用shell格式编写,标红处为该语法编写的开头
      - /bin/sh
      - -c
      - |
        mvn clean install -Dmaven.test.skip=true -Pdocker
        cd login-server
        echo "执行登录服务器......"
        mvn spring-boot:run -Dmaven.test.skip=true -Dspring.profiles.active=docker &
        sleep 60
        echo "执行登录服务器完成......"
        cd ../uka-admin/
        echo "执行admin模块......"
        mvn spring-boot:run -Dmaven.test.skip=true -Dspring.profiles.active=docker &
        sleep 60
        echo "执行admin模块完成......"
        cd ../uka-treeknow/
        echo "执行treeknow模块......"
        mvn spring-boot:run -Dmaven.test.skip=true -Dspring.profiles.active=docker &
        sleep 60
        echo "执行treeknow模块完成."

3.3、springboot配置文件

Ps:mysql连接时的路径需要设置为docker-compose.yml里面的mysql服务的名称。docker容器与容器之间通过服务名称进行访问。

认证服务器配置(Login-server)

server:
  port: 8002
  servlet:
    context-path: /server
logging:
  level:
    org.springframework: error

spring:
  application:
    name: loginserver
  datasource:
    name: uka
    url: jdbc:mysql://mysqldb:${MYSQL_PORT:3306}/uka?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
    # druid
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
  liquibase:
    enabled: true
    change-log: classpath:/db/db.changelog-master.xml
mybatis:
  basepackage: team.csrj.uka.**.mapper
  mapper-locations: ["classpath*:mapper/**/*.xml"]

业务服务配置(uka-xxx)

spring:
  application:
    name: uka-admin
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: NON_NULL
  redis:
    host: 127.0.0.1
  datasource:
    name: uka
    url: jdbc:mysql://mysqldb:${MYSQL_PORT:3306}/uka?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
    # druid
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
  liquibase:
    enabled: true
    change-log: classpath:/db/db.changelog-master.xml
  messages:
    encoding: UTF-8
    basename: i18n/abt_messages
    use-code-as-default-message: true
  mvc:
    view:
      suffix: .html
      prefix: /

mybatis:
  basepackage: team.csrj.uka.**.mapper
  mapper-locations: ["classpath*:mapper/**/*.xml"]

logging:
  level:
    team.csrj.uka: debug

server:
  port: 8003
  servlet:
    context-path: /admin



csrj:
  security:
    browser:
      signOutUrl: /index.html


auth-server: http://login-server:8002/server #由于容器与容器的连接是通过服务名,因此这里需要输入认证服务的服务名作为url

security:
  oauth2:
    client:
      client-id: uka
      client-secret: heaven
      user-authorization-uri: http://192.168.1.189/server/oauth/authorize #因为当业务服务重定向到登录服务器的时候,url会变成当前路径,因此不能使用服务名
      access-token-uri: ${auth-server}/oauth/token
    resource:
      jwt:
        key-uri: ${auth-server}/oauth/token_key

docker常用命令:https://www.cnblogs.com/bethal/p/5945015.html

docker-compose常用命令:https://blog.csdn.net/skh2015java/article/details/80410306

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/AlphonesEric/article/details/89145228