概念科普
- Docker image:镜像是只读的,镜像中包含有需要运行的文件。镜像用来创建container,一个镜像可以运行多个container;镜像可以通过Dockerfile创建,也可以从Docker hub/registry上下载。
- Docker container:容器是Docker的运行组件,启动一个镜像就是一个容器,容器是一个隔离环境,多个容器之间不会相互影响,保证容器中的程序运行在一个相对安全的环境中。
- Docker hub/registry: 共享和管理Docker镜像,用户可以上传或者下载上面的镜像,官方地址为
https://registry.hub.docker.com/
,也可以搭建自己私有的Docker registry。
运行第一个镜像
将image从仓库下载到本地
docker pull library/hello-world
执行命令docker images 查看所有镜像
[root@insure docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 5958914cc558 3 weeks ago 94.9MB centos latest 75835a67d134 2 months ago 200MB hello-world latest 4ab4c602aa5e 3 months ago 1.84kB redis 4.0.2 8f2e175b3bd1 13 months ago 107MB
运行刚才下载的镜像
[root@insure docker]# docker run hello-world 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/
到此,我们第一个镜像运行成功。
现在我们删除所有的容器和镜像,保持一个干净的环境
首先删除容器,因为容器包含在镜像之内,一个镜像可以有很多容器
[root@insure docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b13eb6f29cb1 hello-world "/hello" About a minute ago Exited (0) About a minute ago blissful_benz 356502c8cbfd centos:latest "java -jar /usr/clai…" 17 hours ago Created claimdashboardtest f5f8aa391da0 centos:latest "/bin/bash" 3 weeks ago Up 3 weeks test-centos [root@insure docker]# docker rm b13eb6f29cb1 b13eb6f29cb1 [root@insure docker]# docker rm 356502c8cbfd 356502c8cbfd [root@insure docker]# docker f5f8aa391da0 docker: 'f5f8aa391da0' is not a docker command. See 'docker --help' [root@insure docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5f8aa391da0 centos:latest "/bin/bash" 3 weeks ago Up 3 weeks test-centos
删除镜像文件
[root@insure docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 5958914cc558 3 weeks ago 94.9MB centos latest 75835a67d134 2 months ago 200MB hello-world latest 4ab4c602aa5e 3 months ago 1.84kB redis 4.0.2 8f2e175b3bd1 13 months ago 107MB [root@insure docker]# docker rmi 5958914cc558 Untagged: redis:latest Untagged: redis@sha256:f57d1597d038a742dfba6acfaf48b10e6383466eea2aef95d1ee76f32633f959 Deleted: sha256:5958914cc55880091b005658a79645a90fd44ac6a33abef25d6be87658eb9599 Deleted: sha256:2034be36bd0f105ea0b4cbb124a96fa434fda3ce9c32dddcf38f1b6e5699ac91 Deleted: sha256:c2d3730f64b8e231f59d86ac8bdf6de3e62538a5b0030c9a37bf1cf21241ec76 Deleted: sha256:1a869407b0486b83e44fcec89fc7b12935c23caea8768e0e9402df67a01f4ffe Deleted: sha256:1568b09301049abf7ed4b38406ce96465f2145af91428d9efa8c8c0dc53297fa Deleted: sha256:42bd21f043c373312ccf3f31fcfeabf596497421e9ff0103b6fb7dc764de631e Deleted: sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3 [root@insure docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 75835a67d134 2 months ago 200MB hello-world latest 4ab4c602aa5e 3 months ago 1.84kB redis 4.0.2 8f2e175b3bd1 13 months ago 107MB
上面简单的练习了一下,镜像的下载和删除。
下面正式进入我们的目标文件------------jar
1.查看所有的容器ip和名字
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
docker run -tid --net=host -v /opt/pj/business.jar:/usr/business.jar --name business java:8u111 java -jar /usr/business.jar
-t: 为container分配一个伪终端(pseudo-tty),并绑定到容器的标准输入上
-i: 让容器的标准输入保持打开
-d: 使容器在后台以守护态(Daemonized)形式运行
--net=host 使用host模式的容器可以直接使用docker host的IP地址与外界通信
-v /usr/springboot-1.jar:/usr/springboot-1.jar 表示将宿主主机的jar文件,映射到容器中(分号前为宿主主机的路径,分号后为容器中的路径)
--name business表示为该容器取一个全局唯一的名称,这里我取的名称为business
java:8u111 表示镜像文件的名称和tag
java -jar /usr/business.jar 表示运行jar包,注意:这里的jar包为容器中的位置,是通过前面的-v属性映射的