❤️❤❣️❤️❤❣️
❤️作者简介:2022新星计划第三季云原生与云计算赛道Top5、华为云享专家、云原生领域潜力新星
博客首页:C站个人主页
作者目的:如有错误请指正,将来会不断的完善笔记,帮助更多的Java爱好者入门,共同进步!
❤️❤❣️❤️❤❣️
本博文一共有7篇,如下
等你对Docker有一定理解的时候可以看高级篇,不过不太建议。
剧透:未来将出云原生技术-Kubernetes(k8s),此时的你可以对Docker进行统一管理、动态扩缩容等等。
看完之后你会对Docker有一定的理解,并能熟练的使用Docker进行容器化开发、以及Docker部署微服务、Docker网络等等。干起来!
Pivotal公司的Matt Stine于2013年首次提出云原生(Cloud-Native)的概念;2015年,云原生刚推广时,Matt Stine在《迁移到云原生架构》一书中定义了符合云原生架构的几个特征:12因素、微服务、自敏捷架构、基于API协作、扛脆弱性;到了2017年,Matt Stine在接受InfoQ采访时又改了口风,将云原生架构归纳为模块化、可观察、可部署、可测试、可替换、可处理6特质;而Pivotal最新官网对云原生概括为4个要点:DevOps+持续交付+微服务+容器。
总而言之,符合云原生架构的应用程序应该是:采用开源堆栈(K8S+Docker)进行容器化,基于微服务架构提高灵活性和可维护性,借助敏捷方法、DevOps支持持续迭代和运维自动化,利用云平台设施实现弹性伸缩、动态调度、优化资源利用率。
(此处摘选自《知乎-华为云官方帐号》)
总而言之:
Docker是一个高性能的容器引擎;
可以把本地源代码、配置文件、依赖、环境通通打包成一个容器即可以到处运行;
使用Docker安装软件十分方便,而且安装的软件十分精简,方便扩展。
sudo docker login --username=游政杰eee registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
Login Succeeded
$ docker login --username=游政杰eee registry.cn-guangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:[镜像版本号]
$ docker push registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:[镜像版本号]
[root@aubin ~]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
[root@aubin ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
-a, --all-tags Push all tagged images in the repository
--disable-content-trust Skip image signing (default true)
-q, --quiet Suppress verbose output
查看镜像
[root@aubin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 6.6 5a035af5ac0c 2 hours ago 176MB
登录阿里云帐号
[root@aubin ~]# docker login --username=游政杰eee registry.cn-guangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
Login Succeeded
$ docker tag 5a035af5ac0c registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
$ docker push registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
[root@aubin ~]# docker push registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.77
The push refers to repository [registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository]
tag does not exist: registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.77
[root@aubin ~]# docker push registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
The push refers to repository [registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository]
11e2b5ff8e45: Pushed
9f54eef41275: Pushed
6.66: digest: sha256:a82e46b4b3a681b744a69df935dccab1ad9d901e257abb297610c9d995f2a83b size: 741
$ docker pull registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:[镜像版本号]
$ docker pull registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
[root@aubin ~]# docker pull registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
6.66: Pulling from my_cloud_namespace/my_cloud_repository
7b1a6ab2e44d: Already exists
58ab36472911: Pull complete
Digest: sha256:a82e46b4b3a681b744a69df935dccab1ad9d901e257abb297610c9d995f2a83b
Status: Downloaded newer image for registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository:6.66
[root@aubin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-guangzhou.aliyuncs.com/my_cloud_namespace/my_cloud_repository 6.66 5a035af5ac0c 3 hours ago
docker run -it 5a035af5ac0c
[root@aubin ~]# docker search registry
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
registry The Docker Registry 2.0 implementation for s… 3564 [OK]
google/docker-registry Docker Registry w/ Google Cloud Storage driv… 48
docker/dtr Docker Trusted Registry 14
deis/registry Docker image registry for the Deis open sour… 12
ibmcom/isam-openldap This image provides a user registry which ca… 10
vmware/registry 6
docker/migrator Tool to migrate Docker images from a v1 regi… 5 [OK]
ibmcom/verify-access-openldap This image provides a user registry which ca… 3
ibmcom/registry-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 2
ibmcom/registry-s390x 1
ibmcom/wazi-code-dev-file A Devfile registry container for IBM® Wazi f… 1
ibmcom/wazi-code-plugin A Plugin registry container for IBM® Wazi fo… 1
ibmcom/registry Docker Image for IBM Cloud private-CE (Commu… 1
snyk/container-registry-agent Container registry agent allows securely con… 1
vmware/registry-photon 0
okteto/registry-configurator 0
docker/trusted-registry-index Search Index for Docker Trusted Registry 0
ibmcom/reg-crawler Docker Image for IBM Cloud Private-CE (Commu… 0
okteto/registry 0
okteto/registry-auth 0
ibmcom/registry-amd64 0
store/microsoft/defaultpublisher This image contains the Azure Industrial IoT… 0
store/microsoft/defaultpublisher This image contains the Azure Industrial IoT… 0
store/microsoft/defaultpublisher This image contains the Azure Industrial IoT… 0
astronomerinc/ap-registry Docker registry for the Astronomer Platform. 0
$ docker pull registry
运行私服库
$ docker run -d -p 5000:5000 -v /youzhengjie/myregistry/:/tmp/registry --privileged=true registry
docker exec -it f82b9b4f63b0 /bin/bash
root@f82b9b4f63b0:/# ifconfig
bash: ifconfig: command not found
更新
$ apt-get update
安装
$ apt-get install net-tools
安装成功了:
root@f82b9b4f63b0:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 20000 bytes 43325081 (43.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13564 bytes 743065 (743.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
退出容器
ctrl p+q
使用docker commit生成新镜像
docker commit -a youzhengjie -m iconfig f82b9b4f63b0 ifconfigubuntu:2.0
查看新镜像
[root@aubin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ifconfigubuntu 2.0 a9683693b1c6 10 seconds ago 176MB
ifconfig: 192.168.184.132就是我的ip
[root@aubin ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.184.132 netmask 255.255.255.0 broadcast 192.168.184.255
inet6 fe80::5c87:5037:8d1d:7650 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:23:28:59 txqueuelen 1000 (Ethernet)
RX packets 169226 bytes 186477878 (177.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84828 bytes 11617557 (11.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
curl -XGET http://ip:5000/v2/_catalog
[root@aubin ~]# curl -XGET http://ip:5000/v2/_catalog
curl: (7) Failed connect to 192.168.184.132:5000; Connection refused
docker run -d -p 5000:5000 -v /youzhengjie/myregistry/:/tmp/registry --privileged=true registry
[root@aubin ~]# curl -XGET http://ip:5000/v2/_catalog
{"repositories":[]}
[root@aubin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ifconfigubuntu 2.0 a9683693b1c6 12 minutes ago 176MB
$ docker tag ifconfigubuntu:2.0 192.168.184.132:5000/configubuntu:3.3
$ docker push 192.168.184.132:5000/configubuntu:3.3
[root@aubin ~]# docker push 192.168.184.132:5000/configubuntu:3.3
The push refers to repository [192.168.184.132:5000/configubuntu]
Get "https://192.168.184.132:5000/v2/": http: server gave HTTP response to HTTPS client
解决方法
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://u01jo9qv.mirror.aliyuncs.com"],
"insecure-registries":["192.168.184.132:5000"]
}
$ systemctl restart docker
docker run -d -p 5000:5000 -v /youzhengjie/myregistry/:/tmp/registry --privileged=true registry
$ docker push 192.168.184.132:5000/configubuntu:3.3
[root@aubin ~]# docker push 192.168.184.132:5000/configubuntu:3.3
The push refers to repository [192.168.184.132:5000/configubuntu]
c45dd515854c: Pushed
9f54eef41275: Pushed
3.3: digest: sha256:b59453f0561ec74a3703a16f7de9c41c34c91cb361410abeb9ea82da2b73f67a size: 741
查询私服库
[root@aubin ~]# curl -XGET http://ip:5000/v2/_catalog
{"repositories":["configubuntu"]}
[root@aubin ~]# docker pull 192.168.184.132:5000/configubuntu:3.3
3.3: Pulling from configubuntu
7b1a6ab2e44d: Already exists
b71359a660f3: Pull complete
Digest: sha256:b59453f0561ec74a3703a16f7de9c41c34c91cb361410abeb9ea82da2b73f67a
Status: Downloaded newer image for 192.168.184.132:5000/configubuntu:3.3
192.168.184.132:5000/configubuntu:3.3
docker run -it -v /host/data:/container/data --name=ub1 ubuntu /bin/bash
root@5ae5cce418d7:/container/data# touch a.txt
[root@aubin data]# ls
a.txt
[root@aubin data]# echo "hello world" > a.txt
[root@aubin data]# cat a.txt
hello world
root@5ae5cce418d7:/container/data# cat a.txt
hello world
docker rm -f ub1
[root@aubin data]# echo "you are best" > a.txt
[root@aubin data]# cat a.txt
you are best
一定要绑定到我们刚刚宿主机存放数据的目录/host/data
docker run -it -v /host/data:/container1/data --name=ub1 ubuntu /bin/bash
root@7cf8eedf967c:/container1/data# ls
a.txt
root@7cf8eedf967c:/container1/data# cat a.txt
you are best
nice,大功告成,数据被完美的恢复了!!!
[root@aubin data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ae5cce418d7 ubuntu "/bin/bash" 59 minutes ago Up 59 minutes ub1
[root@aubin data]# docker inspect ub1
省略...
"Mounts": [
{
"Type": "bind",
"Source": "/host/data",
"Destination": "/container/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
省略...
docker run -it -v /host/data:/container1/data:ro --name=ub2 ubuntu /bin/bash
如果我们强行要写会发生什么?
root@b2c4f74224ae:/container1/data# echo "very good" > a.txt
bash: a.txt: Read-only file system
结论:我们会发现写入失败了!
❤️本章结束,我们下一章见❤️