Docker镜像上传至阿里云

0x01 注册阿里云账户

阿里云官方网站链接:https://dev.aliyun.com

0x02 登陆账户
0x03 管理Docker Hub镜像站点:配置Docker加速器

链接:https://cr.console.aliyun.com/cn-hangzhou/mirrors

0x04 创建镜像仓库的命名空间

例如:trrinity

链接:https://cr.console.aliyun.com/cn-hangzhou/namespaces

0x05 创建镜像仓库

例如:image-test

链接:https://cr.console.aliyun.com/cn-hangzhou/repositories

0x06 操作指南

先查看下本地的镜像,选一个作为base image:

docker images
Docker镜像上传至阿里云
在某一个目录下面创建一个专门存放此demo的目录,也就是Dockerfile所在的context:

[[email protected] ~]# mkdir docker_demo
[[email protected] ~]# cd docker_demo/
[[email protected] docker_demo]# touch Dockerfile
[[email protected] docker_demo]# pwd
/root/docker_demo
[[email protected] docker_demo]# ll
total 0
-rw-r--r--. 1 root root 0 Nov  1 14:34 Dockerfile

接下来就开始编写Dockerfile文件了(注意Dockerfile的D需要大写)

这里以编译nginx提供web服务来构建新的镜像

1、下载nginx源码包到docker_demo这个目录下:

[[email protected] docker_demo]# ll
total 960
-rw-r--r--. 1 root root      0 Nov  1 04:34 Dockerfile
-rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz

2、以下是编写好的Dockerfile v1版:
复制代码

[[email protected] docker_demo]# cat Dockerfile
# base image
FROM centos

# MAINTAINER
MAINTAINER [email protected]

# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src

# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx

# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2

# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install

EXPOSE 80

3、查看docker_demo目录情况:

[[email protected] docker_demo]# ll
total 964
-rw-r--r--. 1 root root   1112 Nov  1 04:58 Dockerfile
-rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz

4、执行docker build进行构建:

docker build -t centos_nginx:v1 .

后面的.代表的是相对路径的当前目录,如果需要全路径则为/root/docker_demo(就是找到Dockerfile文件)

构建成功后,查看新构建的镜像:
![在这里插入图片描述](https://img-blog.****img.cn/20190203132604196.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2dpdGh1Yl8zNzIxNjk0NA==,size_16,color_FFFFFF,t_70

5、然后使用构建的镜像启动一个container,开启nginx服务,将port映射到本机9999端口上:

[[email protected] docker_demo]# docker run -d -p9999:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
e4b6e4846dedc6f130e028701c84828a635f3b367c0d500ebd947de16b1be0b2

再次查看端口映射信息:
docker port
Docker镜像上传至阿里云
于是进行访问:
Docker镜像上传至阿里云
于是基于Dockerfile的一个简单实例构建完成,现在基于这个Dockerfile文件依次添加其他的指令进行构建

Docker镜像上传至阿里云
1)远程登录阿里云docker registry:
$ sudo docker login --username=trrinity registry.cn-hangzhou.aliyuncs.com
注:登录registry的用户名是您的阿里云账号全名,密码是开通namespace时设置的密码。

2)从registry中拉取镜像:
$ sudo docker pull registry.cn-hangzhou.aliyuncs.com/msj/image-test:[镜像版本号]
3)将镜像推送到registry:
$ sudo docker login --username=mashujie registry.cn-hangzhou.aliyuncs.com $ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/msj/image-test:[镜像版本号] $ sudo docker push registry.cn-hangzhou.aliyuncs.com/msj/image-test:[镜像版本号]
其中[ImageId],[镜像版本号]请你根据自己的镜像信息进行填写。

0x07 终端执行脚本代码

#登陆命令
$ docker login --username=mashujieregistry.cn-hangzhou.aliyuncs.com
#查看主机镜像
$ docker images
#复制镜像ID并设置tag (或者tag repository:tag)
$ docker tag 96eecaf1019a registry.cn-hangzhou.aliyuncs.com/msj/image-test:helloimage01 $ docker tag image01:01registry.cn-hangzhou.aliyuncs.com/msj/image-test:helloimage01
#上传镜像到阿里云镜像仓库
$ docker push registry.cn-hangzhou.aliyuncs.com/msj/image-test:helloimage01