1-Jenkins - 容器迁移
before
基于jenkinsci/blueocean:latest镜像的Jenkins
先说要干什么?
我在香港的阿里云服务器搭建了Jenkins的容器环境,现在要在本机的虚拟中使用该Jenkins容器,因为下载插件太难了......
操作流程
- 在香港阿里云服务器上根据配置好环境的Jenkins容器创建一个镜像
- 将该镜像上传到docker hub
- 本机的虚拟机从docker hub中拉取镜像,在本地运行一个容器
准备工作
创建仓库
提前在docker hub上创建一个仓库(如果你没有仓库的话)
当你成功登陆docker hub后,点击Repositories选项,点击Create Repository
来创建:
填写镜像名称和描述及设置公有还有私有:
登录到docker hub
在远程服务器中,首先要登录到docker hub,后续直接上传即可。
[root@r ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: wangzhangkai
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
制作镜像
首选,我有一个目前正在运行的并且配置好各种环境的容器s267
:
[root@r ~]# docker ps | grep s267
a421ad615576 jenkinsci/blueocean:latest "/sbin/tini -- /usr/…" 24 hours ago Up 24 hours 0.0.0.0:50000->50000/tcp, 0.0.0.0:6010->8080/tcp s267
我根据该s267
容器导出一个镜像:
[root@r ~]# docker commit --author zhangkai --message "基于jenkinsci/blueocean:latest镜像的新镜像,配置好了Python环境和allure等插件" s267 tmpjenkins:1.0
sha256:9fbf8dc320dde695898a5b0956d982f7d9a62a66453825f243b690e3583d4851
PS:这里你也可以使用Dockerfile
来完成
接下来,使用tag
命令打个标签:
[root@r ~]# docker tag tmpjenkins:1.0 wangzhangkai/jenkins:1.0
[root@r ~]# docker images | grep jenkins
tmpjenkins 1.0 9fbf8dc320dd 34 seconds ago 805MB
wangzhangkai/jenkins 1.0 9fbf8dc320dd 34 seconds ago 805MB
jenkinsci/blueocean latest 789f2766377f 40 hours ago 567MB
上述三个镜像:
tmpjenkins:1.0
是使用commit
导出的镜像。wangzhangkai/jenkins:1.0
是使用tag
命令打得标签,我们后续也就是将这个镜像push
到docker hub的仓库去。jenkinsci/blueocean:latest
是容器s267
的基础镜像。
另外,tag
命令打标签,必须是your username/image:tag
这种格式。
上传镜像到docker hub
现在,就可以直接使用push
将服务器上镜像推送到docker hub的仓库中:
[root@r ~]# docker push wangzhangkai/jenkins:1.0
The push refers to repository [docker.io/wangzhangkai/jenkins]
a6ef5fec03b9: Pushed
f8ea01675f0a: Mounted from jenkinsci/blueocean
440f8c788004: Mounted from jenkinsci/blueocean
f416194dc11c: Mounted from jenkinsci/blueocean
3985c8e5583d: Mounted from jenkinsci/blueocean
aa0da59b5e96: Mounted from jenkinsci/blueocean
213d58243292: Mounted from jenkinsci/blueocean
c26aca99a218: Mounted from jenkinsci/blueocean
1cc8898ea742: Mounted from jenkinsci/blueocean
0be3891e9475: Mounted from jenkinsci/blueocean
f5abc16eaea6: Mounted from jenkinsci/blueocean
83320167bce4: Mounted from jenkinsci/blueocean
dedc7367e939: Mounted from jenkinsci/blueocean
ceaf9e1ebef5: Mounted from jenkinsci/blueocean
9b9b7f3d56a0: Mounted from jenkinsci/blueocean
f1b5933fe4b5: Mounted from jenkinsci/blueocean
1.0: digest: sha256:8778ae21ef9f2956117546e554ce2f35c7e8a29d9e8358f6ffa6d7ff01acd2a2 size: 3669
推送过程较慢,毕竟是800M左右的镜像!所以,这里也说点题外话,如果有可能尽量的不要这么搞,因为最开始的jenkinsci/blueocean:latest
镜像才500多M,在经过一顿七上八下,BABA后,就到了800多M,等时间长了,鬼知道这个新镜像到底多了哪些东西!
现在,在你的docker hub的镜像仓库中,就能发现新上传的镜像了:
从docker hub拉取镜像
使用pull
拉取镜像:
[root@C ~]# docker pull wangzhangkai/jenkins:1.0
1.0: Pulling from wangzhangkai/jenkins
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
c2274a1a0e27: Pull complete
ad6179c1f162: Pull complete
8948507790d8: Pull complete
7f023dd8e094: Pull complete
015f995a850b: Pull complete
2ed134814058: Pull complete
efd571beed1e: Pull complete
e077c85d8ab4: Pull complete
b9d3f0bd0869: Pull complete
d573fc03ec9e: Pull complete
38bf486c8db9: Pull complete
d81edeb9528c: Pull complete
9fed8b71ad42: Pull complete
c0b333f119e9: Pull complete
Digest: sha256:8778ae21ef9f2956117546e554ce2f35c7e8a29d9e8358f6ffa6d7ff01acd2a2
Status: Downloaded newer image for wangzhangkai/jenkins:1.0
docker.io/wangzhangkai/jenkins:1.0
[root@C ~]# docker images |grep jenkins
wangzhangkai/jenkins 1.0 9fbf8dc320dd 42 minutes ago 805MB
启动镜像:
docker run \
-u root \
--name myjenkins \
--restart=always \
-d \
-p 6010:8080 \
-p 50000:50000 \
--env JAVA_OPTS="-Xmx1024m" \
-e JAVA_OPTS=-Duser.timezone=Asia/Shanghai \
-v /etc/localtime:/etc/localtime \
-v /docker_data/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
wangzhangkai/jenkins:1.0
欢迎斧正,that's all,see also: