提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 1 Docker镜像加速配置;
- 2 Docker镜像操作
- 3 Docker容器操作
- 3.8.2 cp复制文件或目 录
- 先参考 https://www.yisu.com/zixun/322030.html 挂截了一个给oracle用,文件查看读取都可以,就是引起oracle数据库没法正常启动,后面搞明白了再补充这块
- 4 私有镜像仓库 Harbor安装---------
- curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose ## 添加执行权限
- docker-compose -version ##查看版本
- wget https://github.com/goharbor/harbor/releases/download/v2.3.0-rc3/harbor-offline-installer-v2.3.0-rc3.tgz
- vim harbor.cfg
- https related config
- https port for harbor, default is 443
- port: 443
- The path of cert and key files for nginx
- certificate: /your/certificate/path
- private_key: /your/private/key/path
- 总结
前言
1 Docker镜像加速配置;
2 Docker镜像常用操作;
3 Dcoker容器常用操作。
4 容器中
1 Docker镜像加速配置;
1 启动docker
开始前,请先确认下docker服务已经启动好了
启动docker
# systemctl start docker
或者
# service docker start #启动docker
# chkconfig docker on #加入开机启动**
1.1 镜像加速器
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:
1 网易云加速器 https://hub-mirror.c.163.com
2 阿里云加速器(需登录账号获取): https://cr.console.aliyun.com/cn-hangzhou/mirrors
国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务,具体请参考官方文档。
在CentOS7系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
[root@docker01 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://hub-mirror.c.163.com"
]
}
-------阿里云的----
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://****.mirror.aliyuncs.com"]
}
----------
注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
1 systemctl daemon-reload
2 systemctl restart docker
检查加速器是否生效
执行如下命令,如果从结果中看到了如下内容,说明配置成功。
[root@docker01 ~]# docker info # 显示整个系统的信息
....
Registry Mirrors:
https://hub-mirror.c.163.com/
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
2 Docker镜像操作
说明:Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像。
2.1 search搜索镜像**
[root@localhost ives]# docker search centos
2.2 pull从镜像中心下载镜像
# 格式:docker pull <image_name>:<tag> ,如果没有tag,默认为 latest
[root@docker01 ~]# docker pull centos:latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
2.3 push推送镜像到镜像中心*
格式:docker push <image_name>:<tag>
[root@docker01 ~]# docker push registry.cn-beijing.aliyuncs.com/google_registry/centos:latest
说明:如果有疑问可先忽略,后面搭建私有仓库文章会再次说明的。
2.4 images列出镜像
# docker images # 或者 docker image ls
[root@localhost ives]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
centos latest 300e315adb2f 5 months ago 209MB
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g latest 3fa112fd3642 5 years ago 6.85GB
[root@localhost ives]# docker image ls ##没下载centos前查看的
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g latest 3fa112fd3642 5 years ago 6.85GB
2.5 save镜像保存到本地*
格式:docker save -o <保存的文件名> <image_name:tag>|<image_id>
[root@localhost ives]# docker save -o centos_docker_20210603.tar centos:latest
[root@localhost ives]# ll -h
总用量 209M
-rw-------. 1 root root 207M 6月 3 18:05 centos_docker_20210603.tar
drwxr-xr-x. 2 ives ives 6 5月 30 19:07 公共
........
本地用户目录 下显示的 _
2.6 load导入镜像
# 格式:docker load -i <image_file>
[root@localhost ives]# docker load -i centos_docker_20210603.tar
Loaded image: centos:latest
[root@localhost ives]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 5 months ago 209MB
2.7 tag标签
# 格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
[root@localhost ives]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 5 months ago 209MB
[root@localhost ives]# docker tag centos:latest centos:20210603
[root@localhost ives]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 20210603 300e315adb2f 5 months ago 209MB
centos latest 300e315adb2f 5 months ago 209MB
使用:根据需要给docker镜像打一个新标签。
2.8 info显示整个系统的信息
root@localhost ives]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc.)
Server:
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 3
Server Version: 20.10.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.3MiB
Name: localhost.ives
ID: II4B:QRTY:ZDJ2:CRG7:B7HD:KXXN:7CDW:6VN3:BT44:UW2X:RHQJ:65TQ
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
system镜像体积查看
另外一个需要注意的问题是,docker image ls 列表中的镜像体积总和并非是所有镜像实际硬盘消耗。由于 Docker 镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。由于 Docker 使用 Union FS,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多。
可以通过以下命令来便捷的查看镜像、容器、数据卷所占用的空间。
[root@localhost ives]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 2 7.058GB 209.3MB (2%)
Containers 2 1 13.03MB 0B (0%)
Local Volumes 1 1 1.697GB 0B (0%)
Build Cache 0 0 0B 0B
2.9 inspect显示镜像或容器的详情
1 # 格式:docker inspect <镜像ID>|<镜像名>|<容器ID>|<容器名>
2 [root@localhost ives]# docker inspect centos:latest # 显示镜像详情
[{......}]
3 [root@localhost ives]# docker inspect 99c28d07c6f0 # 显示容器详情
3 Docker容器操作
3.1 run创建容器
# 格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
[root@localhost ]# **docker run -i -t --name centos01 centos:latest /bin/bash**
// 例 docker run -d -p1521:1521 --name oracle11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g ##之前创建的oracle镜像
[root@99c28d07c6f0 /]# # 此时已进入docker容器
[root@99c28d07c6f0 /]# exit # 退出容器,此时容器会停止【正常情况】
[root@docker01 ~]#
注:oracle11g的容器用exit就退不出来,只在root和oracle用户间切换 @_@
参数说明:
-i 交互式操作
-t 分配一个终端
--name 运行的容器名称
最后的/bin/bash 要执行的命令
-d, --detach=false, 指定容器运行于前台还是后台,默认为false
-i, --interactive=false, 打开STDIN,用于控制台交互
-t, --tty=false, 分配tty设备,该可以支持终端登录,默认为false
-u, --user=“”, 指定容器的用户
-a, --attach=[], 登录容器(必须是以docker run -d启动的容器)
-w, --workdir=“”, 指定容器的工作目录
-c, --cpu-shares=0, 设置容器CPU权重,在CPU共享场景使用
-e, --env=[], 指定环境变量,容器中可以使用该环境变量
-m, --memory=“”, 指定容器的内存上限
-P, --publish-all=false, 指定容器暴露的端口
-p, --publish=[], 指定容器暴露的端口
-h, --hostname=“”, 指定容器的主机名
-v, --volume=[], 给容器挂载存储卷,挂载到容器的某个目录
–volumes-from=[], 给容器挂载其他容器上的卷,挂载到容器的某个目录
–cap-add=[], 添加权限,权限清单详见:http://linux.die.net/man/7/capabilities
–cap-drop=[], 删除权限,权限清单详见:http://linux.die.net/man/7/capabilities
–cidfile=“”, 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法
–cpuset=“”, 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU
–device=[], 添加主机设备给容器,相当于设备直通
–dns=[], 指定容器的dns服务器
–dns-search=[], 指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件
–entrypoint=“”, 覆盖image的入口点
–env-file=[], 指定环境变量文件,文件格式为每行一个环境变量
–expose=[], 指定容器暴露的端口,即修改镜像的暴露端口
–link=[], 指定容器间的关联,使用其他容器的IP、env等信息
–lxc-conf=[], 指定容器的配置文件,只有在指定–exec-driver=lxc时使用
–name=“”, 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字
–net=“bridge”, 容器网络设置:
bridge 使用docker daemon指定的网桥
host //容器使用主机的网络
container:NAME_or_ID >//使用其他容器的网路,共享IP和PORT等网络资源
none 容器使用自己的网络(类似–net=bridge),但是不进行配置
–privileged=false, 指定容器是否为特权容器,特权容器拥有所有的capabilities
–restart=“no”, 指定容器停止后的重启策略:
no:容器退出时不重启
on-failure:容器故障退出(返回值非零)时重启
always:容器退出时总是重启
–rm=false, 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)
–sig-proxy=true, 设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理
–restart=always 开机自动重启
示例
运行一个在后台执行的容器,同时,还能用控制台管理:
docker run -i -t -d ubuntu:latest
运行一个带命令在后台不断执行的容器,不直接展示容器内部信息:
docker run -d ubuntu:latest ping www.docker.com
运行一个在后台不断执行的容器,同时带有命令,程序被终止后还能重启继续跑,还能用控制台管理,
docker run -d --restart=always ubuntu:latest ping www.docker.com
为容器指定一个名字,
docker run -d --name=ubuntu_server ubuntu:latest
容器暴露80端口,并指定宿主机80端口与其通信(:之前是宿主机端口,之后是容器需暴露的端口),
docker run -d --name=ubuntu_server -p 80:80 ubuntu:latest
指定容器内目录与宿主机目录共享(:之前是宿主机文件夹,之后是容器需共享的文件夹),
docker run -d --name=ubuntu_server -v /etc/www:/var/www ubuntu:latest
启动mysql,指定端口,挂载日志、配置文件、设置Root用户密码
docker run -d -p 3306:3306 --name mysql \
-v /opt/mysql/log:/var/log/mysql \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
3.2 ps查看容器
[root@docker01 ~]# **docker ps** # 查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e54e87065da0 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" 2 days ago Up 25 hours 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp oracle11g
[root@docker01 ~]#
[root@docker01 ~]# **docker ps -a** # 查看所有容器,包括运行和停止的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago centos01
e54e87065da0 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" 2 days ago Up 25 hours 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp oracle11g
6902db634333 hello-world "/hello" 3 days ago Exited (0) 3 days ago cool_johnson
3.3 start启动容器
# 格式:docker start <容器名>|<容器ID>
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 10 minutes ago Exited (0) 3 seconds ago centos01
[root@docker01 ~]# docker start 99c28d07c6f0 # 启动容器
99c28d07c6f0
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 11 minutes ago Up 10 seconds centos01
e54e87065da0 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" 2 days ago Up 25 hours 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp oracle11g
3.4 restart重启容器
# 格式:docker restart <容器名>|<容器ID>
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 15 minutes ago Up 4 minutes centos01
[root@docker01 ~]# docker restart 99c28d07c6f0 # 重启容器
99c28d07c6f0
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 15 minutes ago Up 1 second centos01
修改更新容器开机启动
新建容器时:
在 docker run 时添加 --restart=always 参数
–restart 参数详解
Var Description
no 不自动重启容器(默认值)
on-failure 容器发生error而退出(容器退出状态不为0)时重启容器
unless-stopped 除在Docker守护进程启动(service docker start)时就已经停止的容器之外都启动
always 总是重启容器
已启动的容器:
docker update --restart=always 容器id或容器名
# docker start mysql
# docker update --restart=always mysql
# reboot
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b7a7b4a8514 docker.io/mysql:5.7 "docker-entrypoint..." 39 hours ago Up 46 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
3.5 stop停止容器
# 格式:docker stop <容器名>|<容器ID>
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 3 hours ago Up 4 minutes centos01
[root@docker01 ~]# docker stop 99c28d07c6f0 # 停止容器
99c28d07c6f0
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c28d07c6f0 centos:latest "/bin/bash" 3 hours ago Exited (0) 10 seconds ago centos01
3.6 rename容器重命名
1 # 格式:docker rename CONTAINER NEW_NAME
2 [root@docker01 ~]# docker ps
3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4 99c28d07c6f0 centos:latest "/bin/bash" 20 minutes ago Up About a minute centos01
5 [root@docker01 ~]# docker rename 99c28d07c6f0 centos001 # 容器重命名
6 [root@docker01 ~]# docker ps
7 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8 99c28d07c6f0 centos:latest "/bin/bash" 20 minutes ago Up About a minute centos001
3.7 rm删除容器
1 # 格式:docker rm <容器名>|<容器ID>
2 [root@docker01 ~]# docker ps -a
3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4 99c28d07c6f0 centos:latest "/bin/bash" 3 hours ago Exited (0) 10 seconds ago centos01
5 [root@docker01 ~]# docker rm 99c28d07c6f0 # 删除已停止的容器
6 99c28d07c6f0
说明:如果要强制删除正在运行的容器,使用 docker rm -f <容器ID>。不过生产环境不建议强制删除容器,防止误删除。
3.8 exec进入容器或在运行容器中执行命令
3.8.1 进入容器
[root@localhost ives]# docker exec -it 99c28d07c6f0 bash ##也可用容器名进入
docker exec -it centos001 bash
[root@99c28d07c6f0 /]#
说明:不建议通过 docker attach 方式进入容器。
在容器外让指定容器执行命令
1 # 不要有 -t 选项,因为不需要分配 tty 终端
2 [root@localhost ives]# docker exec -i centos001 bash -c "ps -ef"
3 UID PID PPID C STIME TTY TIME CMD
4 root 1 0 0 10:57 pts/0 00:00:00 /bin/bash
5 root 43 0 0 11:02 ? 00:00:00 ps -ef
3.8.2 cp复制文件或目 录
将宿主机文件或目录,拷贝到docker容器中
[root@localhost ives]# docker exec -i centos001 bash -c "ls -1 /root"
anaconda-ks.cfg
anaconda-post.log
original-ks.cfg
[root@localhost ives]# docker cp /usr/bin/telnet centos001:/root/
lstat /usr/bin/telnet: no such file or directory ##找不到文件或目录
[root@localhost ives]# docker cp /usr/bin/zip centos001:/root/ # 拷贝文件
[root@localhost ives]# docker cp /root/test centos001:/root # 拷贝目录
[root@localhost ives]# docker exec -i centos001 bash -c "ls -1 /root"
anaconda-ks.cfg
anaconda-post.log
original-ks.cfg
test
zip
3.8.3 将docker容器中的文件或目录,拷贝到宿主机中
1 [root@localhost ]# docker cp centos001:/root/original-ks.cfg /root/ # 拷贝文件
2 [root@localhost]# docker cp centos001:/etc /root/test/ # 拷贝目录
3.8.4 logs查看容器日志
[root@localhost ives]# docker logs -f --tail 500 99c28d07c6f0
[root@99c28d07c6f0 /]# exit
exit
[root@99c28d07c6f0 /]# exit
[root@99c28d07c6f0 /]# exit
参数说明:
1 -f 持续打印输出
2 --tail 500 打印日志最后的500行
3.8.5 stats容器使用资源统计
可用于监控
1 [root@docker01 ~]# docker stats <容器ID>|<容器名称> # # 持续监控
# docker stats centos001
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
99c28d07c6f0 centos001 0.00% 4.508MiB / 972.3MiB 0.46% 656B / 0B 31.8MB / 0B 1
.......
2 [root@docker01 ~]# docker stats --no-stream <容器ID>|<容器名称> # 不是持续监控,只显示第一次返回的结果
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
99c28d07c6f0 centos001 0.00% 4.508MiB / 972.3MiB 0.46% 656B / 0B 31.8MB / 0B 1
3.8.6 top容器中运行的进程
[root@localhost ives]# docker top centos001
UID PID PPID C STIME TTY TIME CMD
root 30094 30073 0 18:57 ? 00:00:00 /bin/bash
3.8.6 port容器映射特定端口
容器映射端口有:随机端口映射、指定单个端口映射、指定多个端口映射
获取镜像
1 [root@docker01 ~]# docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
2 [root@docker01 ~]# docker tag ed21b7a8aee9 nginx:1.17
3 [root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
4nginx 1.17 ed21b7a8aee9 14 months ago 127MB
registry.cn-beijing.aliyuncs.com/google_registry/nginx 1.17 ed21b7a8aee9 14 months ago 127MB
3.8.6.1 映射随机端口
[root@localhost ives]# docker run -d -P --name nginx01 nginx:1.17 ##创建容器
c3de16e2c158b69a2c01d0c7118107d15bd7c78eca7958cc6d32af40f037c6e8
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3de16e2c158 nginx:1.17 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx01
说明:
0.0.0.0:49153->80/tcp 前面为宿主机端口,后面为容器端口
容器日志
[root@localhost ives]# docker logs -f --tail 500 nginx01
192.168.118.20 - - [03/Jun/2021:11:57:31 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
2021/06/03 11:57:31 [error] 7#7: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.118.20, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.118.20:49153"
192.168.118.20 - - [03/Jun/2021:11:57:31 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
浏览器访问:
3.8.6.2 映射单个指定端口
[root@localhost ives]# docker run -d -p 81:80 --name nginx02 nginx:1.17
3d375d42e46dd9f25973c97c911a41cd564127d1173b9a43e627dbf24658c3a0
[root@localhost ives]# docker ps | grep 'nginx02'
3d375d42e46d nginx:1.17 "nginx -g 'daemon of…" 52 seconds ago Up 50 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp nginx02
浏览器访问:
补充知识:Docker如何重启后数据不丢失,教你挂载数据卷Volume
大家在使用Docker部署web应用或者mysql数据库时,会发现当容器重启后,容器运行过程中产生的日志或者数据库数据都会被清空,那么我们如何保存这些数据呢?
这就需要了解docker如何挂载宿主机磁盘目录,用来永久存储数据。
- 创建容器时执行Docker Volume
使用 docker run 命令,可以运行一个 Docker容器,使用镜像ubuntu/nginx,挂载本地目录/tmp/source到容器目录/tmp/destination
docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash
基于ubuntu/nginx镜像创建了一个Docker容器。
指定容器的名称为test,由 ––name 选项指定。
Docker Volume 由 ––volume (可以简写为-v)选项指定,主机的 /tmp/source 目录与容器中的 /tmp/destination 目录一一对应。 - 查看Docker Volume
使用 docker inspect 命令,可以查看 Docker容器 的详细信息:
docker inspect --format='{{json .Mounts}}'test | python -m json.tool[{“Destination”: “/tmp/destination”,“Mode”: “”,“Propagation”: “”,“RW”: true,“Source”: “/tmp/source”,“Type”: “bind”}]
使用 ––format 选项,可以选择性查看需要的容器信息。 .Mount 为容器的 Docker Volume 信息。
python -m json.tool 可以将输出的json字符串格式化显示。
Source 表示主机上的目录,即 /tmp/source 。
Destination 为容器中的目录,即 /tmp/destination。 - 本机文件可以同步到容器
在本机/tmp/source目录中新建hello.txt文件
touch /tmp/source/hello.txtls /tmp/source/hello.txt
hello.txt文件在容器/tmp/destination/目录中可见
使用 docker exec 命令,可以在容器中执行命令。
docker exec -i test bash-c “ls /tmp/destination/hello.txt”
所以在宿主机对目录 /tmp/source/ 的修改,可以同步到容器目录 /tmp/destination/ 中。 - 容器文件可以同步到宿主机
在容器/tmp/destination目录中新建world.txt文件
docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt
world.txt文件在宿主机/tmp/source/目录中可见
ls /tmp/source/hello.txt world.txt
以上就是使用docker怎么修改容器的挂载目录,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
参考:
https://www.cnblogs.com/zhanglianghhh/p/13138422.html
3.9 使用docker怎么修改容器的挂载目录
先参考 https://www.yisu.com/zixun/322030.html
挂截了一个给oracle用,文件查看读取都可以,就是引起oracle数据库没法正常启动,后面搞明白了再补充这块
4 私有镜像仓库 Harbor安装---------
构建Docker仓库方式除了使用Registry之外,还可以使用Harbor,如下为Registry方式缺点:
缺少认证机制,任何人都可以随意拉取及上传镜像,安全性缺失
缺乏镜像清理机制,镜像可以push却不能删除,日积月累,占用空间会越来越大(如果要删除找到宿主机目录下面对应文件进行删除)
缺乏相应的扩展机制
鉴于以上缺点,我们通常在生产环境中,不会直接使用docker registry来实现提供镜像服务。
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。
相对Registry,Harbor具有很多优势:
提供分层传输机制,优化网络传输 Docker镜像是是分层的,而如果每次传输都使用全量文件(所以用FTP的方式并不适合),显然不经济。必须提供识别分层传输的机制,以层的UUID为标识,确定传输的对象。
提供WEB界面,优化用户体验 只用镜像的名字来进行上传下载显然很不方便,需要有一个用户界面可以支持登陆、搜索功能,包括区分公有、私有镜像。
支持水平扩展集群 当有用户对镜像的上传下载操作集中在某服务器,需要对相应的访问压力作分解。
良好的安全机制 企业中的开发团队有很多不同的职位,对于不同的职位人员,分配不同的权限,具有更好的安全性。
1 Docker已安装好的情况下, 安装Docker-Compose快速编排工具
下载地址: https://github.com/docker/compose/releases/
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-uname -s
-uname -m
> /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose ## 添加执行权限
docker-compose -version ##查看版本
2 下载Harbor的压缩包
下载地址: https://github.com/goharbor/harbor/releases
wget https://github.com/goharbor/harbor/releases/download/v2.3.0-rc3/harbor-offline-installer-v2.3.0-rc3.tgz
tar -xzf harbor-offline-installer-v2.3.0-rc3.tgz
mkdir /data/harbor
mv harbor/* /data/harbor
cd /data/harbor
3 修改Harbor的配置
cp harbor.yml.tmpl harbor.yml
vi harbor.yml 修改hostname和port
hostname: 192.168.118.20
port: 89
-------- 或修改Habor配置文件harbor.cfg------
vim harbor.cfg
hostname = 192.168.179.100
4 安装Habor,命令如下:./install.sh
./prepare ##准备
prepare base dir is set to /data/harbor
Unable to find image ‘goharbor/prepare:v2.3.0’ locally
v2.3.0: Pulling from goharbor/prepare
7ec05e6a2d8c: Pull complete
22eb7ca0aa9a: Pull complete
f162dd20b3be: Pull complete
2a7e424213db: Pull complete
6cb32ec69555: Pull complete
c972045b5ab9: Pull complete
4cd2066d93bf: Pull complete
221d1a417eeb: Pull complete
Digest: sha256:173e3eb89910e7e7d3151a7d7ca7882a5274457dcfde8466b62cc9635e894ca7
Status: Downloaded newer image for goharbor/prepare:v2.3.0
Error happened in config validation…
ERROR:root:Error: The protocol is https but attribute ssl_cert is not set
--------------报错解决方式,把htts的都#iyc
https related config
#https:
https port for harbor, default is 443
port: 443
The path of cert and key files for nginx
certificate: /your/certificate/path
private_key: /your/private/key/path
./install.sh
#如果需要支持存储helm的chart包,添加如下参数
./install.sh --with-chartmuseum
参考:
https://blog.csdn.net/qq_34556414/article/details/108014074
https://blog.csdn.net/u010948569/article/details/107222583/
在k8s中使用harbor仓库
cat > /etc/docker/daemon.conf <<EOF
{
“registry-mirrors”: [
“https://kfwkfulq.mirror.aliyuncs.com”,
“https://registry.docker-cn.com”,
“http://hub-mirror.c.163.com”
],
{ “insecure-registries”:[“192.168.252.213:80”] }, # harbor ip地址
“dns”: [“8.8.8.8”,“8.8.4.4”]
}
EOF
最后重启docker
service docker restart