• 作者:老汪软件技巧
  • 发表时间:2024-10-06 04:00
  • 浏览量:

docker compose是什么?

Docker Compose 和docker功能一样,为了运行容器服务,但是docker compose比docker更好的一点是:允许你在一个 YAML 文件中定义多个容器及其配置,并通过一条命令启动和管理这些容器。

为什么要使用docker compose?

通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务,然后一个命令来创建并启动所有服务。非常方便使用,类似devops K8S里的yaml文件用法.

安装独立的 docker compose

执行命令,下载docker-compose的可执行环境:

curl -SL https://github.com/docker/compose/releases/download/v2.29.6/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

配置环境目录调整

mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

配置权限

chmod +x docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

使用docker-compose测试安装是否成功测试方案一:

正常情况下执行下面命令即可知道当前安装版本,如果你安装的是其他版本,可以去掉中间-试一下~

docker-compose --version

测试方案二:

使用Compose standalone时键入docker-compose up,而不是docker compose up,如果你已经安装了其他版本的docker compose, 你可以执行 docker compose进行测试

docker-compose

测试示例

USER_01@xiaojinServer:~$ docker compose
Usage:  docker compose [OPTIONS] COMMAND
Define and run multi-container applications with Docker
Options:
      --all-resources              Include all resources, even those not used by services
      --ansi string                Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
      --compatibility              Run compose in backward compatibility mode
      --dry-run                    Execute command in dry run mode
      --env-file stringArray       Specify an alternate environment file
  -f, --file stringArray           Compose configuration files
      --parallel int               Control max parallelism, -1 for unlimited (default -1)
      --profile stringArray        Specify a profile to enable
      --progress string            Set type of progress output (auto, tty, plain, json, quiet) (default "auto")
      --project-directory string   Specify an alternate working directory
                                   (default: the path of the, first specified, Compose file)
  -p, --project-name string        Project name
Commands:
  attach      Attach local standard input, output, and error streams to a service's running container
  build       Build or rebuild services
  config      Parse, resolve and render compose file in canonical format
  cp          Copy files/folders between a service container and the local filesystem
  create      Creates containers for a service
  down        Stop and remove containers, networks
  events      Receive real time events from containers
  exec        Execute a command in a running container
  images      List images used by the created containers
  kill        Force stop service containers
  logs        View output from containers
  ls          List running compose projects
  pause       Pause services
  port        Print the public port for a port binding
  ps          List containers
  pull        Pull service images
  push        Push service images
  restart     Restart service containers
  rm          Removes stopped service containers
  run         Run a one-off command on a service
  scale       Scale services
  start       Start services
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop services
  top         Display the running processes
  unpause     Unpause services
  up          Create and start containers
  version     Show the Docker Compose version information
  wait        Block until the first service container stops
  watch       Watch build context for service and rebuild/refresh containers when files are updated
Run 'docker compose COMMAND --help' for more information on a command.

安装失败解决方案

如果docker-compose安装失败,检查下你的路径权限。可以尝试执行下面命令:

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

命令解析逐条具体分析:作用:使用docker-compose安装sqlserver搜索镜像

docker search mssql-server

拉取镜像

找到适合你的版本,拉取镜像,下面这个是我从官方文档里直接找到的镜像哇~

docker pull mcr.microsoft.com/mssql/server:2022-latest

运行容器,创建数据库创建服务文件

创建一个docker-compose.yml文件并定义SQL Server服务


version: '3.3'
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: mssql-server
    restart: always
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=tangdoudou.123
      - MSSQL_SA_PASSWORD=tangdoudou.123
    ports:
      - 5433:1433
    volumes:
      - ./sqlserver:/var/opt/mssql

安装执行

sudo docker compose up

安装成功

执行结果:

USER_01@xiaojinServer:/docker-compose$ sudo docker compose up -d
WARN[0000] /docker-compose/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1Container mssql-server  Started                                                                                                                                                                        0.4s
USER_01@xiaojinServer:/docker-compose$

_安装使用漏电保护器是_安装使用漏电保护器是属于

查看容器列表

docker ps -a

状态解释查看容器内的 SQL Server 错误日志:

docker exec -t sqlserver01 cat /var/opt/mssql/log/errorlog | grep connection

连接数据库方案一:使用可视化工具连接

下载DBever,连接数据库

方案一实际案例:

连接案例:

方案二:在容器内部使用 SQL Server 命令行工具 sqlcmd 实用程序 来连接 SQL Server

一般情况下,我们会使用可视化工具去连接数据库,如果你需要命令行方式连接的话,可以参考这个文档:

方案二实际案例:进入容器:

docker exec -it sqlserver01 "bash"

登录数据库,如果遇到certificate verify failed:self-signed certificate报错请参考下面的报错解决01。


/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "tangdoudou.123"

查询已经存在的数据库、查询数据库版本

SELECT name AS DatabaseName FROM sys.databases;
go

查询示例:

mssql@9f9XXXXXX:/$ /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "xiaojin的密码"
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : SSL Provider: [error:0A000086:SSL routines::certificate verify failed:self-signed certificate].
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722.
mssql@9f9XXXXXX:/$ /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "xiaojin的密码" -No
1> SELECT name AS DatabaseName FROM sys.databases;
2> go
DatabaseName
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
(4 rows affected)
1> SELECT name AS DatabaseName FROM sys.databases;
2> fo
3> go
Msg 102, Level 15, State 1, Server d046613904c3, Line 2
Incorrect syntax near 'fo'.
1> select @@version
2> go
                                                                                                                                                                                                               
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2022 (RTM-CU15) (KB5041321) - 16.0.4145.4 (X64)
        Sep 19 2024 08:25:04
        Copyright (C) 2022 Microsoft Corporation
        Developer Edition (64-bit) on Linux (Ubuntu 22.04.5 LTS) <X64>
(1 rows affected)
1>

报错解决01:

快速解决报错: SQL Server : SSL Provider: certificate verify failed:self-signed certificate

Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722.

解决方案:使用-No:

-N[s|m|o]的解释说明:Set the connection encryption mode to be Strict, Mandatory, or Optional respectively. Defaults to mandatory if not specified. ([s|m|o] added in sqlcmd 18.0)

/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "tangdoudou.123" -No

实际案例:

mssql@9f9XXXXXX:/$ /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "xiaojin的密码"
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : SSL Provider: [error:0A000086:SSL routines::certificate verify failed:self-signed certificate].
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722.
mssql@9f9XXXXXX:/$ /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "xiaojin的密码" -No
1> SELECT name AS DatabaseName FROM sys.databases;
2> go
DatabaseName
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
(4 rows affected)
1> SELECT name AS DatabaseName FROM sys.databases;
2> fo
3> go
Msg 102, Level 15, State 1, Server d046613904c3, Line 2
Incorrect syntax near 'fo'.
1> select @@version
2> go
                                                                                                                                                                                                               
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2022 (RTM-CU15) (KB5041321) - 16.0.4145.4 (X64)
        Sep 19 2024 08:25:04
        Copyright (C) 2022 Microsoft Corporation
        Developer Edition (64-bit) on Linux (Ubuntu 22.04.5 LTS) <X64>
(1 rows affected)
1>

结语

欢迎大家指出文章需要改正之处~

学无止境,合作共赢

欢迎路过的小哥哥小姐姐们提出更好的意见哇~~