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.创建命名空间,用于团队的隔离

Docker镜像仓库_java

3.创建镜像仓库,保存镜像各个版本(先有命名空间,再有镜像仓库)

Docker镜像仓库_java_02

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.查看镜像版本

Docker镜像仓库_java_03

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