Docker | 虚拟机 | |
---|---|---|
启动速度 | 秒级 | 分钟级 |
运行性能 | 接近原生 | 5%左右的损失 |
磁盘占用 | MB | GB |
数量 | 成百上千 | 一般几十台 |
隔离性 | 进程级别 | 系统级别 |
操作系统 | 只支持Linux | 几乎所有 |
封装程度 | 只打包项目代码和依赖关系,共享宿主机内核 | 完整的操作系统 |
官方文档安装地址: https://docs.docker.com/engine/install/centos/
yum install yum-utils yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce
systemctl start docker systemctl enable docker
[root@localhost yum.repos.d]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:09ca85924b43f7a86a14e1a7fb12aadd75355dea7cdc64c9c80fb2961cc53fe7 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
一个不包括linux内核的简易的Linux系统
[root@localhost ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] } [root@localhost ~]# systemctl restart docker
查看本地所有的镜像
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 months ago 13.3kB
查看一个镜像的创建历史
[root@localhost ~]# docker image history hello-world IMAGE CREATED CREATED BY SIZE COMMENT feb5d9fea6a5 3 months ago /bin/sh -c #(nop) CMD ["/hello"] 0B <missing> 3 months ago /bin/sh -c #(nop) COPY file:50563a97010fd7ce… 13.3kB
查看一个镜像的详细信息
[root@localhost ~]# docker image inspect hello-world
从仓库中拉取一个镜像
[root@localhost ~]# docker image pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
删除一个镜像
[root@localhost ~]# docker image rm hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:09ca85924b43f7a86a14e1a7fb12aadd75355dea7cdc64c9c80fb2961cc53fe7 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
给一个镜像打标签
[root@localhost ~]# docker tag centos:latest centos:8.4 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos 8.4 5d0da3dc9764 3 months ago 231MB centos latest 5d0da3dc9764 3 months ago 231MB
保存一个镜像到本地
[root@localhost ~]# docker image save centos:8.4 > centos:8.4.tar.gz [root@localhost ~]# ll centos:8.4.tar.gz -rw-r--r--. 1 root root 238581248 Jan 13 00:15 centos:8.4.tar.gz
导入一个镜像
[root@localhost ~]# docker image rm centos:8.4 Untagged: centos:8.4 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB [root@localhost ~]# docker load < centos\:8.4.tar.gz Loaded image: centos:8.4 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos 8.4 5d0da3dc9764 3 months ago 231MB centos latest 5d0da3dc9764 3 months ago 231MB
导出一个容器
docker export 'CONTAINER ID' > xxxx.tar
导入容器创建镜像
docker image import xxxx.tar 名字:标签
创建容器命令:
[root@localhost ~]# docker run -itd --name centos centos:8.4
选项 | 描述 |
---|---|
-i | 交互式 |
-t | 分配一个伪终端 |
-d | 运行容器到后台 |
-a list | 附加到运行的容器 |
--dns list | 设置DNS服务器 |
-e | 设置环境变量 |
--env-file list | 从文件读取环境变量 |
-p list | 发布容器端口到主机 |
-P | 发布容器所有EXPOSE的端口到宿主机的随机端口 |
-h | 设置容器主机名 |
--ip string | 指定容器IP,只能用于自定义网络 |
--link list | 添加连接到另一个容器 |
--network | 连接容器到一个网络 |
--mount | 挂载宿主机分区到容器 |
-v | 挂载宿主机目录到容器 |
--restart string | 容器退出时重启策略,默认no |
--add-host list | 添加其他主机到容器中/etc/hosts |
使用方法: docker container [选项] 选项: ls 列出容器 inspect 显示一个或多个容器详细信息 attach 附加本地标准输入,输出和错误到一个运行的容器 exec 在运行容器中执行命令,也可以进入一个容器 commit 创建一个新镜像来自一个容器 cp 拷贝文件或者文件夹到容器 logs 获取一个容器的日志 port 列出或指定容器端口映射 stats 显示容器资源使用统计 top 显示一个容器运行的进程 update 更新一个或多个容器的配置 stop/start 停止/启动一个或多个容器 rm 删除一个或多个容器
Docker提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts和tmpfs
volumes:Docker管理宿主机文件系统的一部分(/var/lib/dokcer/volumes)
bind mounts:可以存储在宿主机系统的任意位置
tmpfs:挂载存储在宿主机内存中,而不会写入宿主机的文件系统
[root@192 ~]# docker volume --help Usage: docker volume COMMAND Manage volumes Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove all unused local volumes rm Remove one or more volumes Run 'docker volume COMMAND --help' for more information on a command.
挂载实例:
docker volume create nginx-vol
docker run -d -it --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
docker run -d -it --name=nginx02 --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
注意:
如果挂载目标在容器中非空目录,则改目录现有内容将会被隐藏。
Docker支持5种网络模式
bridge:默认网络,Docker启动后默认创建一个docker0网桥,默认创建的容器也是添加到这个网桥中。
host:容器不会获得一个独立的network namespace,而是与宿主机共用一个
none:获取独立的network namespace,但不为容器进行任何网络配置
container:与指定的容器使用同一个network namespace,网卡配置也都是相同的
自定义:自定义网桥,默认与bridge网络一样。
指令 | 描述 |
---|---|
FROM | 构建新镜像基于哪个镜像,例如:FROM centos:7 |
MAINTAINER | 描述这个Dockerfile的作者信息,例如:MAINTAINER xxxxx xxxxxxx@163.com |
RUN | 构建镜像时运用的shell命令,例如:RUN yum install httpd |
CMD | 运行容器时执行的shell命令 |
EXPOSE | 声明容器运行的服务端口 |
ENV | 设置容器内环境变量,例如设置JAVA_HOME |
ADD | 拷贝文件或目录到镜像,自动解压压缩包,例如:ADD html.tar.gz /usr/local/html |
USER | 为RUN和CMD设置运行用户 |
COPY | 拷贝文件或目录到镜像,用法同上 |
WORKDIR | 为RUN和CMD设置工作目录 |
HEALTHCHECK | 健康检查 |
ARG | 构建镜像时设置一个变量,可以在build的时候导入 |
docker build 参数: -t #镜像名称 -f #指定Dockerfile文件位置
https://github.com/goharbor/harbor/releases
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose docker-compose version
tar -xzvf harbor-offline-installer-v1.4.0.tgz
创建证书
harbor23.com这里是我harbor仓库的域名,即harbor配置文件中hostname的值,也可以写ip
cd harbor mkdir ssl cd ssl openssl genrsa -out ca.key 4096 openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/CN=harbor23.com" -key ca.key -out ca.crt openssl genrsa -out server.key 4096 openssl req -new -sha512 -subj "/CN=harbor23.com" -key server.key -out server.csr cat > v3.ext <<-EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth subjectAltName = @alt_names [alt_names] DNS.1=harbor23.com EOF openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt
hostname = harbor23.com ui_url_protocol = https ssl_cert = ./ssl/server.crt ssl_cert_key = ./ssl/server.key harbor_admin_password = 123
./prepare ./install.sh
将新建的用户加入test项目
mkdir /etc/docker/certs.d/harbor23.com -p scp 192.168.168.130:/root/harbor/ssl/server.crt /etc/docker/certs.d/harbor23.com
[root@192 harbor23.com]# cat /etc/docker/daemon.json { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn" ], "insecure-registries": [ "harbor23.com" ] }
[root@192 harbor23.com]# docker login harbor23.com Username: xcn 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 tag nginx harbor23.com/test/nginx:v1 docker push harbor23.com/test/nginx:v1
docker pull harbor23.com/test/nginx:v1