docker入門_下載、創建第一個image、啟動第一個container


image

啟動container就需要image,image是啟動container的模板。如何獲取image?image可以從repository直接下載也可以通過dockerfile自定義

從repository下載第一個image

https://hub.docker.com/ 網站注冊賬號然后搜索需要下載的image,以hello-world為例,在linux上執行docker pull  hello-world;其中hello-world為image的名字

[root@k8s-01 ~]# docker pull  hello-world
Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest
View Code

docker image ls;#查看linux里面所有得images

[root@k8s-01 ~]# docker image ls
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
wordpress                                                         latest              baf5889057ff        6 weeks ago         551MB
mysql                                                             5.7                 8cf625070931        7 weeks ago         448MB
hello-world                                                       latest              d1165f221234        6 months ago        13.3kB
View Code

#REPOSITORY image名稱      #TAG 版本號     #IMAGE ID image的id          #CREATED 創建時間       #SIZE image的大小

默認情況下docker pull image會下載最新版本的鏡像,需要指定版本可以使用下面的格式
docker pull redis:6.0.15-buster 6.0.15-buster是image在repository中的tag信息;登錄repository(https://hub.docker.com)查詢image相關信息,確認需要下載image名稱和版本信息
docker rmi -f imageid 刪除本地鏡像
 
 
 

container

啟動第一個container

[root@k8s-01 ~]# 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/
View Code

docker container ls #查詢當前正在運行的container,上面的hello-world的image拉起的container在宿主機輸出回顯后就運行結束了,處於退出狀態。需要使用-a參數查詢

 docker container ls -a|grep hello
4aa268832b1c        hello-world                                         "/hello"                 4 minutes ago       Exited (0) 4 minutes ago                           brave_chaplygin
View Code
docker run -d imageid #后台拉起container,某些image並不是執行命令就結束,例如nginx的image是需要持續提供服務,這時啟動container就需要使用-d參數,否則啟動任務就會卡主,這時docker container ls查詢就是運行狀態
[root@k8s-01 ~]# docker run -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a330b6cecb98: Pull complete 
e0ad2c0621bc: Pull complete 
9e56c3e0e6b7: Pull complete 
09f31c94adc6: Pull complete 
32b26e9cdb83: Pull complete 
20ab512bbb07: Pull complete 
Digest: sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3e
Status: Downloaded newer image for nginx:latest
362f3279023a981efc0a68915eb4b19589458ce2b47bfb0e7fd9e06eaa13b395
[root@k8s-01 ~]# docker container ls -a|grep nginx
362f3279023a        nginx                                               "/docker-entrypoint.…"   11 seconds ago      Up 8 seconds                   80/tcp              optimistic_thompson
View Code
docker container ls -aq只列出container的id

docker container ls -f "status=exited" -q 列出status為exit狀態的container

docker container kill contaiberid停止正在運行的container
[root@k8s-01 ~]# docker container ls -a|grep nginx                 
362f3279023a        nginx                                               "/docker-entrypoint.…"   5 minutes ago       Up 4 minutes                   80/tcp              optimistic_thompson
[root@k8s-01 ~]# docker container kill 362f3279023a
362f3279023a
[root@k8s-01 ~]# docker container ls -a|grep nginx 
362f3279023a        nginx                                               "/docker-entrypoint.…"   5 minutes ago       Exited (137) 3 seconds ago                         optimistic_thompson
View Code
docker container rm containerid 刪除container,停止狀態的container才可以刪除
[root@k8s-01 ~]# docker container ls -a|grep nginx 
362f3279023a        nginx                                               "/docker-entrypoint.…"   5 minutes ago       Exited (137) 3 seconds ago                         optimistic_thompson
[root@k8s-01 ~]# docker container rm 362f3279023a  
362f3279023a
[root@k8s-01 ~]# docker container ls -a|grep nginx
[root@k8s-01 ~]# 
View Code
docker container rm $(docker container ls -aq) 刪除所有container,刪除其它狀態container類似,使用不同過濾條件即可

 

使用container創建image

docker提供了使用container創建image的接口,我們可以在container內部署完自定義應用后如果需要將當前container的狀態保存以便后續直接拉起當前狀態的container就可以使用該功能,該功能更類似雲服務的虛擬機制作私有鏡像。
使用nginx image啟動container,使用該container制作名稱為nginx_by_container的image
[root@k8s-01 ~]# docker run -d nginx
e529d2651d3c8ab18e4de3702f42e1f229b095d2b19ed7973944657a9199a4a7
[root@k8s-01 ~]# docker container ls -a|grep nginx
e529d2651d3c        nginx                                               "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds                80/tcp              priceless_khorana
[root@k8s-01 ~]# docker container commit e529d2651d3c nginx_by_container
sha256:bdf3ae335c0c51906460caffa10ddfe51d5900b3852fc2749d1a0e6cdf11b02e
[root@k8s-01 ~]# docker image ls|grep nginx                             
nginx_by_container                                                latest              bdf3ae335c0c        9 seconds ago       133MB
nginx                                                             latest              ad4c705f24d3        4 days ago          133MB
View Code

 

dockerfile創建image

除了container創建image外也可以使用dockerfile創建image,而且推薦使用dockerfile創建image

dockerfile創建的第一個image

1、mkdir demo創建一個目錄,用於存放dockerfile文件和原始image需要配置的app相關文件

2、進入demo目錄下創建app目錄,app目錄下創建如下py文件,用於驗證dockerfile創建的鏡像。

[root@k8s-01 demo]# mkdir app
[root@k8s-01 demo]# ls
app
[root@k8s-01 demo]# cd app/
[root@k8s-01 app]# ll
total 0
[root@k8s-01 app]# vim hello_image.py
#!/usr/bin/ENV python3
# -*- coding: utf-8 -*-

print("hello_my_image !!!!!")
View Code

3、從app目錄返回上一級到demo目錄,創建dockerfile文件,文件內容如下

[root@k8s-01 app]# cd ..
[root@k8s-01 demo]# ll
total 0
drwxr-xr-x 2 root root 28 Sep 14 22:29 app
[root@k8s-01 demo]# vim Dockerfile
FROM faucet/python3
ADD app/hello_image.py  /
CMD ["python3", "/hello_image.py"]
View Code

4、demo目錄執行創建image的命令:[root@k8s-01 demo]# docker build -t flagzhang/hello_image .     #-t后面的參數是創建image的名字,“.”代表Dockerfile文件的相對路徑

[root@k8s-01 demo]# docker build -t flagzhang/hello_image .
Sending build context to Docker daemon  3.584kB
Step 1/3 : FROM faucet/python3
latest: Pulling from faucet/python3
21c83c524219: Pull complete 
293feef12dcd: Pull complete 
40050b79c9fc: Pull complete 
47305f521609: Pull complete 
Digest: sha256:8109b3bc7aeb3e4879ba30fb54ffbabe1359fdaa51fde74c523c70cb19fa0f9e
Status: Downloaded newer image for faucet/python3:latest
 ---> fa4fc740aef4
Step 2/3 : ADD app/hello_image.py  /
 ---> 9380d953168a
Step 3/3 : CMD ["python3", "/hello_image.py"]
 ---> Running in f5a876fe2fe9
Removing intermediate container f5a876fe2fe9
 ---> 888c3483396f
Successfully built 888c3483396f
Successfully tagged flagzhang/hello_image:latest
View Code

5、查看通過dockerfile創建的image,使用此image創建container。到此為止我們使用dockerfile創建的第一個image就成功運行起來了

[root@k8s-01 demo]# docker image ls
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
flagzhang/hello_image                                             latest              888c3483396f        13 seconds ago      58.3MB

[root@k8s-01 demo]# docker run flagzhang/hello_image
Starting with UID=0 GID=0
hello_my_image !!!!!
[root@k8s-01 demo]#
View Code

 

dockerfile語法簡介

上面制作image編輯dockerfile文件中有一些關鍵字,這些都是dockerfile的語法,下面就dockerfile常用語法進行簡單介紹

docker語法
FROM imagename 從哪個image作為基礎,默認為空。非官方鏡像需要把id/imagename寫全,本地沒有的inamge會從repository拉取該鏡像
LABLE key1=value1 key2=value2 …… image的注釋,使用docker inspect查看image詳細信息時候能夠看到
RUN cmd1 && cmd2 …… 在container拉起時候執行的shell命令,一行寫不下可以用\換行;可以定義多行RUN
WORKDIR dir 切換路徑,相當於cd。可以是絕對路徑也可以是相對路徑
ADD filename dir 將宿主機文件移動到當前dockerfile定義的鏡像對應路徑下,如果是壓縮文件會自動解壓
COPY 使用方法於ADD一致,移動文件時不解壓壓縮文件。
ENV var value 定義常量,RUN echo ${value}使用ENV定義的常量。
 
dockerfile中執行命令的語法
  • RUN 指定命令並創建新的鏡像層,在拉起cotainer過程中執行;一般用於安裝軟件包
    • 有兩種語法 Shell Exec
  • CMD container啟動后默認執行的命令和參數,但CMD能夠被docker run后面跟的命令進行替換;因此CMD一般執行的是默認執行的命令個;如果dockerfile定義多個CMD命令只有最后一個生效
    • 有三種語法格式:Shell Exec CMD ["para1", "para2"]為ENTRYPOINT提供額外的參數,此時ENTRYPOINT必須使用Exec格式
  • ENTRYPOINT 配置容器啟動時執行的命令,一定會執行,不會被替換。
    • 支持三種語法格式Shell Exec(推薦) 使用CMD作為額外參數    
      ENTRYPOINT ["/bin/echo", "Hello"]
      CMD ["world"]
      docker run -it [image] #輸出Hello world
      docker run -it [image] /bin/bash #CMD忽略 ENTRYPOINT 執⾏
      docker run -it [image] docker #輸出 Hello docker
      eg:ENTRYPOINT ["java","-jar","hello-1.0-SNAPSHOT.jar"]
      CMD []

 

 

 
dockerfile的Shell和Exec語法格式
Shell格式:關鍵字RUN CMD ENTRYPOINT 命令 <instraction> <cmd>
RUN install -y python
ENV NAME TEST
CMD echo "hello ${NAME}"
 
Exec格式:<instraction> ["executable", "param1", "raram2", ……]
RUN ["yum", "install", "python"]
CMD ["/bin/echo", "Hello World"]
ENTRYPOINT ["/bin/echo", "Hello World"]
 
ENV NAME MYTEST
CMD ["/bin/echo", "Hello World ${NAME}"] #輸出Hello World ${NAME}
CMD ["/bin/sh", "-c", "echo Hello World ${NAME}"] #輸出Hello World MYTEST
注意:Exec格式如果需要引用變量則需要使用"/bin/sh", "-c"開頭
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM