无法在GCP虚拟机的启动脚本中执行Docker Compose和启动Docker Compose。

huangapple go评论57阅读模式
英文:

Not able to execute the docker compose and up a docker compose in the startup script of a GCP vm

问题

以下是翻译好的部分:

#!/bin/bash
sudo apt update
sudo apt install -y docker.io
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo git clone https://github.com/XXXXX/Nodejs_Kafka.git /home/XXXX/app
sudo mv /home/XXXXX/app/docker-compose.yml /home/XXXXXXX/docker-compose.yml
sudo cd /home/XXXXXX
sudo docker-compose up -d

我尝试使用Node.js客户端库,然后使用它来创建一个GCP虚拟机,提供所有必要的详细信息,并在元数据部分提供了这个脚本。我克隆了一个仓库以获取docker-compose文件,并将其移动到主目录。但每当我通过SSH登录到虚拟机,然后尝试通过"docker ps -a"查看容器时,从未创建任何容器,这意味着Docker Compose从未运行。

正如@Puteri提到的,如果在SSH之后逐个执行,它百分之百有效。

英文:

This below start up script doesn't result in Kafka containers running when I check using "docker ps -a".

              #!/bin/bash
              sudo apt update
              sudo apt install -y docker.io
              sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
              sudo chmod +x /usr/local/bin/docker-compose
              sudo git clone https://github.com/XXXXX/Nodejs_Kafka.git  /home/XXXX/app 
              sudo mv /home/XXXXX/app/docker-compose.yml /home/XXXXXXX/docker-compose.yml
              sudo cd /home/XXXXXX
              sudo docker-compose up -d

I tried using a nodejs client library and then using it to create a GCP VM giving all the necessary details and in the metadata section I gave this script. I cloned a repo to get a docker-compose file and moved to home directory. But whenever I login to the vm by ssh and then try to see the containers by giving "docker ps -a" , no containers are ever created , which means the docker compose never ran.

As @Puteri mentioned, if executed on bash one by one after ssh, it 100 % works.

答案1

得分: 1

The sudo was really indeed not required. And as to the answer to my question, I found that the Docker Compose YAML file with 9 containers was taking its own time to be up. After 4-5 minutes when I checked randomly, I suddenly found it to be there, all up. Kudos.

英文:

OK the sudo was really indeed not required. And as to the answer to my question, i found that the docker compose yaml file being having 9 containers , it was taking its own time to be up. After 4-5 mins when i checked randomly , i suddenly found it to be there, all up. Kudos

答案2

得分: 0

以下是翻译好的内容:

  1. 如 @puteri 所提到的,在手动在虚拟机上运行脚本之前,请检查日志中是否有任何错误。
  2. 为了更好地管理生命周期,可以编写一个带有 restart=true 的 systemd 单元(与 bash 脚本一起),然后在引导过程中启用/启动该单元。
  3. 如 @guillaume 所提到的,尝试在脚本中不使用 sudo 或切换到非根用户运行。
  4. 你也可以尝试按照社区成员 Sim 在 此SO帖子 中建议的方式以分离的进程运行 Docker 容器。
  5. 若要进行更多的故障排除,可以参考这个文档
英文:

<p dir="ltr" style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt; margin: 0;"><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Below troubleshooting steps will help you in resolving the issue:</span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">1.As @puteri mentioned, check if the script is running when you manually run on the VM and before that check for any errors in the logs.</span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /><br /></span></p>
<p style="margin: 0;"><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">2.For better lifetime management by writing out a systemd unit (together with the bash script) with restart=true and just enable/start the unit during the boot process.</span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">3.As @guillaume mentioned, try to run without sudo or switch to a non-root user in the script.</span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">4.You can also try running a docker container in the detached process as suggested by the community member Sim in the </span><a href="https://stackoverflow.com/questions/34029680/docker-detached-mode"><span style="font-size: 11pt; font-family: Arial; color: #1155cc; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: underline; -webkit-text-decoration-skip: none; text-decoration-skip-ink: none; vertical-align: baseline; white-space: pre-wrap;">SO post</span></a><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.</span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">For more troubleshooting you can follow this </span><a href="https://cloud.google.com/container-optimized-os/docs/how-to/run-container-instance#starting_a_docker_container_via_cloud-config"><span style="font-size: 11pt; font-family: Arial; color: #1155cc; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: underline; -webkit-text-decoration-skip: none; text-decoration-skip-ink: none; vertical-align: baseline; white-space: pre-wrap;">document</span></a><span style="font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">.</span></p>

huangapple
  • 本文由 发表于 2023年6月8日 13:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定