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语法格式