Docker使用:将镜像上传至阿里云
0x01 注册阿里云账户阿里云官方网站链接:https://dev.aliyun.com0x02 登陆账户0x03 管理Docker Hub镜像站点:配置Docker加速器链接:https://cr.console.aliyun.com/?spm=5176.1971733.0.2.duOGn4#/accelerator0x04 创建镜像仓库的命名空间例如:trrinity链接:htt...
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
在某一个目录下面创建一个专门存放此demo的目录,也就是Dockerfile所在的context:
[root@docker ~]# mkdir docker_demo
[root@docker ~]# cd docker_demo/
[root@docker docker_demo]# touch Dockerfile
[root@docker docker_demo]# pwd
/root/docker_demo
[root@docker 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这个目录下:
[root@docker 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版:
复制代码
# base image
FROM centos
# MAINTAINER
MAINTAINER xxxxxx@163.com
# 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目录情况:
[root@docker 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文件)
构建成功后,查看新构建的镜像:
5、然后使用构建的镜像启动一个container,开启nginx服务,将port映射到本机9999端口上:
[root@docker docker_demo]# docker run -d -p9999:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
e4b6e4846dedc6f130e028701c84828a635f3b367c0d500ebd947de16b1be0b2
再次查看端口映射信息:
docker port
于是进行访问:
于是基于Dockerfile的一个简单实例构建完成,现在基于这个Dockerfile文件依次添加其他的指令进行构建
添加ENV环境变量指令:
# base image
FROM centos
# MAINTAINER
MAINTAINER xxx@163.com
# 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
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
由于在构建的过程中docker会采用缓存的机制,上面的构建过程中包含很多using cache,所以这次构建非常快,如果需要重新构建,不想使用cache需要添加–no-cache
–no-cache Do not use cache when building the image
使用v2版本的镜像启动一个container:
docker run -d -p81:80 centos_nginx:v2 nginx -g "daemon off;"
上述启动容器的过程中使用的命令docker run -d -p81:80 centos_nginx:v2 nginx -g "daemon off;"为什么这里使用的nginx而不是
/usr/local/nginx/sbin/nginx,因为在Dockerfile文化中执行了PATH=/usr/local/nginx/sbin:$PATH,添加到了环境变量
添加指令CMD:
# base image
FROM centos
# MAINTAINER
MAINTAINER xxxxxx@163.com
# 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
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD /bin/sh -c 'nginx -g "daemon off;"'
然后进行构建:
[root@docker docker_demo]# docker build -t centos_nginx:v3 .
然后基于v3版本的镜像启动一个container:
[root@docker docker_demo]# docker run -d -p82:80 centos_nginx:v3
新增加的CMD /bin/sh -c 'nginx -g “daemon off;”'表示
当启动一个container时默认运行的命令,如果在启动container时赋予了command的话,那么定义的CMD中的命令将不会被执行,而会去执行command的命令
添加entrypoint指令:
# base image
FROM centos
# MAINTAINER
MAINTAINER xxxxxx@163.com
# 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
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g “daemon off;”
而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)
开始进行构建v4版本:
[root@docker docker_demo]# docker build -t centos_nginx:v4 .
查看新建的镜像:
[root@docker docker_demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v4 6c5128aaff05 4 minutes ago 464MB
为v4版本的镜像启动一个container:
# shannon @ localhost [21:05:48] docker run -d -p4444:80 centos_nginx:v4
# shannon @ localhost [21:05:48] docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
887e2aab1e6e centos_nginx:v5 "nginx -g 'daemon off" 2 seconds ago Up 2 seconds 0.0.0.0:4444->80/tcp frosty_leakey
添加VOLUME指令:
# base image
FROM centos
# MAINTAINER
MAINTAINER xxxxxx@163.com
# 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
# mount a dir to container
VOLUME ["/data"]
# 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
# setup PATH
ENV PATH /usr/local/nginx/sbin:$PATH
# EXPOSE
EXPOSE 80
# the command of entrypoint
ENTRYPOINT ["nginx"]
CMD ["-g"]
开始进行构建:
[root@docker docker_demo]# docker build -t centos_nginx:v5 .
查看v5版本的镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v5 959fdf4d4288 35 seconds ago 464MB
利用该镜像启动一个container:
[root@docker ~]# docker run -d -p 86:80 --name=nginx6 centos_nginx:v5 -g "daemon off;"
6c15a9e93fb1421bdb7eddaabe439996e97415e85a003f80c1d8b4b2c5ee3ffa
查看启动的容器状态:
[root@docker ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c15a9e93fb1 centos_nginx:v5 "nginx -g 'daemon ..." 4 seconds ago Up 4 seconds 0.0.0.0:86->80/tcp nginx6
利用docker exec进入到container中,查看是否存在卷/data:
docker inspect 查看通过该dockerfile创建的镜像生成的容器
这个/data挂载的目录对应本机source的这个目录:
现在在本机/data上的这个目录创建一个文件:
[root@docker _data]# touch wadeson.sh
[root@docker _data]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 1 21:45 wadeson.sh
然后切换到container中查看是否存在这个文件:
[root@6c15a9e93fb1 /]# ll /data/
total 0
-rw-r--r--. 1 root root 0 Nov 2 01:45 wadeson.sh
war包上传
shannon @ localhost in ~/yann/docker-tomcat on git:master x [7:15:30]
docker pull tomcat
shannon @ localhost in ~/yann/docker-tomcat on git:master x [7:15:30]
vi Dockerfile
FROM tomcat
MAINTAINER "dora <522268397@qq.com>"
ADD Dora.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
# shannon @ localhost in ~/yann/docker-tomcat on git:master x [7:15:30]
$ docker build -t tomcat:0.0.1 .
0x06 阿里云操作指南
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
更多推荐
所有评论(0)