<tfoot id='f2Big'></tfoot>

        <legend id='f2Big'><style id='f2Big'><dir id='f2Big'><q id='f2Big'></q></dir></style></legend>
        <i id='f2Big'><tr id='f2Big'><dt id='f2Big'><q id='f2Big'><span id='f2Big'><b id='f2Big'><form id='f2Big'><ins id='f2Big'></ins><ul id='f2Big'></ul><sub id='f2Big'></sub></form><legend id='f2Big'></legend><bdo id='f2Big'><pre id='f2Big'><center id='f2Big'></center></pre></bdo></b><th id='f2Big'></th></span></q></dt></tr></i><div id='f2Big'><tfoot id='f2Big'></tfoot><dl id='f2Big'><fieldset id='f2Big'></fieldset></dl></div>

        <small id='f2Big'></small><noframes id='f2Big'>

        • <bdo id='f2Big'></bdo><ul id='f2Big'></ul>

      1. docker 容器启动后运行命令

        时间:2024-04-16

        <legend id='PK0Zr'><style id='PK0Zr'><dir id='PK0Zr'><q id='PK0Zr'></q></dir></style></legend>
          <bdo id='PK0Zr'></bdo><ul id='PK0Zr'></ul>
          <i id='PK0Zr'><tr id='PK0Zr'><dt id='PK0Zr'><q id='PK0Zr'><span id='PK0Zr'><b id='PK0Zr'><form id='PK0Zr'><ins id='PK0Zr'></ins><ul id='PK0Zr'></ul><sub id='PK0Zr'></sub></form><legend id='PK0Zr'></legend><bdo id='PK0Zr'><pre id='PK0Zr'><center id='PK0Zr'></center></pre></bdo></b><th id='PK0Zr'></th></span></q></dt></tr></i><div id='PK0Zr'><tfoot id='PK0Zr'></tfoot><dl id='PK0Zr'><fieldset id='PK0Zr'></fieldset></dl></div>

          • <tfoot id='PK0Zr'></tfoot>
              <tbody id='PK0Zr'></tbody>

            <small id='PK0Zr'></small><noframes id='PK0Zr'>

                  本文介绍了docker 容器启动后运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我准备了一个 docker-compose 文件来部署带有数据库的容器:

                  I've prepared a docker-compose file to deploy container with database:

                  services:
                    tmp-db:
                      image: microsoft/mssql-server-linux:latest
                      environment:
                        ACCEPT_EULA: Y
                        SA_PASSWORD: yourStrong(!)Password
                      ports:
                      - 1433:1433
                  

                  没关系.但是现在我需要创建一个数据库并构建它的结构.我需要执行一些 sql 命令.为了检查我是否能够做到这一点,我在服务中添加了 command:

                  It is okay. But now I need to create a database and build its structure. I need to execute some sql commands. To check if i am able to do this I added command to the service:

                  services:
                    tmp-db:
                      image: microsoft/mssql-server-linux:latest
                      environment:
                        ACCEPT_EULA: Y
                        SA_PASSWORD: yourStrong(!)Password
                      command: /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
                      ports:
                      - 1433:1433
                  

                  但是我遇到了以下错误:

                  However I got the following errors:

                  tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
                  tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
                  tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
                  

                  感觉命令是在Sql Server实例启动之前执行的.我该如何解决?Sql Server 启动后如何执行一些sql?

                  I feel the command is executed before Sql Server instance is started. How can I fix it? How can I execute some sql after Sql Server is started?

                  推荐答案

                  问题是在一个容器中只执行一个命令.当您在 docker-compose.yml 中指定命令时,它会覆盖默认命令,该命令应该启动容器.所以你有两个选择

                  The issue is that only one command is executed in a container. When you specify the command in docker-compose.yml it overrides the default command, which was supposed to start the container. So you have two options

                  手动运行命令

                  services:
                    tmp-db:
                      image: microsoft/mssql-server-linux:latest
                      environment:
                        ACCEPT_EULA: Y
                        SA_PASSWORD: yourStrong(!)Password
                      ports:
                      - 1433:1433
                  

                  然后就可以执行

                  docker-compose exec tmp-db /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
                  

                  有两项服务 - 一项用于服务器 &一个用于数据加载

                  services:
                    load-db:
                      image: microsoft/mssql-server-linux:latest
                      command: sh -c 'sleep 10 && /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"'
                      network_mode: service:tmp-db
                    tmp-db:
                      image: microsoft/mssql-server-linux:latest
                      environment:
                        ACCEPT_EULA: Y
                        SA_PASSWORD: yourStrong(!)Password
                      ports:
                      - 1433:1433
                  

                  在这种方法中,我们使用加载数据的命令启动另一个容器,我们在数据库服务器容器的网络上运行它.这样做只是为了避免数据库的主机名,如果您愿意,也可以将主机名作为 tmp-db 传递并删除 network_mode: service:tmp-db>.

                  In this approach we launch another container with the command to load our data, we run it on the network of our DB server container. This is done just to avoid the host name of the DB, if you prefer you can pass the host name also as tmp-db and remove the network_mode: service:tmp-db.

                  这篇关于docker 容器启动后运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 cmakelist.txt 中添加和链接 mysql 库 下一篇:mySQL init 脚本未与 docker-compose 一起运行

                  相关文章

                  • <bdo id='D5PmE'></bdo><ul id='D5PmE'></ul>
                  <i id='D5PmE'><tr id='D5PmE'><dt id='D5PmE'><q id='D5PmE'><span id='D5PmE'><b id='D5PmE'><form id='D5PmE'><ins id='D5PmE'></ins><ul id='D5PmE'></ul><sub id='D5PmE'></sub></form><legend id='D5PmE'></legend><bdo id='D5PmE'><pre id='D5PmE'><center id='D5PmE'></center></pre></bdo></b><th id='D5PmE'></th></span></q></dt></tr></i><div id='D5PmE'><tfoot id='D5PmE'></tfoot><dl id='D5PmE'><fieldset id='D5PmE'></fieldset></dl></div>

                    1. <tfoot id='D5PmE'></tfoot>

                      <small id='D5PmE'></small><noframes id='D5PmE'>

                      <legend id='D5PmE'><style id='D5PmE'><dir id='D5PmE'><q id='D5PmE'></q></dir></style></legend>