阅读 56

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 命令
RUN install -y python
ENV NAME TEST
CMD echo "hello ${NAME}"
 
Exec格式: ["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"开头
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 

 

原文:https://www.cnblogs.com/flags-blog/p/15265402.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐