文章目录
容器技术 Docker 应用
version: "3.8"
services:
web:
image:
# 自定义(个性化)的配置
# 把容器中的数据保存到本地,数据卷 volume
- type: volume
source: 数据卷名称
target: 容器的目录或者文件/
- "数据卷名称:容器的目录或者文件/"
# 当容器内的服务提供给其他主机使用时
# 应该映射容器内的监听端口到宿主机
ports:
- "8000:80" # 宿主机端口:容器端口
- "8443:443"
networks:
- net-name
volumes:
数据卷名称:
networks:
net-name:
____________________
如果存在多个容器间的互相访问、通信
应该相应的配置文件中,使用服务名、容器名称、容器的 IP
1. volume 已存在且其中有数据
2. 数据卷挂载的两种方式的区别
bind
绑定挂载
使用场景:
当你想让容器使用你自定义的配置文件时候,使用此种方式
- nginx.conf
- conf.d/default.conf
- my.cnf
- logstsh.yml
- pipeline.conf
read_only: true # 只读方式挂载,最好是把配置文件放到目录中,
# 之后,挂载目录
官方不建议 Docker 容器内的进程访问被挂载的文件
Docker 容器内的进程不要直接访问被挂载的文件
相当于我平常所说的 挂载
volume
数据卷
使用场景:
当你希望持久化存储容器内产生的数据的时候,
应该使用此种方式
- /var/lib/mysql 数据库
- /usr/share/elasticsearch/data 数据
宿主机的进程不应该直接访问到 数据卷的数据
一、Docker Hub
1.推送镜像
修改 镜像的 tag
[root@Docker ~]# docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 8 300e315adb2f 7 weeks ago 209MB
centos latest 300e315adb2f 7 weeks ago 209MB
centos 7 8652b9f0cb4c 2 months ago 204MB
[root@Docker ~]# docker tag centos:latest nekoosuu/centos:1.0
[root@Docker ~]# docker images |grep centos
nekoosuu/centos 1.0 300e315adb2f 7 weeks ago 209MB
centos 8 300e315adb2f 7 weeks ago 209MB
centos latest 300e315adb2f 7 weeks ago 209MB
centos 7 8652b9f0cb4c 2 months ago 204MB
2.从本地登录 Docker hub
[root@Docker ~]# 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: nekoosuu
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 # 此处显示为已登录
3.推送镜像到 Docker hub
[root@Docker ~]# docker push nekoosuu/centos:1.0
The push refers to repository [docker.io/nekoosuu/centos]
2653d992f4ef: Mounted from library/centos
1.0: digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 size: 529
在网页页面就可以查看到自己上传的
4.从本地退出 Docker hub
docker logout
二、Harbor仓库
在做此操作前必须保证有
docker-compose
1.安装compose
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
2.下载 docker Harbor
离线安装包
wget https://github.com/goharbor/harbor/releases/download/v2.1.3/harbor-offline-installer-v2.1.3.tgz
3.解压
tar -xf harbor-offline-installer-v2.1.3.tgz
4.配置 harbor 服务的主机名
[root@Docker ~]# cd harbor/
[root@Docker harbor]# mv harbor.yml.tmpl harbor.yml
修改 harbor.yml 文件中的 hostname
的值为 自己宿主机的主机名或者 IP
hosname = 192.168.31.248
5,关闭 https
还是在 harbor.yml 文件中,注释掉如下几行
# 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
打开如下注释行
[root@Docker harbor]# vim harbor.yml
# 打开如下配置文件的注释
external_url: 192.168.31.248:8017
6.执行 install.sh 脚本
./install.sh
假如出现端口冲突的错误,修改 harbor.yml 文件中的如下内容
http: port: 宿主机的端口, 此端口将会映射到容器内的 8080 端口
显示如下就表示成功了
[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db ... done
Creating registryctl ... done
Creating harbor-portal ... done
Creating registry ... done
Creating redis ... done
Creating harbor-core ... done
Creating nginx ... done
Creating harbor-jobservice ... done
✔ ----Harbor has been installed and started successfully.----
浏览器访问测试:192.168.31.248
或者 192.168.31.248:宿主机端口
默认用户为
admin
默认密码为
7.创建仓库
8.创建账户
9.测试Harbor
[root@Docker harbor]# vim /etc/docker/daemon.json
[root@Docker harbor]# cat /etc/docker/daemon.json
{
"insecure-registries": ["192.168.31.248:8017"]
"registry-mirrors": ["https://mr9nt7jk.mirror.aliyuncs.com"]
}
[root@Docker harbor]# docker login 192.168.31.248:8017
Username: neko
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
上传测试
[root@Docker harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest be1f31be9a87 13 days ago 109MB
[root@Docker harbor]# docker image tag nginx 172.21.39.43:80/jenkins/nginx:v0
[root@Docker harbor]# docker push 172.21.39.43:80/jenkins/nginx:v0
The push refers to repository [harbor.io/library/nginx]
92b86b4e7957: Pushed
94ad191a291b: Pushed
8b15606a9e3e: Pushed
latest: digest: sha256:204a9a8e65061b10b92ad361dd6f406248404fe60efd5d6a8f2595f18bb37aad size: 948
# 在web界面中查看镜像是否被上传到仓库