如何从 dockerfile 启动 Docker Compose 并一起运行 curl

Posted

技术标签:

【中文标题】如何从 dockerfile 启动 Docker Compose 并一起运行 curl【英文标题】:How to launch Docker Compose and run curl together from dockerfile 【发布时间】:2021-09-23 22:51:26 【问题描述】:

我在为我的应用程序编写 dockerfile 时遇到问题。我的代码是beloy:

# define a imagem base
FROM ubuntu:latest

# define a owner image
LABEL maintainer="MyCompany"

# Update a image with packages
RUN apt-get update && apt-get upgrade -y

# Expose port 80
EXPOSE 8089

# Command to start my docker compose file
CMD ["docker-compose -f compose.yaml up -d"]

# Command to link KafkaConnect with mysql (images in docker compose file)
CMD ["curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" 
localhost:8083/connectors/ -d "
 \"name\": \"inventory-connector\",
      \"config\":  
            \"connector.class\": \"io.debezium.connector.mysql.MySqlConnector\",
            \"tasks.max\": \"1\", 
            \"database.hostname\": \"mysql\",
            \"database.port\": \"3306\",
            \"database.user\": \"debezium\",
            \"database.password\": \"dbz\",
            \"database.server.id\": \"184054\",
            \"database.server.name\": \"dbserver1\",
            \"database.include.list\": \"inventory\",
            \"database.history.kafka.bootstrap.servers\": \"kafka:9092\",
            \"database.history.kafka.topic\": \"dbhistory.inventory\"
      
"]

我知道 dockerfile 文件中只能有一个 CMD。 如何运行我的撰写文件,然后进行 cURL 调用?

【问题讨论】:

在容器中运行docker-compose作为主命令有点不寻常;通常你只需在主机上运行它来启动你的其他容器。您可以在其他容器之一中将 curl 命令作为启动序列的一部分运行,或者原则上您可以将其作为 Compose 启动的“服务”包含在内。 #1 你的问题解决了吗? #2 需要在你的docker-compose.yml中设置一个容器的连接值吗? 【参考方案1】:

您需要为此使用RUN 命令。检查this answer 以了解RUNCMD 之间的区别。

如果您的第二个 CMD 是 Dockerfile 中的最终命令,则只需更改以下行:

# Command to start my docker compose file
RUN docker-compose -f compose.yaml up -d

如果您现在拥有的CMDs 之后还有更多命令要运行,请尝试以下操作:

# Command to start my docker compose file
RUN docker-compose -f compose.yaml up -d

# Command to link KafkaConnect with MySql (images in docker compose file)
RUN curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" 
localhost:8083/connectors/ -d "
 \"name\": \"inventory-connector\",
      \"config\":  
            \"connector.class\": \"io.debezium.connector.mysql.MySqlConnector\",
            \"tasks.max\": \"1\", 
            \"database.hostname\": \"mysql\",
            \"database.port\": \"3306\",
            \"database.user\": \"debezium\",
            \"database.password\": \"dbz\",
            \"database.server.id\": \"184054\",
            \"database.server.name\": \"dbserver1\",
            \"database.include.list\": \"inventory\",
            \"database.history.kafka.bootstrap.servers\": \"kafka:9092\",
            \"database.history.kafka.topic\": \"dbhistory.inventory\"
      
"

# To set your ENTRYPOINT at the end of the file, uncomment the following line
# ENTRYPOINT ["some-other-command-you-need", "arg1", "arg2"]

【讨论】:

【参考方案2】:

这是另一个选项 - 从您正在创建的 Kafka Connect 容器中运行 curl。它看起来像这样:

  kafka-connect:
    image: confluentinc/cp-kafka-connect-base:6.2.0
    container_name: kafka-connect
    depends_on:
      - broker
    ports:
      - 8083:8083
    environment:
      CONNECT_BOOTSTRAP_SERVERS: "kafka:9092"
      CONNECT_REST_ADVERTISED_HOST_NAME: "kafka-connect"
      CONNECT_REST_PORT: 8083
      CONNECT_GROUP_ID: kafka-connect
      CONNECT_CONFIG_STORAGE_TOPIC: _kafka-connect-configs
      CONNECT_OFFSET_STORAGE_TOPIC: _kafka-connect-offsets
      CONNECT_STATUS_STORAGE_TOPIC: _kafka-connect-status
      CONNECT_KEY_CONVERTER: io.confluent.connect.avro.AvroConverter
      CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL: 'http://schema-registry:8081'
      CONNECT_VALUE_CONVERTER: io.confluent.connect.avro.AvroConverter
      CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: 'http://schema-registry:8081'
      CONNECT_LOG4J_ROOT_LOGLEVEL: "INFO"
      CONNECT_LOG4J_LOGGERS: "org.apache.kafka.connect.runtime.rest=WARN,org.reflections=ERROR"
      CONNECT_LOG4J_APPENDER_STDOUT_LAYOUT_CONVERSIONPATTERN: "[%d] %p %Xconnector.context%m (%c:%L)%n"
      CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: "1"
      CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: "1"
      CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: "1"
    command: 
      - bash 
      - -c 
      - |
        #
        echo "Installing connector plugins"
        confluent-hub install --no-prompt debezium/debezium-connector-mysql:1.5.0
        #
        echo "Launching Kafka Connect worker"
        /etc/confluent/docker/run & 
        #
        echo "Waiting for Kafka Connect to start listening on localhost ⏳"
        while : ; do
          curl_status=$$(curl -s -o /dev/null -w %http_code http://localhost:8083/connectors)
          echo -e $$(date) " Kafka Connect listener HTTP state: " $$curl_status " (waiting for 200)"
          if [ $$curl_status -eq 200 ] ; then
            break
          fi
          sleep 5 
        done
        echo -e "\n--\n+> Creating Data Generator source"
        curl -s -X PUT -H  "Content-Type:application/json" http://localhost:8083/connectors/inventory-connector/config \
            -d ' 
                "connector.class": "io.debezium.connector.mysql.MySqlConnector",
                "tasks.max": "1", 
                "database.hostname": "mysql",
                "database.port": "3306",
                "database.user": "debezium",
                "database.password": "dbz",
                "database.server.id": "184054",
                "database.server.name": "dbserver1",
                "database.include.list": "inventory",
                "database.history.kafka.bootstrap.servers": "kafka:9092",
                "database.history.kafka.topic": "dbhistory.inventory"
        '
        sleep infinity

你可以看到full Docker Compose here

【讨论】:

以上是关于如何从 dockerfile 启动 Docker Compose 并一起运行 curl的主要内容,如果未能解决你的问题,请参考以下文章

docker 在没有dockerfile的情况下 如何查看一个运行容器的启动命令

如何从 Docker 镜像里提取 dockerfile!

Docker-Dockerfile1

用Dockerfile做docker镜像多应用启动mysql,guacd,tomcat

Docker容器学习梳理-Dockerfile构建镜像

如何将文件夹从我的主机挂载到自定义 docker 映像,在目录中使用 Dockerfile 进行设置? [复制]