1. 程式人生 > >Docker02 Docker初識:第一個Docker容器和Docker鏡像

Docker02 Docker初識:第一個Docker容器和Docker鏡像

gen ech current aer digest 第一個 -- 文件 展示

目錄

[TOC]

一、第一個Docker容器

使用docker run 命令時,如果在本地沒有改鏡像,那麽會直接重Docker Hub(一個官方的鏡像庫)中拉取鏡像。

docker run --rm hello-world

# 運行結果展示
Unable to find image ‘hello-world:latest‘ locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pulling fs layer 
docker: error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/e3/e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96/data?Expires=1525996669&Signature=M6vcU5NqAiIMXSuJowD1zmLStFXMGck436eqPJk6GdSKrx4v~YIkV1DHQpz5aKOQnPIHowmSe6wLPWCn7E4U2my-BNqhbRVr65ndw-fJYO0eucaeRnEp7jkyhfxNJFWzMiVHmk~U595HGt4vZ4E50Umc76xKLvciYl1HGLwJhtw_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout.
See ‘docker run --help‘.
[gupan@localhost ~]$ docker run --rm hello-world
Unable to find image ‘hello-world:latest‘ locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
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/engine/userguide/

二、第一個Docker鏡像

2.1 創建Docker鏡像準備工作

# 新建一個文件夾hello
mkdir hello
cd hello
# hello中新建一個文件,命名為Dockerfile,文件內容如下:
FROM alpine # 即將構建的鏡像是基於名為Apline的鏡像
CMD "echo" "Hello World"

2.2 構建Docker鏡像

打包鏡像

# 將上面的文件打包
docker build -t hello .
# -t 後面的參數是給這個鏡像取得標簽,.代表重當前路徑搜索Dockerfile文件,並執行裏面的代碼

運行結果

[gupan@localhost hello]$ docker build -t hello .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
latest: Pulling from library/alpine
ff3a5c916c92: Pull complete 
Digest: sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
Status: Downloaded newer image for alpine:latest
 ---> 3fd9065eaf02
Step 2/2 : CMD "echo" "Hello World"
 ---> Running in 4891b2d2a317
Removing intermediate container 4891b2d2a317
 ---> 4b1c2e073c23
Successfully built 4b1c2e073c23
Successfully tagged hello:latest

執行鏡像

[gupan@localhost hello]$ docker run --rm hello
Hello World
[gupan@localhost hello]$ 

Docker02 Docker初識:第一個Docker容器和Docker鏡像