Closed20

remote-container の Cannot reconnect. Please reload the window. に立ち向かう

tkttkt


開いてしばらくすると、このメッセージが出てコンテナにアクセスできなくなる

tkttkt
docker-compose.yml
version: "3.7"
services:
  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    working_dir: /workspace
+   command: "/bin/bash" # ここを足すと動くようになる
    environment:
      - USER=$USER
      - DISCORD_TOKEN=$DISCORD_TOKEN
    volumes:
      - .:/workspace:cached
      - target:/workspace/target:cached
    tty: true
  tf:
    image: hashicorp/terraform:1.0.0
    container_name: "terraform"
    working_dir: /workspace
    environment:
      - TF_VAR_billing_id=$TF_VAR_billing_id
    volumes:
      - ./terraform:/workspace:cached
      - gcloud-config:/root/.config
  gcloud:
    entrypoint: "gcloud"
    image: google/cloud-sdk:alpine
    container_name: "gcloud"
    working_dir: /workspace
    volumes:
      - ./terraform:/workspace:cached
      - gcloud-config:/root/.config
volumes:
  target:
  gcloud-config:
tkttkt
FROM rust:1.49-slim-buster

RUN apt-get update && \
    apt-get install -y \
    libopus-dev \
    build-essential \
    autoconf \
    automake \
    libtool \
    m4 \
    ffmpeg \
    curl \
    python \
 && apt-get -y clean \
 && rm -rf /var/lib/apt/lists/*

RUN curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/bin/youtube-dl && \
    chmod a+rx /usr/bin/youtube-dl

ENV LC_ALL=C.UTF-8

COPY Cargo.lock Cargo.lock
COPY Cargo.toml Cargo.toml

COPY  . .

CMD [ "/bin/sh",  "-c", "cargo run" ]
tkttkt
devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.165.1/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
	"name": "discord-speech-bot",

	// Update the 'dockerComposeFile' list if you have more compose files or use different names.
	// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
	"dockerComposeFile": [
		"./../docker-compose.yml"
	],
	// The 'service' property is the name of the service for the container that VS Code should
	// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
	"service": "app",

	// The optional 'workspaceFolder' property is the path VS Code should open by default when
	// connected. This is typically a file mount in .devcontainer/docker-compose.yml
	"workspaceFolder": "/workspace",

	// Set *default* container specific settings.json values on container create.
	"settings": {
		"terminal.integrated.shell.linux": null
	},

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"rust-lang.rust",
		"hashicorp.terraform"
	],

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// Uncomment the next line if you want start specific services in your Docker Compose config.
	// "runServices": [],

	// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
	// "shutdownAction": "none",

	// Uncomment the next line to run commands after the container is created - for example installing curl.
	// "postCreateCommand": "apt-get update && apt-get install -y curl",

	// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
	// "remoteUser": "vscode"
}
tkttkt

remote-container 側で container の維持をコントロールしてくれるわけではないから、docker-compose up の後に exit するような内容の場合、接続が維持できない説

tkttkt
CMD [ "/bin/sh",  "-c", "cargo run" ]

これで run してるから(run すると落とすまでプロセスが立つような処理が入ってる)落ちないはずでは?と思って覗いてみたら、エラーでコケてた。
エラー出てるときもあるだろうから、これでコケると remote-container も起動しない、だと不便

tkttkt

remote-container 用の docker-compose を作るのも手だけど、メンテコストを考えるとやりたくない
docker-compose の動作は期待通りではないけど command: "/bin/bash" つけるのが良いんだろうか

tkttkt

devcontainer 側で command を override すれば解決しそう
できるのか調べる

tkttkt
	"postAttachCommand": "while sleep 1000; do :; done",

これで解決したりしないかなと思って書いてみたけど、

[28156 ms] Extensions cache, remote removals: None
[47151 ms] Error: Command failed: /bin/sh -c while sleep 1000; do :; done
        at ag (/home/tkt/.vscode/extensions/ms-vscode-remote.remote-containers-0.183.0/dist/extension/extension.js:169:15)
        at async _V (/home/tkt/.vscode/extensions/ms-vscode-remote.remote-containers-0.183.0/dist/extension/extension.js:163:2880)
        at async /home/tkt/.vscode/extensions/ms-vscode-remote.remote-containers-0.183.0/dist/extension/extension.js:145:7815

しばらくしたらエラーで落ちた

tkttkt

devcontainer のときだけ override の yml を読みに行くみたいな制御はできないかな

tkttkt

docker-compose.yml はそのままに、remote-container で開いても container が落ちないようになった

tkttkt
.devcontainer/docker-compose.devcontainer.yml
version: "3.7"
services:
  app:
    command: "/bin/sh -c \"while sleep 1000; do :; done\""
.devcontainer/devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.165.1/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
	"name": "discord-speech-bot",

	// Update the 'dockerComposeFile' list if you have more compose files or use different names.
	// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
        // ここで base と extend の2つを指定する
	"dockerComposeFile": [
		"./../docker-compose.yml", "./docker-compose.devcontainer.yml"
	],
	// The 'service' property is the name of the service for the container that VS Code should
	// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
	"service": "app",

	// The optional 'workspaceFolder' property is the path VS Code should open by default when
	// connected. This is typically a file mount in .devcontainer/docker-compose.yml
	"workspaceFolder": "/workspace",

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"rust-lang.rust",
		"hashicorp.terraform"
	],

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// Uncomment the next line if you want start specific services in your Docker Compose config.
	// "runServices": [],

	// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
	// "shutdownAction": "none",

	// Uncomment the next line to run commands after the container is created - for example installing curl.
	// "onCreateCommand": "rustup component add clippy rust-analysis rust-src rls && apt install git -y",

	// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
	// "remoteUser": "vscode"
}
このスクラップは2021/06/24にクローズされました