Docker Compose学习笔记

编程入门 行业动态 更新时间:2024-10-27 20:27:54

Docker Compose<a href=https://www.elefans.com/category/jswz/34/1770117.html style=学习笔记"/>

Docker Compose学习笔记

Docker Compose用来做什么?

Docker Compose 是Docker官方的开源项目。

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

使用Compose,我们只需要配置好配置文件(YAML文件),一条命令就可以启动我们想要的多个窗口容器应用。

我的理解,简单说就是不用每次一个一个去输入参数和命令,一个一个启动容器,而只要将配置配好后,一条命令就可以实现我们想要启动/停止多个容器(带配置)的工作。

Docker Compose需要另外安装。

Docker compose使用三步骤

  1. 编写Dockerfile
  2. 编写compose.yaml
  3. 在compose.yaml所在的目录上输入命令docker compose up
    当然,过程中需要准备好compose.yaml中所用到的文件。

安装和查看版本

curl -SL .23.0/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
# -S选项表示在下载过程中保持连接,-L选项表示如果服务器返回重定向,则自动跟随重定向。
#由于网络原因,我是直接从电脑上下下来,改名再移到centos对应的目录中。#使文件可以执行
[root@localhost cli-plugins]# ./docker-compose
-bash: ./docker-compose: 权限不够
[root@localhost cli-plugins]# ll
总用量 58232
-rw-r--r--. 1 root root 59628532 11月  2 22:09 docker-compose
[root@localhost cli-plugins]# chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
[root@localhost cli-plugins]# ll
总用量 58232
-rwxr-xr-x. 1 root root 59628532 11月  2 22:09 docker-compose
[root@localhost cli-plugins]# #查看版本
[root@localhost cli-plugins]# ./docker-compose --version
Docker Compose version v2.23.0
[root@localhost cli-plugins]# docker-compose --versionln -s /usr/local/lib/docker/cli-plugins/docker-compose /usr/bin/docker-compose#加上软连接
[root@localhost docker]# docker-compose --version
bash: docker-compose: 未找到命令...
[root@localhost docker]# ln -s /usr/local/lib/docker/cli-plugins/docker-compose /usr/bin/docker-compose
[root@localhost docker]# cd /usr/bin
[root@localhost bin]# ls doc*
docker  docker-compose  dockerd  dockerd-rootless-setuptool.sh  dockerd-rootless.sh  docker-proxy
[root@localhost bin]# cd /
[root@localhost /]# docker-compose --version
Docker Compose version v2.23.0
[root@localhost /]# 

常用命令

#
# 运行当前目录下的copmpose.yaml
[root@localhost composetest]# docker compose up#以后台形式运行当前目录下的copmpose.yaml
[root@localhost composetest]# docker compose up -d#停止一个docker compose项目
[root@localhost composetest]# docker-compose down
[+] Running 3/3✔ Container composetest-redis-1  Removed                                                                                                                                                    0.4s ✔ Container composetest-web-1    Removed                                                                                                                                                   10.5s ✔ Network composetest_default    Removed                                                                                                                                                    0.5s 
[root@localhost composetest]#
# 这个操作的结果是:下载和创建的镜像还在,容器停止并被清除。#但是如果没有执行docker-compose down命令而直接重启主机或者重启docker服务后,镜像和容器会被保留下来,包括里面的正在运行的redis记录。
# 如果想每次重启自动运行,那在compose.yaml上的服务下面,加上restart: always,这样每次docker重启,就会自动运行容器而无需手动再启动。# 查看网络ID列表
[root@localhost ~]# docker network ls
# 检查具体某个网络
[root@localhost ~]# docker network inspect f8c18a80a81a

更多推荐

Docker Compose学习笔记

本文发布于:2023-11-16 16:33:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:学习笔记   Docker   Compose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!