網頁

2019/12/15

Docker 執行 hello-world image

以下出自Docker官方文件Getting started with Docker Desktop for Mac - Explore the application的內容。


在命令列(終端機)執行docker run hello-world來執行hello-world image。由於hello-world image目前不存在本機,所以docker會自動去Docker Hub下載(pull) hello-world image,下載完成後會接著執行。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest

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/    

這就是你第一個執行的image。


接著試著執行另一個image,這個image名稱叫nginxnginx是一個web server網頁伺服器,而nginx image即為已容器化(dockerized)的nginx。


在命令列執行docker run --detach --publish=80:80 --name=webserver nginx

hello-world一樣nginx不存在本機所以會先從Docker Hub下載。

$ docker run --detach --publish=80:80 --name=webserver nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
000eee12ec04: Pull complete
eb22865337de: Pull complete
bee5d581ef8b: Pull complete
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
b47952bc76d2896e8e8fe67f849d1ff64ac7ad1291eb60a09cb3facdb3c836ab

執行後開啟瀏覽器,在網址列輸入http://localhost/即可進入nginx web server的預設畫面如下。



輸入docker container ls可列出Docker目前運行中的containers(容器)。

執行docker container ls可以看到目前運行中的容器名稱為webserver,該容器的image為nginx

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
b47952bc76d2        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes       0.0.0.0:80->80/tcp   webserver

接著輸入docker container stop webserver停止運行中的webserver容器。

$ docker container stop webserver
webserver

輸入docker container rm webserver移除webserver容器。

$ docker container rm webserver
webserver

接著用docker image ls列出本機中有哪些images。

可以看到目前local有兩個剛剛下載並執行的hello-worldnginx image。

$ docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
nginx                              latest              231d40e811cd        3 weeks ago         126MB
hello-world                        latest              fce289e99eb9        11 months ago       1.84kB

輸入docker image rm nginxnginx image刪除

$ docker image rm nginx
Untagged: nginx:latest
Untagged: nginx@sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Deleted: sha256:231d40e811cd970168fb0c4770f2161aa30b9ba6fe8e68527504df69643aa145
Deleted: sha256:dc8adf8fa0fc82a56c32efac9d0da5f84153888317c88ab55123d9e71777bc62
Deleted: sha256:77fcff986d3b13762e4777046b9210a109fda20cb261bd3bbe5d7161d4e73c8e
Deleted: sha256:831c5620387fb9efec59fc82a42b948546c6be601e3ab34a87108ecf852aa15f

再次輸入docker image ls可看到nginx image已經不見被刪除了。

$ docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
hello-world                        latest              fce289e99eb9        11 months ago       1.84kB

以上為執行Docker image的基本教學。


參考:

沒有留言:

張貼留言