代码之家  ›  专栏  ›  技术社区  ›  MarkeD

检测docker是否应该在启动时运行或启动容器-使用systemd,但它会不断传播错误-如何停止?

  •  0
  • MarkeD  · 技术社区  · 7 年前

    我尝试在Google Compute Engine实例上使用启动脚本来执行以下操作之一:

    1. 如果名为rstudio的docker容器存在但处于停止状态,请运行 docker start rstudio
    2. 如果docker容器不存在,请运行 rstudio run --name=rstudio rocker/rstudio

    SO 我认为这可以通过 docker top rstudio || docker run --name=rstudio rocker/rstudio 但它似乎总是错误的 docker top rstudio docker top rstudio &>/dev/null 但没有效果。

    我有一个在实例启动时运行的云配置。

    #cloud-config
    
    users:
    - name: gcer
      uid: 2000
    
    write_files:
    - path: /home/gcer/docker-rstudio.sh
      permissions: 0755
      owner: root
      content: |
        #!/bin/bash
    
        echo "Docker RStudio launch script"
        if ! docker top rstudio &>/dev/null
        then
          echo "Pulling new rstudio"
          docker run -p 80:8787 \
                     -e ROOT=TRUE \
                     -e USER=%s -e PASSWORD=%s \
                      -v /home/gcer:/home/rstudio \
                      --name=rstudio \
                      %s
        else
          echo "Starting existing rstudio"
          docker start rstudio
        fi
    
    - path: /etc/systemd/system/rstudio.service
      permissions: 0644
      owner: root
      content: |
        [Unit]
        Description=RStudio Server
        Requires=docker.service
        After=docker.service
    
        [Service]
        Restart=always
        Environment="HOME=/home/gcer"
        ExecStartPre=/usr/share/google/dockercfg_update.sh
        ExecStart=-/home/gcer/docker-rstudio.sh
        ExecStop=/usr/bin/docker stop rstudio
    
    runcmd:
    - systemctl daemon-reload
    - systemctl start rstudio.service
    

    无论我尝试什么,当我运行时都会出现这个错误日志 sudo journalctl -u rstudio.service

    Feb 14 23:26:09 test-9 systemd[1]: Started RStudio Server.
    Feb 14 23:26:09 test-9 docker[770]: Error response from daemon: No such container: rstudio
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Control process exited, code=exited status=1
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Unit entered failed state.
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Failed with result 'exit-code'.
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Service hold-off time over, scheduling restart.
    Feb 14 23:26:09 test-9 systemd[1]: Stopped RStudio Server.
    Feb 14 23:26:09 test-9 systemd[1]: Starting RStudio Server...
    ...
    Feb 14 23:26:09 test-9 systemd[1]: Started RStudio Server.
    Feb 14 23:26:09 test-9 docker[809]: Error response from daemon: No such container: rstudio
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Control process exited, code=exited status=1
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Unit entered failed state.
    Feb 14 23:26:09 test-9 systemd[1]: rstudio.service: Failed with result 'exit-code'.
    Feb 14 23:26:10 test-9 systemd[1]: rstudio.service: Service hold-off time over, scheduling restart.
    Feb 14 23:26:10 test-9 systemd[1]: Stopped RStudio Server.
    Feb 14 23:26:10 test-9 systemd[1]: rstudio.service: Start request repeated too quickly.
    Feb 14 23:26:10 test-9 systemd[1]: Failed to start RStudio Server.
    Feb 14 23:26:10 test-9 systemd[1]: rstudio.service: Unit entered failed state.
    Feb 14 23:26:10 test-9 systemd[1]: rstudio.service: Failed with result 'exit-code'.
    

    有人能帮我把这个弄好吗?

    0 回复  |  直到 7 年前
        1
  •  0
  •   David Maze    7 年前

    我会在你停止时删除容器。然后,您的启动脚本将简化为确保容器被无条件删除 docker run 重新开始。

    这将使脚本的全部内容为:

    #!/bin/sh
    docker stop rstudio
    docker rm rstudio
    docker run -p 80:8787 \
       --name=rstudio \
       ... \
       rstudio run --name=rstudio rocker/rstudio
    

    没有 set -e 选项,即使前面的命令失败(因为容器不存在),脚本仍将继续执行 码头工人赛跑

    类似地,为了更好地清理,我将更改systemd unit文件以在容器停止后删除它

    ExecStop=/usr/bin/docker stop rstudio
    ExecStopPost=/usr/bin/docker rm rstudio
    

    (您的安装程序有三种可能的状态:容器正在运行;容器存在但已停止;容器不存在。我的安装程序删除了“存在但已停止”状态,该状态没有太多价值,特别是因为您使用了 docker run -v 用于在容器空间之外存储数据的选项。)

    推荐文章