什么是Docker?
Docker是一个构建在LXC之上,基于进程容器的轻量级VM解决方案,实现了一种应用程序级别的资源隔离及配额。Docker起源于PaaS提供商dotCloud 基于go语言开发,遵从Apache2.0开源协议。
Docker 自开源后受到广泛的关注和讨论,以至于 dotCloud 公司后来都改名为 Docker Inc。
简单的理解,Docker类似于集装箱,各式各样的货物,经过集装箱的标准化进行托管,而集装箱和集装箱之间没有影响。也就是说,Docker平台就是一个软件集装箱化平台,这就意味着我们自己可以构建应用程序,将其依赖关系一起打包到一个容器中,然后这容器就很容易运送到其他的机器上进行运行,而且非常易于装载、复制、移除,非常适合软件弹性架构。
为什么要使用Docker?
性能上比VM好
一台牛X的服务器,VMWare中启动10个虚拟机,基本上就不能再做其他事情了,大部分CPU和内存资源都耗费在VM内的OS了。而Docker将APP直接放在OS上面,省了大量内存和CPU资源占用,但在效果(沙箱)上与虚拟机完全一致
重复使用、隔离应用
1、根据特定的开发需求,生成一个标准的开发和测试环境,比如机构库的开发,我们需要的环境包括:JDK + Postgresql + tomcat + Nginx + ...
当我们新来一个开发或者测试人员,我们根据对应的环境,生成一个属于这个开发/测试人员的虚拟机(暂且如此称呼吧),编译部署代码,测试系统都隔离在自身负责的范围。
2、对于系统平台部分,数据库和平台部分,安装在一个环境中,然后我们开发的不同模块都连接到这个虚拟机中公开的接口上,这样,这个环境对我们开发产生积极的影响,核心部分隔离了,应用开发不用再繁琐的部署平台的其他部分了,及保证了核心代码安全,又可以让开发人员专注自身业务而不用管平台底层部分。
Docker熟识
Docker部署及配置
-
安装docker工具
$ cd /etc/yum.repos.d
$ wget https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
$ sed -i 's#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g' docker-ce.repo
$ yum -y install docker-ce
$ systemctl enable --now docker.service
- 启动第一个容器
$ docker container run -itd -p 8080:80 nginx:1.21
Unable to find image 'nginx:1.21' locally
1.21: Pulling from library/nginx
1fe172e4850f: Pull complete
35c195f487df: Pull complete
213b9b16f495: Pull complete
a8172d9e19b9: Pull complete
f5eee2cb2150: Pull complete
93e404ba8667: Pull complete
Digest: sha256:859ab6768a6f26a79bc42b231664111317d095a4f04e4b6fe79ce37b3d199097
Status: Downloaded newer image for nginx:1.21
28a074f14de734d565b61d49f8dd1fd005a7da696b381ec3514fe89449ae96b3
- 配置镜像加速器
$ vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
$ systemctl daemon-reload
$ systemctl restart docker.service
Docker基本指令
-
查看docker版本
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 25.0.3
API version: 1.39 (downgraded from 1.44)
Go version: go1.21.6
Git commit: 4debf41
Built: Tue Feb 6 21:17:10 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 18.09.1
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 4c52b90
Built: Wed Jan 9 19:06:30 2019
OS/Arch: linux/amd64
Experimental: false
- 基础资源查看
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31bdb0575074 nginx:1.21 "/docker-entrypoint.…" About a minute ago Up 58 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp romantic_elion
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.21 fa5269854a5e 12 days ago 142MB
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
47ea72ec86c9 bridge bridge local
af2d08d73c78 host host local
166d963f62c8 none null local
$ docker volume ls
DRIVER VOLUME NAME
- 登录远程仓库, 获取拉镜像的权限
$ 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: hlions
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
$ docker logout
Removing login credentials for https://index.docker.io/v1/
- 搜索镜像
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 19668 [OK]
unit Official build of NGINX Unit: Universal Web … 24 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 88
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 141
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 38
nginxinc/nginx-s3-gateway Authenticating and caching gateway based on … 6
nginx/unit This repository is retired, use the Docker o… 64
nginx/nginx-ingress-operator NGINX Ingress Operator for NGINX and NGINX P… 2
nginxinc/amplify-agent NGINX Amplify Agent docker repository 1
nginx/nginx-quic-qns NGINX QUIC interop 1
nginxinc/ingress-demo Ingress Demo 4
nginxproxy/nginx-proxy Automated nginx proxy for Docker containers … 132
nginxproxy/acme-companion Automated ACME SSL certificate generation fo… 130
bitnami/nginx Bitnami nginx Docker Image 183
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 32
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 112
nginxinc/nginmesh_proxy_debug 0
nginxproxy/docker-gen Generate files from docker container meta-da… 16
nginxinc/mra-fakes3 0
kasmweb/nginx An Nginx image based off nginx:alpine and in… 7
rancher/nginx-ingress-controller 12
nginxinc/nginmesh_proxy_init 0
nginxinc/mra_python_base 0
nginxinc/ngx-rust-tool 0