docker-compose实现微服务jar+mysql的容器服务发布(经典版)

一  安装mysql服务

1.1 拉取镜像

1.拉取: docker  pull  mysql:5.7.29

2.查看镜像: docker  images

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第1张图片

1.2 在宿主机创建文件存储mysql

1.创建映射目录:mysql-c5  在/root/export/dockertest 目录下,mkdir  -p  mysql-c5  ;存储mysql信息

 1.3  创建容器

docker run -id --privileged=true \
-p 3306:3306 \
--name=c5_mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7.29

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第2张图片

1.4 进入容器

root@localhost dockertest]# docker exec -it c5_mysql /bin/bash
root@5d6f5def65ce:/#  mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

1.5  远程连接

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第3张图片

二  微服务工程上传并发布成镜像

2.1 微服务工程创建

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第4张图片

配置连接:

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第5张图片

 3.访问

2.2 将jar包上传服务器

上传前配置文件这里改成: ip改成容器中myql的服务名称,参考docker-compose中定义mysql的服务的名称。

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第6张图片

 2.连接配置改成容器中mysql的名称

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第7张图片

3.上传jar包

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第8张图片

2.3 dockerfile文件的编写

注意dockerfile和jar包要放到同一个目录下,这里是在/root/export/dockertest/docker-compose-dir中

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第9张图片

2.编写dockerfile的内容: vi  spt04-dockerfile  

     这里的dockerfile的名称为:spt04-dockerfile  ;内容如下:

#基础镜像使用java
#FROM java:8
FROM openjdk:8
#作者
MAINTAINER ljf
#volume 制定临时文件目录为/tmp,
VOLUME /tmp
#将jar包添加到容器中并更名为spt-docker。jar
ADD  04-spt-mybaitsplus-fenye-1.0-SNAPSHOT.jar 04-spt-fenye.jar
#运行jar包
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","04-spt-fenye.jar"]
#暴露端口8080作为微服务
EXPOSE 8084

2.4 通过dockefile生成镜像

1.注意结尾有一个点。说明 -f 为dockerfile的名称 spt04-dockerfile  ; 04-spt-fenye-master:1.6为镜像名称。

2.打成镜像:  docker build  -f spt04-dockerfile  -t 04-spt-fenye-master:1.6 .

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第10张图片

3. 查看镜像

三  docker-compose 下载安装 

3.1 docker-compose的安装

1.、安装Docker Compose
# Compose目前已经完全支持Linux、Mac OS和Windows,在我们安装Compose之前,需要先安装Docker。下面我 们以编译好的二进制包方式安装在Linux系统中。 
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
2. 设置文件可执行权限 
chmod +x /usr/local/bin/docker-compose
3.查看版本信息 
docker-compose -version

4.卸载Docker Compose
 二进制包方式安装的,删除二进制文件即可
rm /usr/local/bin/docker-compose

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第11张图片

 四 创建docker网络

4.1 创建docker网络

创建网络,让容器之间可以进行互通访问。

1.查看网络   docker network  ls

2.创建网络 docker network create ljf_net

[root@localhost docker-compose-dir]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
fe668cbe4240        bridge              bridge              local
ead95c34bb4d        host                host                local
2452f37044ee        none                null                local
[root@localhost docker-compose-dir]# docker network create ljf_net
d9e1593ce665508aa690393a6976ce4165669cbee93c65645fc6056d8765b737
[root@localhost docker-compose-dir]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
fe668cbe4240        bridge              bridge              local
ead95c34bb4d        host                host                local
d9e1593ce665        ljf_net             bridge              local
2452f37044ee        none                null                local

2 截图

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第12张图片

五 编写docker-compose文件

5.1 编写docker-compose文件

1.jar包,dockerfile,docker-compose 这3个文件,需要在同一级目录下。

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第13张图片

 2.编写docker-compose的内容

1. - /app/microService:/data    不用自己创建,执行后会自动在宿主机和docker自动创建

2. privileged: true   防止容器中mysql没有操作目录的权限而设置

3.networks:
  ljf_net:  

 自己创建,目的让jar包能够访问到myql容器中的服务。

4.通过命令docker-compose config -q  可以判断所写文件格式是否有问题

version: '3'
services:
  microService:
    image: 04-spt-fenye-master:1.6
    container_name: spt01
    ports:
      - "8084:8084"
    volumes:
      - /app/microService:/data
    networks:
      - ljf_net
    depends_on:
      - mysql
  mysql:
    image: mysql:5.7.29
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'test-db'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: '123456'
    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
    privileged: true
    container_name: "mysql07"
    command: --default-authentication-plugin=mysql_native_password #解决外部无法访问
    networks:
      - ljf_net
networks:
  ljf_net:

5.2 执行docker-compose命令一键启动

1)执行前确保服务中docker的服务启动。

1.查看docker的服务: systemctl    status  docker

2.启动docker服务:systemctl start  docker

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第14张图片

 2) 一键启动

[root@localhost docker-compose-dir]# docker-compose up -d
Creating mysql07 ... done
Creating spt01   ... done
[root@localhost docker-compose-dir]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                     PORTS                               NAMES
ac39b0f18163        04-spt-fenye-master:1.6   "java -Djava.secur..."   8 seconds ago       Up 6 seconds               0.0.0.0:8084->8084/tcp              spt01
e8710c0dfb42        mysql:5.7.29              "docker-entrypoint..."   8 seconds ago       Up 7 seconds               0.0.0.0:3306->3306/tcp, 33060/tcp   mysql07
5d6f5def65ce        mysql:5.7.29              "docker-entrypoint..."   20 hours ago        Exited (0) 3 hours ago                                         c5_mysql
14e0070ee163        spt-docker-master:1.6     "java -Djava.secur..."   24 hours ago        Exited (143) 3 hours ago                                       c11
b3ad03aefa87        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5904->5901/tcp              jolly_hawking
341af5f795b4        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5903->5901/tcp              sad_turing
a3857988c03d        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5902->5901/tcp              elastic_hoover
bb9e19cbfd14        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5901->5901/tcp              silly_kalam

5.3 查看容器log日志

[root@localhost docker-compose-dir]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                     PORTS                               NAMES
ac39b0f18163        04-spt-fenye-master:1.6   "java -Djava.secur..."   17 seconds ago      Up 15 seconds              0.0.0.0:8084->8084/tcp              spt01
e8710c0dfb42        mysql:5.7.29              "docker-entrypoint..."   17 seconds ago      Up 16 seconds              0.0.0.0:3306->3306/tcp, 33060/tcp   mysql07
5d6f5def65ce        mysql:5.7.29              "docker-entrypoint..."   20 hours ago        Exited (0) 3 hours ago                                         c5_mysql
14e0070ee163        spt-docker-master:1.6     "java -Djava.secur..."   24 hours ago        Exited (143) 3 hours ago                                       c11
b3ad03aefa87        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5904->5901/tcp              jolly_hawking
341af5f795b4        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5903->5901/tcp              sad_turing
a3857988c03d        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5902->5901/tcp              elastic_hoover
bb9e19cbfd14        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago   0.0.0.0:5901->5901/tcp              silly_kalam
[root@localhost docker-compose-dir]# docker logs spt01

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第15张图片

5.4 登录初始化数据表数据

1.通过navicate连接后,新建表tb_user表和添加数据。

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第16张图片

 2.通过命令进入容器查看

[root@localhost docker-compose-dir]# docker ps -a

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                       PORTS                               NAMES

7c18286e5e68        04-spt-fenye-master:1.6   "java -Djava.secur..."   2 minutes ago       Exited (1) 2 minutes ago                                         spt01

9c79cd96ec6e        mysql:5.7.29              "docker-entrypoint..."   2 minutes ago       Up 2 minutes                 0.0.0.0:3306->3306/tcp, 33060/tcp   mysql07

5d6f5def65ce        mysql:5.7.29              "docker-entrypoint..."   17 hours ago        Exited (0) 3 minutes ago                                         c5_mysql

14e0070ee163        spt-docker-master:1.6     "java -Djava.secur..."   21 hours ago        Exited (143) 3 minutes ago                                       c11

b3ad03aefa87        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago     0.0.0.0:5904->5901/tcp              jolly_hawking

341af5f795b4        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago     0.0.0.0:5903->5901/tcp              sad_turing

a3857988c03d        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago     0.0.0.0:5902->5901/tcp              elastic_hoover

bb9e19cbfd14        xfce-tools-base           "run.sh"                 2 years ago         Exited (255) 2 years ago     0.0.0.0:5901->5901/tcp              silly_kalam

[root@localhost docker-compose-dir]# docker exec -it mysql07 /bin/bash

root@9c79cd96ec6e:/# mysql -uroot -p123456

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 13

Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| test-db            |

+--------------------+

5 rows in set (0.01 sec)

mysql> use test-db;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> use test-db;

Database changed

mysql> show tables;

+-------------------+

| Tables_in_test-db |

+-------------------+

| tb_user           |

+-------------------+

1 row in set (0.00 sec)

mysql>

六 测试 访问

6.1 关闭防护墙

systemctl  status  firewalld

systemctl   stop  firewalld

6.2 进行访问

 代码地址:

docker-compose实现微服务jar+mysql的容器服务发布(经典版)_第17张图片

你可能感兴趣的:(docker,docker,mysql,容器)