Spring Cloud

快速交付和部署
构建镜像文件搭建开发环境【测试和运维】
启动速度
dockerfile【统一】
高效利用资源
迁移和扩展

镜像、容器、仓库
docker UnionFS 的层级文件系统{container+image}
一个容器放一个服务
-p[端口] -v[存放数据] -link[共享数据]

container –>docker commit–>image–>docker tag–>image–> docker push–>docker hub

查看环境
cat /proc/version:Linux version 3.10.0-327.36.3.el7.x86_64
cat /etc/redhat-release:CentOS Linux release 7.2.1511 (Core)

安装Docker: yum -y install docker

已安装:
docker.x86_64 2:1.12.5-14.el7.centos                                                                                        作为依赖被安装:
container-selinux.x86_64 2:1.12.5-14.el7.centos                 docker-client.x86_64 2:1.12.5-14.el7.centos                 docker-common.x86_64 2:1.12.5-14.el7.centos                     oci-register-machine.x86_64 1:0-1.11.gitdd0daef.el7         oci-systemd-hook.x86_64 1:0.1.4-9.git671c428.el7                skopeo-containers.x86_64 1:0.1.17-1.el7                     更新完毕:
systemd.x86_64 0:219-30.el7_3.6                                                                                             
作为依赖被升级:
libgudev1.x86_64 0:219-30.el7_3.6                                   libselinux.x86_64 0:2.5-6.el7                         libselinux-python.x86_64 0:2.5-6.el7                                  libselinux-utils.x86_64 0:2.5-6.el7                     libsemanage.x86_64 0:2.5-5.1.el7_3                                    libsemanage-python.x86_64 0:2.5-5.1.el7_3               libsepol.x86_64 0:2.5-6.el7                                           policycoreutils.x86_64 0:2.5-11.el7_3                  
policycoreutils-python.x86_64 0:2.5-11.el7_3                          selinux-policy.noarch 0:3.13.1-102.el7_3.13             selinux-policy-targeted.noarch 0:3.13.1-102.el7_3.13                  setools-libs.x86_64 0:3.3.8-1.1.el7                     systemd-libs.x86_64 0:219-30.el7_3.6                                  systemd-python.x86_64 0:219-30.el7_3.6                  systemd-sysv.x86_64 0:219-30.el7_3.6

验证docker是否安装成功:docker version

Client:
 Version:         1.12.5
 API version:     1.24
 Package version: docker-common-1.12.5-14.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      047e51b/1.12.5
 Built:           Mon Jan 23 15:35:13 2017
 OS/Arch:         linux/amd64

Server:
 Version:         1.12.5
 API version:     1.24
 Package version: docker-common-1.12.5-14.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      047e51b/1.12.5
 Built:           Mon Jan 23 15:35:13 2017
 OS/Arch:         linux/amd64

启动docker服务:systemctl start docker

docker操作

1. 查看docker信息(version、info)
# 查看docker版本  $docker version
# 显示docker系统的信息  $docker info

2. 对image的操作(search、pull、images、rmi、history)
# 检索image   $docker search image_name
# 下载image   $docker pull image_name
# 列出镜像列表    $docker images
# 删除一个或者多个镜像    $docker rmi image_name
# 显示一个镜像的历史; $docker history image_name

3.对容器的操作
# 在容器中运行"echo"命令,输出"hello word" $docker run image_name echo "hello word"
# 交互式进入容器中  $docker run -i -t image_name /bin/bash
# 在容器中安装新的程序    $docker run image_name apt-get install -y app_name

实例步骤:

1.创建Dockerfile文件
2.执行docker编译 docker build -t dockertest . 
3.查看编译好的镜像 docker images
4.运行镜像 docker run 
5.查看状态 docker ps -a

Dockerfile

FROM java:8
MAINTAINER lzy
ADD demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE  8080
CMD ["java","-jar","/app.jar"]

docker build -t demo .

Sending build context to Docker daemon  7.68 kB
Step 1 : FROM java:8
Trying to pull repository docker.io/library/java ... 
8: Pulling from docker.io/library/java
75a822cd7888: Pull complete 
57de64c72267: Pull complete 
4306be1e8943: Pull complete 
1e6944bfb718: Pull complete 
3521f2f45ed2: Pull complete 
9c2f0d9b5f90: Pull complete 
8cedcf6d2527: Pull complete 
31f99da7583d: Pull complete 
Digest: sha256:6bfae6cf8902a28d3f0209f5cdbb269032762a94835d331811df33498970aa7e
Status: Downloaded newer image for docker.io/java:8
 ---> a001fc27db5a
Step 2 : MAINTAINER lzy
 ---> Running in c4ca691dc4ee
 ---> ad2d9fb9adc9
Removing intermediate container c4ca691dc4ee
Step 3 : ADD demo-0.0.1-SNAPSHOT.jar app.jar
 ---> a4ef68eeab7f
Removing intermediate container 81c9e4af8cf3
Step 4 : WORKDIR /app/
 ---> Running in c5b96e7f0071
 ---> 4649690c9c7b
Removing intermediate container c5b96e7f0071
Step 5 : EXPOSE 31000
 ---> Running in b6d9162c337c
 ---> 9be50b594447
Removing intermediate container b6d9162c337c
Step 6 : CMD java -jar app.jarr
 ---> Running in 1e97a09b0caf
 ---> 985145e73436
Removing intermediate container 1e97a09b0caf
Successfully built 985145e73436

docker images

REPOSITORY           TAG                 IMAGE ID            CREATED              SIZE
demo                 latest              985145e73436        About a minute ago   643.2 MB
docker.io/java       8                   a001fc27db5a        4 weeks ago          643.2 MB
docker.io/registry   latest              c9bd19d022f6        3 months ago         33.27 MB

docker run -d –name dockertest -p 31000:31000 985145e73436
docker ps -a

CONTAINER ID   IMAGE        COMMAND           CREATED             STATUS          PORTS          NAMES
9871c54c260d  registry "/entrypoint.sh /etc/"   11 weeks ago    Exited (2) 8 weeks ago       suspicious_varahamihira

容器详细信息:docker inspect containerID
查看日志:docker logs containerID
容器开关:docker stop/start/restart container_name
进入容器:docker exec -it containerID /bin/sh

实战:Spring Cloud基于docker
持久层框架:Spring Data JPA
数据库:PostgreSQL
安装软件:
JDK、busybox、PostgreSQL通过镜像安装

docker pull docker.io/java 
docker pull docker.io/progrium/busybox
docker pull docker.io/sameersbn/postgresql

maven

wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
tar vxf apache-maven-3.2.5-bin.tar.gz
mv apache-maven-3.2.5 /usr/local/maven3

Docker-compose

curl -L "https://github.com/docker/compose/releases/download/1.6.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

项目构建

<modules>
        <module>config</module> 配置服务器,为personsome提供配置服务
        <module>discovery</module> 注册服务器
        <module>ui</module> 应用网关
        <module>person</module> person模块
        <module>some</module> some模块
        <module>monitor</module> 监控器
</modules>

执行mvn clean package docker:build -DskipTests

[INFO] Building jar: /home/lzy/ch12/config/target/config-1.0.0-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.2.4.RELEASE:repackage (default) @ config ---
[INFO] 
[INFO] --- docker-maven-plugin:0.2.9:build (default-cli) @ config ---
[INFO] Copying /home/lzy/ch12/config/target/config-1.0.0-SNAPSHOT.jar -> /home/lzy/ch12/config/target/docker/config-1.0.0-SNAPSHOT.jar
[INFO] Copying /home/lzy/ch12/config/src/main/docker/runboot.sh -> /home/lzy/ch12/config/target/docker/runboot.sh
[INFO] Copying /home/lzy/ch12/config/src/main/docker/Dockerfile -> /home/lzy/ch12/config/target/docker/Dockerfile
[INFO] Building image config:1.0.0-SNAPSHOT
Step 1 : FROM java:8
---> a001fc27db5a
Step 2 : VOLUME /tmp
---> Running in 0c1dfc6ca14f
---> 246a3d1a11e7
Removing intermediate container 0c1dfc6ca14f
Step 3 : RUN mkdir /app
---> Running in b3d7b2a52dc8
---> 13f7c1702e5c
Removing intermediate container b3d7b2a52dc8
Step 4 : ADD config-1.0.0-SNAPSHOT.jar /app/app.jar
---> 5f6a6604be1b
Removing intermediate container a3ebe00ec06c
Step 5 : ADD runboot.sh /app/
---> af433048eba4
Removing intermediate container 8eb8ebf4ed01
Step 6 : RUN bash -c 'touch /app/app.jar'
---> Running in 2c1559eb2176
---> 8c5faf2cbe8d
Removing intermediate container 2c1559eb2176
Step 7 : WORKDIR /app
---> Running in 2696b17fc82f
---> cbca7f50aa58
Removing intermediate container 2696b17fc82f
Step 8 : RUN chmod a+x runboot.sh
---> Running in c2e27fdf3086
---> 2c314a148e6a
Removing intermediate container c2e27fdf3086
Step 9 : EXPOSE 8888
---> Running in bd28dca5fdf8
---> c51e1a868357
Removing intermediate container bd28dca5fdf8
Step 10 : CMD /app/runboot.sh
---> Running in 691b2a4f2501
---> 4c2689ce9c54
Removing intermediate container 691b2a4f2501
Successfully built 4c2689ce9c54

docker images

REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
monitor                          1.0.0-SNAPSHOT      bf8192fc903e        18 hours ago        701.6 MB
some                             1.0.0-SNAPSHOT      772711be95dd        19 hours ago        691.9 MB
person                           1.0.0-SNAPSHOT      9df400157224        19 hours ago        719.6 MB
ui                               1.0.0-SNAPSHOT      eb44b851d3bc        19 hours ago        705.4 MB
discovery                        1.0.0-SNAPSHOT      310e98431587        19 hours ago        716.6 MB
config                           1.0.0-SNAPSHOT      4c2689ce9c54        19 hours ago        701.7 MB
docker.io/sameersbn/postgresql   latest              409a9e95e432        9 days ago          234 MB
lzydome                          latest              2968058d1eb0        2 weeks ago         656.7 MB
docker.io/mysql                  latest              f3694c67abdb        3 weeks ago         400.1 MB
docker.io/java                   8                   a001fc27db5a        8 weeks ago         643.2 MB
docker.io/registry               latest              c9bd19d022f6        3 months ago        33.27 MB
docker.io/progrium/busybox       latest              05cb04a8038f        18 months ago       4.786 MB

编写docker-compose.yml

postgresdb:
  image: busybox
  volumes:
    - /var/lib/postgresql/data

postgres:
#  name: postgres
  image: docker.io/sameersbn/postgresql
  hostname: postgres
  volumes_from:
    - postgresdb
  ports: 
   - "5432:5432"
  environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres

discovery:
  image: "discovery:1.0.0-SNAPSHOT"
  hostname: discovery
#  name: discovery
  ports:
   - "8761:8761"

config:
  image: "config:1.0.0-SNAPSHOT"
  hostname: config
#  name: config
  links:
    - discovery
  environment:
     EUREKA_HOST: discovery
     EUREKA_PORT: 8761
#  ports:
#    - "8888:8888"

person:
  image: person:1.0.0-SNAPSHOT
  hostname: person
  links:
    - discovery
    - config
    - postgres
  environment:
     EUREKA_HOST: discovery
     EUREKA_PORT: 8761
     SPRING_PROFILES_ACTIVE: docker
#  ports:
#    - "8082:8082"

some:
  image: some:1.0.0-SNAPSHOT
  hostname: some
  links:
    - discovery
    - config
  environment:
     EUREKA_HOST: discovery
     EUREKA_PORT: 8761
     SPRING_PROFILES_ACTIVE: docker
#  ports:
#    - "8083:8083"

ui:
  image: ui:1.0.0-SNAPSHOT
  hostname: ui
  links:
    - discovery
    - config
    - person
    - some
  environment:
     EUREKA_HOST: discovery
     EUREKA_PORT: 8761
     SPRING_PROFILES_ACTIVE: docker
  ports:
    - "80:80"

monitor:
  image: monitor:1.0.0-SNAPSHOT
  hostname: monitor
  links:
    - discovery
    - config
    - person
    - some
    - ui
  environment:
     EUREKA_HOST: discovery
     EUREKA_PORT: 8761
     SPRING_PROFILES_ACTIVE: docker
#  ports:
#    - "8989:8989"

执行docker-compose up
docker ps

19504ba16c8a  monitor:1.0.0-SNAPSHOT         "/bin/sh -c /app/runb"  8989/tcp                ch12_monitor_1
8b01980929b6  ui:1.0.0-SNAPSHOT              "/bin/sh -c /app/runb"  0.0.0.0:80->80/tcp      ch12_ui_1
bc9184ff0aa0  some:1.0.0-SNAPSHOT            "/bin/sh -c /app/runb"  8083/tcp                ch12_some_1
0b89fc3ac9b0  person:1.0.0-SNAPSHOT          "/bin/sh -c /app/runb"  8082/tcp                ch12_person_1
c506e62c99ce  config:1.0.0-SNAPSHOT          "/bin/sh -c /app/runb"  8888/tcp                ch12_config_1
bfe6647f9a50  discovery:1.0.0-SNAPSHOT       "/bin/sh -c /app/runb"  0.0.0.0:8761->8761/tcp  ch12_discovery_1
525ce326a93a  docker.io/sameersbn/postgresql "/sbin/entrypoint.sh"   5432/tcp                ch12_postgres_1

这里写图片描述
这里写图片描述
备注

启动postgres
docker run --name yourappname -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -d -p 5432:5432 postgres
异常:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "snsbuy"
解决:
进入容器:docker exec -it containerID /bin/sh
进入目录:/var/lib/postgresql/9.6/main
修改pg_hba.conf MD5改成trust
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值