docker学习笔记

附:参考博客

 docker常用命令  https://blog.csdn.net/itguangit/article/details/80246071

史上最简单的Docker入门教程 https://blog.csdn.net/itguangit/article/details/80222387

若依项目地址: https://gitee.com/y_project/RuoYi

一、docker原理概览

                                                     

二、镜像文件 image

一个镜像文件包含了某个应用程序及其相关的依赖包,通过docker的镜像文件可以生成多个容器实例运行。image可以在不同的环境移植,避免了由于开发环境的差异造成的问题。

image文件来源有两个:Dockfile文件和Docker hub

官方镜像网站   https://hub.docker.com/

image命令

#查看image帮助
[root@centos01 ~]# docker image -h
Flag shorthand -h has been deprecated, please use --help

Usage:	docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

# 查看image
[root@centos01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
kevin/ruoyi-admin   latest              f5bdc79b6201        2 weeks ago         770MB
tomcat              latest              f1332ae3f570        3 weeks ago         463MB
nginx               latest              2bcb04bdb83f        3 weeks ago         109MB
centos              latest              9f38484d220f        5 weeks ago         202MB
mysql               5.7.24              ba7a93aae2a8        3 months ago        372MB
java                8                   d23bdf5b1b1b        2 years ago         643MB
java                8u111               d23bdf5b1b1b        2 years ago         643MB

#删除image
[root@centos01 ~]# docker image rm 2bcb04bdb83f
Untagged: nginx:latest
Untagged: nginx@sha256:c8a861b8a1eeef6d48955a6c6d5dff8e2580f13ff4d0f549e082e7c82a8617a2
Deleted: sha256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c
Deleted: sha256:dfce9ec5eeabad339cf90fce93b20f179926d5819359141e49e0006a52c066ca
Deleted: sha256:166d13b0f0cb542034a2aef1c034ee2271e1d6aaee4490f749e72d1c04449c5b
Deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda

三、关于容器

image文件生成的实例本身也是一个文件,成为容器文件.也即是说,一旦容器生成,就会存在两个文件:一个image文件,一个容器文件.而且关闭容器并不会删除容器文件,只是容器停止运行而已。下面通过俩个实例说明:

1.第一个docker程序 hello-world

#使用docker search查询镜像
[root@centos01 ~]# docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   891                 [OK]          
#下载docker到本地
[root@centos01 ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Already exists 
Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for hello-world:latest
#查看
[root@centos01 ~]# docker image ls | grep hello
hello-world         latest              fce289e99eb9        3 months ago        1.84kB

镜像已经拉取成功,现在开始运行

#在后台运行容器实例,-d:后台运行,--name:指定容器的名称,最后为镜像名称,如果没有标签,默认为lastest
[root@centos01 ~]# docker run -d --name demo hello-world
5525c23268488f469d8de8def52f08d172a903955da21c0cc6372a36014c4dfe
#成功后返回容器Id,查看运行的容器
[root@centos01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
5525c2326848        hello-world         "/hello"                 52 seconds ago      Exited (0) 47 seconds ago                       demo

查看docker容器后台的日志

#查看docker容器日志
[root@centos01 ~]# docker logs -f demo

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/


Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2.使用dokcer部署web应用

下面以ruoyi管理系统为例,先启动,后续讲解如何构建docker镜像

#提前将应用打成docker镜像,查看镜像
[root@centos01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
kevin/ruoyi-admin   latest              f5bdc79b6201        2 weeks ago         770MB
tomcat              latest              f1332ae3f570        3 weeks ago         463MB
centos              latest              9f38484d220f        5 weeks ago         202MB
hello-world         latest              fce289e99eb9        3 months ago        1.84kB
mysql               5.7.24              ba7a93aae2a8        3 months ago        372MB
java                8                   d23bdf5b1b1b        2 years ago         643MB
java                8u111               d23bdf5b1b1b        2 years ago         643MB

#使用docker后台运行ry
[root@centos01 ~]# docker run -d --net=host --name ry-test -p 9527:80 kevin/ruoyi-admin
WARNING: Published ports are discarded when using host network mode
0bce3690ba8c7e5269157d724b6fe2b1a83e0fdd8ca103d3f78e798e180f88e2

#查看启动日志
[root@centos01 ~]# docker logs -f ry-test
Application Version: 3.2.0
Spring Boot Version: 2.1.1.RELEASE

//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//             佛祖保佑       永不宕机      永无BUG               //

23:09:18.252 [main] INFO  c.r.RuoYiApplication - [logStarting,50] - Starting RuoYiApplication on centos01 with PID 1 (/app.jar started by root in /)
23:09:18.275 [main] DEBUG c.r.RuoYiApplication - [logStarting,53] - Running with Spring Boot v2.1.1.RELEASE, Spring v5.1.3.RELEASE
23:09:18.277 [main] INFO  c.r.RuoYiApplication - [logStartupProfileInfo,679] - The following profiles are active: druid
23:09:25.250 [main] INFO  o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache' does not yet exist.  Creating now.
23:09:25.251 [main] INFO  o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]
23:09:28.628 [main] INFO  o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]
23:09:30.357 [main] INFO  o.a.c.h.Http11NioProtocol - [log,173] - Initializing ProtocolHandler ["http-nio-80"]
23:09:30.395 [main] INFO  o.a.c.c.StandardService - [log,173] - Starting service [Tomcat]
23:09:30.395 [main] INFO  o.a.c.c.StandardEngine - [log,173] - Starting Servlet Engine: Apache Tomcat/9.0.13
23:09:30.597 [main] INFO  o.a.c.c.AprLifecycleListener - [log,173] - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
23:09:30.845 [main] INFO  o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring embedded WebApplicationContext
23:09:32.557 [main] INFO  c.a.d.p.DruidDataSource - [init,930] - {dataSource-1} inited
23:09:32.736 [main] INFO  o.q.i.StdSchedulerFactory - [instantiate,1208] - Using default implementation for ThreadExecutor
23:09:32.842 [main] INFO  o.q.c.SchedulerSignalerImpl - [<init>,61] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
23:09:32.843 [main] INFO  o.q.c.QuartzScheduler - [<init>,229] - Quartz Scheduler v.2.3.0 created.
23:09:32.859 [main] INFO  o.q.c.QuartzScheduler - [initialize,294] - Scheduler meta-data: Quartz Scheduler (v2.3.0) 'RuoyiScheduler' with instanceId 'centos011556032172739'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 20 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is clustered.

23:09:32.860 [main] INFO  o.q.i.StdSchedulerFactory - [instantiate,1362] - Quartz scheduler 'RuoyiScheduler' initialized from an externally provided properties instance.
23:09:32.860 [main] INFO  o.q.i.StdSchedulerFactory - [instantiate,1366] - Quartz scheduler version: 2.3.0
23:09:32.862 [main] INFO  o.q.c.QuartzScheduler - [setJobFactory,2287] - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@7d1cfb8b
23:09:33.013 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,159] - ==>  Preparing: select job_id, job_name, job_group, method_name, method_params, cron_expression, misfire_policy, status, create_by, create_time, remark from sys_job 
23:09:33.342 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,159] - ==> Parameters: 
23:09:33.425 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,159] - <==      Total: 2
23:09:36.424 [main] INFO  s.d.s.w.PropertySourcedRequestMappingHandlerMapping - [initHandlerMethods,69] - Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
23:09:38.468 [main] INFO  s.d.s.w.p.DocumentationPluginsBootstrapper - [start,147] - Context refreshed
23:09:38.521 [main] INFO  s.d.s.w.p.DocumentationPluginsBootstrapper - [start,150] - Found 1 custom documentation plugin(s)
23:09:38.761 [main] INFO  s.d.s.w.s.ApiListingReferenceScanner - [scan,41] - Scanning for api listing references
23:09:39.385 [main] INFO  o.a.c.h.Http11NioProtocol - [log,173] - Starting ProtocolHandler ["http-nio-80"]
23:09:39.419 [main] INFO  o.a.t.u.n.NioSelectorPool - [log,173] - Using a shared selector for servlet write/read
23:09:39.832 [main] INFO  c.r.RuoYiApplication - [logStarted,59] - Started RuoYiApplication in 23.405 seconds (JVM running for 25.53)
(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  
 .-------.       ____     __        
 |  _ _   \      \   \   /  /    
 | ( ' )  |       \  _. /  '       
 |(_ o _) /        _( )_ .'         
 | (_,_).' __  ___(_ o _)'          
 |  |\ \  |  ||   |(_,_)'         
 |  | \ `'   /|   `-'  /           
 |  |  \    /  \      /           
 ''-'   `'-'    `-..-'              
23:09:40.478 [Quartz Scheduler [RuoyiScheduler]] INFO  o.q.c.QuartzScheduler - [start,547] - Scheduler RuoyiScheduler_$_centos011556032172739 started.
#查看后台进程
[root@centos01 ~]# ps -ef | grep java | grep -v grep
root      6335  6319 15 11:09 ?        00:00:26 java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

至此,web系统就在docker中运行了。如下图:

查看运行的容器

#查看运行的容器
[root@centos01 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0bce3690ba8c        kevin/ruoyi-admin   "java -Djava.securit…"   8 minutes ago       Up 8 minutes                            ry-test

#查看全部容器实例,包含停止的
[root@centos01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
0bce3690ba8c        kevin/ruoyi-admin   "java -Djava.securit…"   8 minutes ago       Up 8 minutes                                    ry-test
5525c2326848        hello-world         "/hello"                 23 minutes ago      Exited (0) 14 minutes ago                       demo
3a46e2bd8b96        kevin/ruoyi-admin   "java -Djava.securit…"   2 weeks ago         Exited (255) 2 weeks ago                        kevin-ry

#杀掉容器
[root@centos01 ~]# docker container kill 0bce3690ba8c
0bce3690ba8c

#移除容器
[root@centos01 ~]# docker container rm 0bce3690ba8c

四、Dockerfile文件

上面的俩个实例,第一个hello-world是从官方库直接拉取的镜像,如果我们要构建自己的镜像,如第二个示例,将web应用部署到docker中,那么就要先编写dockrefile文件,然后构建自己的镜像。以ruoyi系统为例,下面说明使用dockrefile构建镜像的过程:

先看一下位置:ruoyi-admin/src/main/docker/Dockerfile

Dockerfile文件

#拉取jdk8镜像
FROM java:8

#作者信息
MAINTAINER xxxx@163.com

#挂载
VOLUME /tmp

#添加jar包到容器并重命名为app.jar
ADD ruoyi-admin.jar app.jar

#设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

#执行命令改变文件或目录时间
RUN bash -c 'touch /app.jar'

#暴露的端口
EXPOSE 9527

#程序入口 相当于 java -jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

使用docker构建镜像

docker image build -t ruoyi-learnging:dev

运行

docker run -d --net=host --name ry-dev -p 9527:80 ruoyi-learnging:dev

后续可以将自己的docker镜像push到网上或者搭建自己的docker私服。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值