1. 程式人生 > >docker執行第一個應用

docker執行第一個應用

概念科普

  • 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 檢視所有映象

[[email protected] 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

執行剛才下載的映象

[[email protected] 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/

到此,我們第一個映象執行成功。

現在我們刪除所有的容器和映象,保持一個乾淨的環境

首先刪除容器,因為容器包含在映象之內,一個映象可以有很多容器

[[email protected] 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
[[email protected] docker]# docker rm b13eb6f29cb1 
b13eb6f29cb1
[[email protected] docker]# docker rm 356502c8cbfd
356502c8cbfd
[[email protected] docker]# docker f5f8aa391da0
docker: 'f5f8aa391da0' is not a docker command.
See 'docker --help'
[[email protected] 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

刪除映象檔案

[[email protected] 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
[[email protected] docker]# docker rmi 5958914cc558
Untagged: redis:latest
Untagged: [email protected]:f57d1597d038a742dfba6acfaf48b10e6383466eea2aef95d1ee76f32633f959
Deleted: sha256:5958914cc55880091b005658a79645a90fd44ac6a33abef25d6be87658eb9599
Deleted: sha256:2034be36bd0f105ea0b4cbb124a96fa434fda3ce9c32dddcf38f1b6e5699ac91
Deleted: sha256:c2d3730f64b8e231f59d86ac8bdf6de3e62538a5b0030c9a37bf1cf21241ec76
Deleted: sha256:1a869407b0486b83e44fcec89fc7b12935c23caea8768e0e9402df67a01f4ffe
Deleted: sha256:1568b09301049abf7ed4b38406ce96465f2145af91428d9efa8c8c0dc53297fa
Deleted: sha256:42bd21f043c373312ccf3f31fcfeabf596497421e9ff0103b6fb7dc764de631e
Deleted: sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3
[[email protected] 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屬性對映的