1、编写一个Dockerfile
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# mkdir Docker
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# cd Docker/
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# cat Dockerfile
FROM alpine
CMD pingbaidu.com
2、打包镜像文件
# -t : tag标签。镜像名字
# . : 当前目录下工作
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker build -t hello .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
latest: Pulling from library/alpine
cbdbe7a5bc2a: Already exists
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image foralpine:latest
---> f70734b6a266
Step 2/2 : CMD pingbaidu.com
---> Running in0e36a3c24806
Removing intermediate container 0e36a3c24806
---> 316d9e8b09e0
Successfully built 316d9e8b09e0
Successfully tagged hello:latest
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello latest 316d9e8b09e0 17seconds ago 5.61MB
3、指定文件名打包镜像文件
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker build -f Dockerfile2 -t hello:v1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
---> f70734b6a266
Step 2/2 : CMD pingbaidu.com
---> Using cache
---> 316d9e8b09e0
Successfully built 316d9e8b09e0
Successfully tagged hello:v1.0
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello latest 316d9e8b09e0 7minutes ago 5.61MB
hello v1.0 316d9e8b09e0 7minutes ago 5.61MB
4、创建镜像
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker run -d --name haha hello
0fcab7e24449434e3971cd77d9a1f3489ce947bb4baddbd52a9e3f5e179f3868
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fcab7e24449 hello "/bin/sh -c 'ping ba…" 33seconds ago Up 32seconds haha
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker logs haha
PING baidu.com (39.156.69.79): 56data bytes
64bytes from 39.156.69.79: seq=0ttl=50time=6.057 ms
64bytes from 39.156.69.79: seq=1ttl=50time=6.023 ms
64bytes from 39.156.69.79: seq=2ttl=50time=6.036 ms
5、使用Docker hub镜像仓库
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker push --help
Usage:docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image signing (default true)
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker login -u icodingallen
Password:
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in/root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker push hello:v1.0 # 外网访问特别慢,无法推送
The push refers to repository [docker.io/library/hello]
3e207b409db3: Preparing
denied: requested access to the resource is denied
6、使用阿里云镜像仓库
1.登录阿里云仓库
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# sudo docker login --username=运维猫 registry.cn-beijing.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in/root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
2.创建命名空间,用于团队的隔离
3.创建镜像仓库,保存镜像各个版本(先有命名空间,再有镜像仓库)
4.将本地镜像变成阿里云镜像
# 给镜像打标签
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker tag hello:v1.0 registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello:v1.3
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello latest 316d9e8b09e0 About an hour ago 5.61MB
hello v1.0 316d9e8b09e0 About an hour ago 5.61MB
registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello v1.3 316d9e8b09e0 About an hour ago 5.61MB
5.上传镜像到镜像仓库
[root@iZ2zeir6vcnpz8qw3t455tZ Docker]# docker push registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello:v1.3
The push refers to repository [registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello]
3e207b409db3: Pushed
v1.3: digest: sha256:df378a58f23a66f7283ae96f9541ea942e41aab0a9814af1f6c78b0415fd6138 size: 528
6.查看镜像版本
7.拉取镜像到本地
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker pull registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello:v1.3
v1.3: Pulling from docker-yunweimao/hello
cbdbe7a5bc2a: Already exists
Digest: sha256:df378a58f23a66f7283ae96f9541ea942e41aab0a9814af1f6c78b0415fd6138
Status: Downloaded newer image forregistry.cn-beijing.aliyuncs.com/docker-yunweimao/hello:v1.3
registry.cn-beijing.aliyuncs.com/docker-yunweimao/hello:v1.3
如果文章有任何错误欢迎不吝赐教,其次大家有任何关于运维的疑难杂问,也欢迎和大家一起交流讨论。关于运维学习、分享、交流,笔者开通了微信公众号【运维猫】,感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的小圈子,一起学运维知识。群主还经营一家Orchis饰品店,喜欢的小伙伴欢迎????前来下单。
扫描二维码
获取更多精彩
运维猫公众号
有需要技术交流的小伙伴可以加我微信,期待与大家共同成长,本人微信:
扫描二维码
添加私人微信
运维猫博主
扫码加微信
最近有一些星友咨询我知识星球的事,我也想继续在星球上发布更优质的内容供大家学习和探讨。运维猫公众号平台致力于为大家提供免费的学习资源,知识星球主要致力于即将入坑或者已经入坑的运维行业的小伙伴。
点击阅读原文 查看更多精彩内容!!!