SpringBoot Docker入门,SpringBoot Docker安装

SpringBoot Docker入门,SpringBoot Docker安装

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/

一、安装Docker

1、查看Linux版本

Java代码  收藏代码
  1. uname -r  

 Docker要求Linux系统的版本不低于3.10,如下:

Java代码  收藏代码
  1. uname -r  
  2.   3.10.0-327.el7.x86_64  

2、安装Docker

Java代码  收藏代码
  1. yum install docker  

安装过程需要输入 y 确定安装

当出现

Complete!

表示Docker安装成功!

3、启动Docker

Java代码  收藏代码
  1. systemctl start docker  

4、查看Docker版本

Java代码  收藏代码
  1. docker -v  

 结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker -v  
  2. Docker version 1.13.1, build 774336d/1.13.1  

或者使用:

Java代码  收藏代码
  1. docker version  

 结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker version  
  2.   
  3. Client:  
  4.  Version:         1.13.1  
  5.  API version:     1.26  
  6.  Package version: <unknown>  
  7.  Go version:      go1.8.3  
  8.  Git commit:      774336d/1.13.1  
  9.  Built:           Wed Mar  17:06:16 2018  
  10.  OS/Arch:         linux/amd64  
  11. Server:  
  12.  Version:         1.13.1  
  13.  API version:     1.26 (minimum version 1.12)  
  14.  Package version: <unknown>  
  15.  Go version:      go1.8.3  
  16.  Git commit:      774336d/1.13.1  
  17.  Built:           Wed Mar  17:06:16 2018  
  18.  OS/Arch:         linux/amd64  
  19.  Experimental:    false  

5、设置Docker开机启动

Java代码  收藏代码
  1. systemctl enable docker  

 结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# systemctl enable docker  
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/  
  3. systemd/system/docker.service.  

6、停止Docker

Java代码  收藏代码
  1. systemctl stop docker  

二、Docker使用

1、搜索Mysql

Java代码  收藏代码
  1. docker search mysql  

2、下载mysql

Java代码  收藏代码
  1. docker pull mysql  

 默认是下载最新版本,如果想下载指定版本的,可以加上tag,tag指的就是版本号,

查看版本可以到docker Hub:https://hub.docker.com/r/library/mysql/tags/

命令如下:

Java代码  收藏代码
  1. docker pull mysql:tag  

 如:

Java代码  收藏代码
  1. docker pull mysql:5.5  

3、更换Dock Hub

官网地址:https://hub.docker.com/explore/

使用docker Hub直接下载会出现超时,如下:

Java代码  收藏代码
  1. net/http: TLS handshake timeout  

 所以需要更换镜像,如下(更改为阿里的镜像):

Java代码  收藏代码
  1. curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s https://04c5r1cy.mirror.aliyuncs.com  

 然后重启docker

Java代码  收藏代码
  1. systemctl restart docker  

 此时会发生错误:

Java代码  收藏代码
  1. Job for docker.service failed because the control process exited with error code. See "systemctl statu  
  2. s docker.service" and "journalctl -xe" for details.  

 原因是修改了镜像,造成/etc/docker/daemon.json文件的格式不正确,后面多了一个逗号

查看daemon.json文件:

Java代码  收藏代码
  1. vi /etc/docker/daemon.json  

 文件内容如下:

Java代码  收藏代码
  1. {"registry-mirrors": ["https://04c5r1cy.mirror.aliyuncs.com"],}  

 后面多了一个逗号,需要删除,修改为

Java代码  收藏代码
  1. {"registry-mirrors": ["https://04c5r1cy.mirror.aliyuncs.com"]}  

 保存退出,重启docker

Java代码  收藏代码
  1. systemctl restart docker  

 然后再去获取mysql 5.5版本

Java代码  收藏代码
  1. docker pull mysql:5.5  

4、查看系统存在的镜像

Java代码  收藏代码
  1. docker images  

 结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker images  
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
  3. docker.io/mysql     5.5                 0da48351c371        2 weeks ago         205 MB  

三、运行镜像(容器使用)

1、运行一个镜像组件,即创建一个docker窗口,可以运行多个

Java代码  收藏代码
  1. docker run --name xxxx -d 组件名:版本号(Tag)  

 --name:表示重命名(--是两横杠,这里看不出来)

-d:表示后台运行

如运行一个tomcat:

Java代码  收藏代码
  1. docker run --name mytomcat -d tomcat:8.5  

2、查看运行中的容器

Java代码  收藏代码
  1. docker ps  

 

3、查看所有容器,包括运行的和暂停的容器

Java代码  收藏代码
  1. docker ps -a  

4、停止运行中的容器

Java代码  收藏代码
  1. docker stop 容器ID  

 或

Java代码  收藏代码
  1. docker stop 容器names  

 最好使用容器ID

5、重新开始运行暂定的容器

Java代码  收藏代码
  1. docker start 容器ID  

 暂停的容器ID通过docker ps -a 查找

6、删除容器

Java代码  收藏代码
  1. docker rm -f 容器ID  

 结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker rm -f a9c2f22a4b50  
  2. a9c2f22a4b50  

7、启动一个有端口映射的容器(Tomcat)

Java代码  收藏代码
  1. docker run -d -p 8888:8080 tomcat  

 -d:表示后台运行

-p:表示端口映射,前面的端口为虚拟机的端口,后面的端口表示tomcat的端口,表示将虚拟机的8888端口映射到tomcat的8080端口,当访问192.168.1.166:8888就可以访问tomcat

启动的结果如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker run --name tomcat8 -d -p 8080:8080 tomcat:8.5  
  2. b91a31986a63f82340c588272a334c164de571fb4201d628bad3418f55d7f20b  
  3. [root@localhost ~]# docker ps  
  4. CONTAINER ID  IMAGE       COMMAND           CREATED        STATUS        PORTS                    NAMES  
  5. b91a31986a63  tomcat:8.5  "catalina.sh run" 12 seconds ago Up 8 second   0.0.0.0:8080->8080/tcp   tomcat8  

通过浏览器访问:http://localhost:8080/来检查tomcat有没有启动成功。

注意:

如果访问不了,可能是因为防火墙的原因!!!

查看防火墙状态:

Java代码  收藏代码
  1. service firewall status  

关闭防火墙

Java代码  收藏代码
  1. service firewall stop  

8、查看docker日志

Java代码  收藏代码
  1. docker logs 容器ID  

9、其它:

使用

Java代码  收藏代码
  1. docker --help  

 查看其它命令,如下:

Java代码  收藏代码
  1. [root@localhost ~]# docker --help  
  2.   
  3. Usage:  docker COMMAND  
  4.   
  5. A self-sufficient runtime for containers  
  6.   
  7. Options:  
  8.       --config string      Location of client config files (default "/root/.docker")  
  9.   -D, --debug              Enable debug mode  
  10.       --help               Print usage  
  11.   -H, --host list          Daemon socket(s) to connect to (default [])  
  12.   -l, --log-level string   Set the logging level ("debug", "info", "warn", "error",                                 "fatal") (default "info")  
  13.       --tls                Use TLS; implied by --tlsverify  
  14.       --tlscacert string   Trust certs signed only by this CA (default                                              "/root/.docker/ca.pem")  
  15.       --tlscert string     Path to TLS certificate file (default                                                    "/root/.docker/cert.pem")  
  16.       --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")  
  17.       --tlsverify          Use TLS and verify the remote  
  18.   -v, --version            Print version information and quit  
  19.   
  20. Management Commands:  
  21.   container   Manage containers  
  22.   image       Manage images  
  23.   network     Manage networks  
  24.   node        Manage Swarm nodes  
  25.   plugin      Manage plugins  
  26.   secret      Manage Docker secrets  
  27.   service     Manage services  
  28.   stack       Manage Docker stacks  
  29.   swarm       Manage Swarm  
  30.   system      Manage Docker  
  31.   volume      Manage volumes  
  32.   
  33. Commands:  
  34.   attach      Attach to a running container  
  35.   build       Build an image from a Dockerfile  
  36.   commit      Create a new image from a container's changes  
  37.   cp          Copy files/folders between a container and the local filesystem  
  38.   create      Create a new container  
  39.   diff        Inspect changes on a container's filesystem  
  40.   events      Get real time events from the server  
  41.   exec        Run a command in a running container  
  42.   export      Export a container's filesystem as a tar archive  
  43.   history     Show the history of an image  
  44.   images      List images  
  45.   import      Import the contents from a tarball to create a filesystem image  
  46.   info        Display system-wide information  
  47.   inspect     Return low-level information on Docker objects  
  48.   kill        Kill one or more running containers  
  49.   load        Load an image from a tar archive or STDIN  
  50.   login       Log in to a Docker registry  
  51.   logout      Log out from a Docker registry  
  52.   logs        Fetch the logs of a container  
  53.   pause       Pause all processes within one or more containers  
  54.   port        List port mappings or a specific mapping for the container  
  55.   ps          List containers  
  56.   pull        Pull an image or a repository from a registry  
  57.   push        Push an image or a repository to a registry  
  58.   rename      Rename a container  
  59.   restart     Restart one or more containers  
  60.   rm          Remove one or more containers  
  61.   rmi         Remove one or more images  
  62.   run         Run a command in a new container  
  63.   save        Save one or more images to a tar archive (streamed to STDOUT by default)  
  64.   search      Search the Docker Hub for images  
  65.   start       Start one or more stopped containers  
  66.   stats       Display a live stream of container(s) resource usage statistics  
  67.   stop        Stop one or more running containers  
  68.   tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE  
  69.   top         Display the running processes of a container  
  70.   unpause     Unpause all processes within one or more containers  
  71.   update      Update configuration of one or more containers  
  72.   version     Show the Docker version information  
  73.   wait        Block until one or more containers stop, then print their exit codes  
  74.   
  75. Run 'docker COMMAND --help' for more information on a command.  

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/

原文地址:https://www.cnblogs.com/fanshuyao/p/8745142.html