首先,在https://hub.docker.com上注册自己的帐号,例如帐号是(test_a),留着后台上传自己的镜像至hub时用。
- 创建自己的Dockerfile
示例:
vim Dockerfile
写一个很简单的内容:
FROM centos
ADD jdk-8u191-linux-x64.tar.gz /usr/local/
复制centos镜像,将与Dockerfile文件相同目录下的这个文件(也可以是文件夹)jdk-8u191-linux-x64.tar.gz,添加至虚拟机下/usr/local/
wq
保存退出
- 创建镜像
docker build -t test_a/centos_test .
结果:
[master@master test]$ docker build -t test_a/centos .
Sending build context to Docker daemon 1.051GB
Step 1/2 : FROM centos
---> 0f3e07c0138f
Step 2/2 : ADD jdk-8u191-linux-x64.tar.gz /usr/local/
---> Using cache
---> 5fa9985ff9bd
Successfully built 5fa9985ff9bd
Successfully tagged test_a/centos:latest
镜像就创建好了,使用命令查看镜像:
[master@master test]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test_a/centos latest 5fa9985ff9bd 10 days ago 616MB
nginx latest 540a289bab6c 2 months ago 126MB
centos latest 0f3e07c0138f 3 months ago 220MB
如果要上传hub,需要登录帐号:
[master@master test]$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/test/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
我这边已经登录好了,步骤很简单。
接着使用命令上传镜像:
[master@master test]$ docker push test_a/centos_test
The push refers to repository [docker.io/mondu/centos_test]
7c2429fbXXXX: Pushed
1436e4XXXXX: Pushed
1b1d55XXXXX: Pushed
904167XXXXX: Pushed
9e607bXXXXX: Mounted from library/centos
latest: digest: sha256:1f1a83XXXXXb7ede8bcaXXXXX... size: 4544
再到自己的hub帐号下,就可以看到这个镜像了。