? 在实际生产环境中,一个应用往往由许多服务构成,而 docker 的最佳实践是一个容器只运行一个进程,因此运行多个微服务就要运行多个容器。多个容器协同工作需要一个有效的工具来管理他们,定义这些容器如何相互关联。compose 应运而生。compose 是用来定义和运行一个或多个容器(通常都是多个)运行和应用的工具。使用 compose 可以简化容器镜像的构建以及容器的运行。compose 使用 YAML 文件来定义多容器之间的关系。一个 docker-compose up 就可以把完整的应用跑起来。 本质上, compose 把 YAML 文件解析成 docker 命令的参数,然后调用相应的 docker 命令行接口,从而将应用以容器化的方式管理起来。它通过解析容器间的依赖关系顺序地启动容器。而容器间的依赖关系由 YAML 文件中的 links 标记指定。
? 官网地址:https://docs.docker.com/compose/compose-file/
? github地址:https://github.com/docker/compose
? 在github上面找到相应的版本下载,下图是本次安装所使用的版本:
? 将下载好的文件上传到相应目录,执行下面操作:
[root@localhost software]# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
[root@localhost software]# chmod +x /usr/local/bin/docker-compose
[root@localhost software]# docker-compose -v
docker-compose version 1.27.4, build 40524192
[root@localhost software]# docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
? 看到版本信息代表安装成功。
? docker-compose卸载只需要删除二进制文件就可以了。
rm -rf /usr/local/bin/docker-compose
reboot
? 官网地址:https://docs.docker.com/compose/reference/build/
//启动服务
docker-compose up -d
//停止服务
docker-compose down
//列出所有运行容器
docker-compose ps
//查看服务日志
docker-compose logs
//构建或者重新构建服务
docker-compose build
//启动服务
docker-compose start
//停止已运行的服务
docker-compose stop
//重启服务
docker-compose restart
? harbor官网地址: https://goharbor.io/
? github官网地址: https://github.com/goharbor/harbor
? 开发环境大部分采用http方式进行安装;生产环境建议采用https方式安装。harbor下载地址https://github.com/goharbor/harbor/releases。
? 下载完成后将包上传到192.168.43.108这台服务器的/root/app目录。
[root@localhost app]# tar -zxvf harbor-offline-installer-v2.5.1.tgz
harbor/harbor.v2.5.1.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@localhost app]# ll
总用量 645596
drwxr-xr-x. 2 root root 122 6月 23 21:56 harbor
-rw-r--r--. 1 root root 661087658 6月 23 11:27 harbor-offline-installer-v2.5.1.tgz
[root@localhost app]# cd harbor/
? 解压成功之后进入到harbor目录:
[root@localhost harbor]# cp harbor.yml.tmpl harbor.yml
[root@localhost harbor]# vim harbor.yml
? 修改点:
hostname: 192.168.43.108
# http related config
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 5000
#注释掉https的相关信息
#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
#修改数据卷位置,其他不变
data_volume: /data/harbor
? 执行下面命令启动harbor:
[root@localhost harbor]# ./install.sh
? 启动成功之后登陆页面http://192.168.43.108:5000/,用户名:admin,密码:Harbor12345 。
? 在控制台登陆搭建的harbor仓库,发现报了https的错误:
[root@localhost harbor]# docker login -u admin -p Harbor12345 192.168.43.108:5000
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://192.168.43.108:5000/v2/: http: server gave HTTP response to HTTPS client
? 编辑下面文件,添加如下内容:
[root@localhost harbor]# vim /etc/docker/daemon.json
? 添加的内容:
"insecure-registries":["192.168.198.101:5000"]
? 重启docker服务:
[root@localhost harbor]# systemctl daemon-reload
[root@localhost harbor]# systemctl restart docker
? 再次登陆正常::
[root@localhost harbor]# docker login -u admin -p Harbor12345 192.168.43.108:5000
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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@localhost harbor]# docker logout 192.168.43.108:5000
Removing login credentials for 192.168.43.108:5000
? 将自定义的镜像上传到harbor服务器,在harbor的管控台上面新建一个dream21th的项目:
[root@localhost harbor]# docker tag nginx:1.19.3-alpine 192.168.43.108:5000/dream21th/nginx:v1
[root@localhost harbor]# docker push 192.168.43.108:5000/dream21th/nginx:v1
The push refers to repository [192.168.43.108:5000/dream21th/nginx]
8d6d1951ab0a: Pushed
d0e26daf1f58: Pushed
835f5b67679c: Pushed
4daeb7840e4d: Pushed
ace0eda3e3be: Pushed
v1: digest: sha256:a411d06ab4f5347ac9652357ac35600555aeff0b910326cc7adc36d471e0b36f size: 1360
? 使用docker操作指令commit只做镜像上传到仓库。
[root@localhost harbor]# docker run -itd -p 80:80 192.168.43.108:5000/dream21th/nginx:v1
72a74245bfc2faf4ca76747678d06bba9b9390bfd1840c24d249e9e5a8e03a21
[root@localhost harbor]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72a74245bfc2 192.168.43.108:5000/dream21th/nginx:v1 "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp busy_bassi
[root@localhost data]# cd /data
[root@localhost data]# echo "this is dream21th nginx welcome page" > index.html
[root@localhost data]# docker cp index.html busy_bassi:/usr/share/nginx/html/index.html
[root@localhost data]# curl http://192.168.43.108/
this is dream21th nginx welcome page
[root@localhost data]# docker container commit -m "update index.html file" -a "dream21th" busy_bassi 192.168.43.108:5000/dream21th/nginx:v2
sha256:f5557a570faa7237b8c4ef5d48767f49bf009a3c0410626df80b52874d3fa2f5
[root@localhost data]# docker push 192.168.43.108:5000/dream21th/nginx:v2
The push refers to repository [192.168.43.108:5000/dream21th/nginx]
967a418347a5: Pushed
8d6d1951ab0a: Layer already exists
d0e26daf1f58: Layer already exists
835f5b67679c: Layer already exists
4daeb7840e4d: Layer already exists
ace0eda3e3be: Layer already exists
v2: digest: sha256:b57f5bbe2633b91a9466cc785be61a01b945fc4c4b2f4dfa723981e7848afd43 size: 1568
? 上传成功之后可以看到harbor的的项目目录有下面两个版本的nginx的信息: