概念科普
- 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
第一步
拉取openjdk镜像
[root@insure dockerfile]# docker pull openjdk Using default tag: latest latest: Pulling from library/openjdk 16e82e17faef: Pull complete 117dc02416a3: Pull complete 7e4c717259ac: Pull complete 7a518b8f48be: Pull complete add32d44f708: Pull complete a0158fa08543: Pull complete eacd9302ecfd: Pull complete 8f9d52bf6cde: Pull complete Digest: sha256:2cbb95c7479634c53bc2be243554a98d6928c189360fa958d2c970974e7f131f Status: Downloaded newer image for openjdk:latest [root@insure dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE openjdk latest 8e7eacedab93 2 weeks ago 986MB centos latest 75835a67d134 2 months ago 200MB
第二步编写dockerfile文件
#centos:latest 分别是镜像和版本号 FROM openjdk:latest #将文件<src>拷贝到container的文件系统对应的路径<dest> ADD claimdashboard-0.0.1-SNAPSHOT.jar app.jar #RUN命令将在当前image中执行任意合法命令并提交执行结果。命令执行提交后,就会自动执行Dockerfile中的下一个指令 RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-jar","/app.jar"]
第三步生成镜像
[root@insure dockerfile]# sudo docker build -t="dashboard" . Sending build context to Docker daemon 42.15MB Step 1/4 : FROM openjdk:latest ---> 8e7eacedab93 Step 2/4 : ADD claimdashboard-0.0.1-SNAPSHOT.jar app.jar ---> ea0688f6b7cb Step 3/4 : RUN bash -c 'touch /app.jar' ---> Running in e97e88a2fc96 Removing intermediate container e97e88a2fc96 ---> 4d3d3cc9f9ce Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"] ---> Running in 9fa1b0191846 Removing intermediate container 9fa1b0191846 ---> 4762105b8815 Successfully built 4762105b8815 Successfully tagged dashboard:latest
[root@insure dockerfile]# docker images 查看已经的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
dashboard latest 4762105b8815 3 minutes ago 1.07GB
openjdk latest 8e7eacedab93 2 weeks ago 986MB
centos latest 75835a67d134 2 months ago 200MB
注意:命令后面要带上. -t表给镜像命名为dashboard
第四步 启动容器
[root@insure dockerfile]# docker run -d -p 8070:8070 dashboard 0924d9762dccb860e3d4869f27ed8dd6c7b9f69ad25ad368c83ec036e61a7ee0 [root@insure dockerfile]# docker ps -a 查看已有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0924d9762dcc dashboard "java -jar /app.jar" 4 seconds ago Up 3 seconds 0.0.0.0:8070->8070/tcp sleepy_ptolemy f5f8aa391da0 centos:latest "/bin/bash" 3 weeks ago Up 32 minutes test-centos
命令解析docker run -d -p 8070:8070 dashboard
docker--专有命令 run--表示运行容器 -d 表示后台运行
-p表示端口 前一个端口是外部浏览器访问的端口 后一个端口是内部的端口 两个端口可以不一样
dashboard 容器的名字
第五步 用postman测试效果
到此第一个在docker容器上的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属性映射的