这里,记录一下比较常用的docker操作细节,对于初次使用者,可能有很大的帮助。
docker作为云计算Paas层面的东西,风靡全世界了,主要是因为它小巧,好用,功能强大。今天主要介绍一下如何依据自己的dockerfile创建image,并将自己的image共享到DockerHub上。方便团队在需要的时候取用。
这里主要是demo,所以,Dockerfile也很简单:
1 #this is my demo dockerfile 2 3 FROM ubuntu:14.10 4 MAINTAINER xxxxx@yyyy.com 5 ADD script.sh /usr/script/ 6 ENTRYPOINT ["/usr/script/script.sh"] 7 CMD ["This is a test dockfile out"]
这里的script.sh的内容如下:
1 #!/bin/bash 2 3 echo "There are $# parameters provided!" 4 5 for i in $@ 6 do 7 echo $i 8 done
这里,需要注意一点的是,第一行的命令,必须是FROM,比如,将FROM命令放在第二行了,在执行docker build的时候,会报下面的错误:
[root@CloudGame myimage]# docker build -t shihuc/hello:v1 . Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : MAINTAINER xxxx@yyyy.com Please provide a source image with `from` prior to commit
接着看正常的Dockerfile的build的过程:
1 [root@CloudGame myimage]# docker build -t shihuc/demo:v1 . 2 Sending build context to Docker daemon 3.072 kB 3 Sending build context to Docker daemon 4 Step 0 : FROM ubuntu:14.10 5 14.10: Pulling from ubuntu 6 6d370c930acc: Already exists 7 9a63f1e91c4c: Already exists 8 74364530838d: Already exists 9 dce38fb57986: Already exists 10 ubuntu:14.10: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. 11 Digest: sha256:6341c688b4b0b82ec735389b3c97df8cf2831b8cb8bd1856779130a86574ac5c 12 Status: Downloaded newer image for ubuntu:14.10 13 ---> dce38fb57986 14 Step 1 : MAINTAINER xxxx@yyyy.com 15 ---> Using cache 16 ---> d77b1889ec2b 17 Step 2 : ADD script.sh /usr/script/ 18 ---> Using cache 19 ---> 2d927db27a62 20 Step 3 : ENTRYPOINT /usr/script/script.sh 21 ---> Using cache 22 ---> 2d0fea5aca3c 23 Step 4 : CMD This is a test dockfile out 24 ---> Using cache 25 ---> 54b2b27ac4ef 26 Successfully built 54b2b27ac4ef
现在docker image demo:v1已经创建好了。这个时候就可以共享出去了,共享到DockerHub上去。首先需要有一个DockerHub的帐号,当然,这里,我申请了一个Public的repo,免费的,和GitHub类似,public的免费,private的要钱哟。网址为https://hub.docker.com/
申请好了帐号后,就可以登录了:
1 [root@CloudGame myimage]# docker login 2 Username: shihuc 3 Password: 4 Email: xxxx@yyyy.com 5 WARNING: login credentials saved in /root/.docker/config.json 6 Login Succeeded 7 [root@CloudGame myimage]#
现在,我就将这个docker image demo:v1上传到DockerHub上去:
1 [root@CloudGame myimage]# docker push shihuc/demo:v1 2 The push refers to a repository [shihuc/demo] (len: 1) 3 54b2b27ac4ef: Image already exists 4 2d0fea5aca3c: Image already exists 5 2d927db27a62: Image successfully pushed 6 d77b1889ec2b: Image already exists 7 dce38fb57986: Image already exists 8 74364530838d: Image successfully pushed 9 9a63f1e91c4c: Image successfully pushed 10 Digest: sha256:593286981f39efb3e7caa52020645f3f5b2a38f45a8d3ffaa1b4079b5f508a2e
这里有两点要注意哟
1. 必须是在命令行下登录了DockerHub后才能作push操作。这个很容易理解,否则push不知道往那push,这个和git push有点不同,git push到GitHub时,通常是走SSH,这个时候要在github设置Public key,本地会默认用当前用户.ssh下的私钥。而DockerHub是通过用户名密码和邮箱地址来确认用户身份的。
2. 上面push操作的指令中,红色部分,shihuc表示DockerHub上的用户名,demo是repo的名字,v1表示tag号。若在创建docker image的时候,-t选项不是shihuc/demo:v1,而是demo:v1,那么即使用shihuc用户名登录了DockerHub,最后也无法push的,会遇到下面的错误:
1 [root@CloudGame myimage]# docker push demo:v1 2 You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: shihuc/demo)
而此时,你若根据提示,采用docker push shihuc/demo:v1提交,则会遇到下面的错误:
1 [root@CloudGame myimage]# docker push shihuc/demo:v1 2 The push refers to a repository [shihuc/demo] (len: 1) 3 Repository does not exist: shihuc/demo
好了,最后看看我们创建的image运行的如何吧,首先默认运行,不加任何参数(结合上面的脚本,分析一下,就可以知道答案是正确的,注意CMD指令后的参数是在一个引号内哟):
1 [root@CloudGame myimage]# docker run shihuc/demo:v1 2 There are 1 parameters provided! 3 This 4 is 5 a 6 test 7 dockfile 8 out
再来看看,添加参数的运行结果:
1 [root@CloudGame myimage]# docker run shihuc/demo:v1 hello docker 2 There are 2 parameters provided! 3 hello 4 docker
这个例子,说明自己创建的image运行一切正常。也可以看出CMD的参数可以被命令行替换,而ENTRYPOINT的参数是不能被替换的。