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
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
#REPOSITORY image名稱 #TAG 版本號 #IMAGE ID image的id #CREATED 創建時間 #SIZE image的大小
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/
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

[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

[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

[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 ~]#
使用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
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 !!!!!")
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"]
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
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]#
dockerfile語法簡介
上面制作image編輯dockerfile文件中有一些關鍵字,這些都是dockerfile的語法,下面就dockerfile常用語法進行簡單介紹
docker語法
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作為額外參數
dockerfile的Shell和Exec語法格式