如何创建自己的docker image并上传到DockerHub上

时间:2023-02-07 05:53:51

这里,记录一下比较常用的docker操作细节,对于初次使用者,可能有很大的帮助。

docker作为云计算Paas层面的东西,风靡全世界了,主要是因为它小巧,好用,功能强大。今天主要介绍一下如何依据自己的dockerfile创建image,并将自己的image共享到DockerHub上。方便团队在需要的时候取用。

这里主要是demo,所以,Dockerfile也很简单:

 #this is my demo dockerfile

 FROM ubuntu:14.10
MAINTAINER xxxxx@yyyy.com
ADD script.sh /usr/script/
ENTRYPOINT ["/usr/script/script.sh"]
CMD ["This is a test dockfile out"]

这里的script.sh的内容如下:

 #!/bin/bash

 echo "There are $# parameters provided!"

 for i in $@
do
echo $i
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 : MAINTAINER xxxx@yyyy.com
Please provide a source image with `from` prior to commit

接着看正常的Dockerfile的build的过程:

 [root@CloudGame myimage]# docker build -t shihuc/demo:v1 .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon
Step : FROM ubuntu:14.10
14.10: Pulling from ubuntu
6d370c930acc: Already exists
9a63f1e91c4c: Already exists
74364530838d: Already exists
dce38fb57986: Already exists
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.
Digest: sha256:6341c688b4b0b82ec735389b3c97df8cf2831b8cb8bd1856779130a86574ac5c
Status: Downloaded newer image for ubuntu:14.10
---> dce38fb57986
Step : MAINTAINER xxxx@yyyy.com
---> Using cache
---> d77b1889ec2b
Step : ADD script.sh /usr/script/
---> Using cache
---> 2d927db27a62
Step : ENTRYPOINT /usr/script/script.sh
---> Using cache
---> 2d0fea5aca3c
Step : CMD This is a test dockfile out
---> Using cache
---> 54b2b27ac4ef
Successfully built 54b2b27ac4ef

现在docker image demo:v1已经创建好了。这个时候就可以共享出去了,共享到DockerHub上去。首先需要有一个DockerHub的帐号,当然,这里,我申请了一个Public的repo,免费的,和GitHub类似,public的免费,private的要钱哟。网址为https://hub.docker.com/

申请好了帐号后,就可以登录了:

 [root@CloudGame myimage]# docker login
Username: shihuc
Password:
Email: xxxx@yyyy.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@CloudGame myimage]#

现在,我就将这个docker image demo:v1上传到DockerHub上去:

 [root@CloudGame myimage]# docker push shihuc/demo:v1
The push refers to a repository [shihuc/demo] (len: )
54b2b27ac4ef: Image already exists
2d0fea5aca3c: Image already exists
2d927db27a62: Image successfully pushed
d77b1889ec2b: Image already exists
dce38fb57986: Image already exists
74364530838d: Image successfully pushed
9a63f1e91c4c: Image successfully pushed
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的,会遇到下面的错误:

 [root@CloudGame myimage]# docker push demo:v1
You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: shihuc/demo)

而此时,你若根据提示,采用docker push shihuc/demo:v1提交,则会遇到下面的错误:

 [root@CloudGame myimage]# docker push shihuc/demo:v1
The push refers to a repository [shihuc/demo] (len: )
Repository does not exist: shihuc/demo

好了,最后看看我们创建的image运行的如何吧,首先默认运行,不加任何参数(结合上面的脚本,分析一下,就可以知道答案是正确的,注意CMD指令后的参数是在一个引号内哟):

 [root@CloudGame myimage]# docker run shihuc/demo:v1
There are parameters provided!
This
is
a
test
dockfile
out

再来看看,添加参数的运行结果:

 [root@CloudGame myimage]# docker run shihuc/demo:v1 hello docker
There are parameters provided!
hello
docker

这个例子,说明自己创建的image运行一切正常。也可以看出CMD的参数可以被命令行替换,而ENTRYPOINT的参数是不能被替换的。