0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerfileに書いたCMDがdevcontainerで実行されていないっぽい

Last updated at Posted at 2025-06-20

:warning: 注意

この記事は根本的な解決方法を示していません
誰か教えてください。

3行でまとめ

  1. DockerfileにCMDを書いてdevcontainerを開いた時にnodemonが起動しててほしかった。
  2. でもできてなかった。
  3. devcontainer.jsonのpostCreateCommandに書いて解決した。

やりたかったこと

vscode、devcontainerで実行環境を作る。
コンテナ作成時にホットリロードが起動するようにする。

devcontainer.json
{
    "name": "graphql-first-step",
    "overrideCommand": false,
    "build": {
        "dockerfile": "../Dockerfile-dev",
        "context": ".."
    },
    "containerEnv": {
        "CHOKIDAR_USEPOLLING": "true"
    }
}

Dockerfile-dev

FROM node:24-alpine

WORKDIR /app

COPY package.json ./
RUN npm i

COPY . .

RUN apk update && apk add git

EXPOSE 3000

CMD ["npm", "run", "dev"]

npm run devにはnodemonが登録されている。
これでreopen in containerしたときにnodemonが起動されているはずだったが...

nodemonが起動していない?!

devcontainerを開きターミナルを確認したところ...

/workspaces/graphql-first-step # 

なんのログもでていない?!

おかしい...ここにnodemonが起動したことを示すログが表示されるはずだが...
記事では表現できないが、ホットリロードも効かない

手動でなら起動できる

reopen in containerと同時に、とはいかなかったが、ターミナルからnpm run devと打ち込めばちゃんとnodemonが立ち上がる。

/workspaces/graphql-first-step # npm run dev

> graphql-first-step@1.0.0 dev
> nodemon

[nodemon] 3.1.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts
[nodemon] starting `ts-node ./index.ts`
Hello, World!!!
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `ts-node ./index.ts`
Hello, World!!
[nodemon] clean exit - waiting for changes before restart

これを立ち上げ時に自動でやってほしかったんだよな
パッケージ自体はちゃんと入ってるくさい

コンテナ作成時にはnodemon起動されてる

ホストマシンからコンテナのログを確認したところ、コンテナを立ち上げた時にはnodemonが起動されている

C:\Users\(伏せます)\Documents\graphql-first-step>docker logs ff0752cbd385
Container started

> graphql-first-step@1.0.0 dev
> nodemon

[nodemon] 3.1.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts
[nodemon] starting `ts-node ./index.ts`
Hello, World!!!
[nodemon] clean exit - waiting for changes before restart

ということはDockerfileのCMDは効いてるんだよな。
なのにdevcontainer内の環境にはそれが反映されてない...どういうこと???

こんな方法で解決しました

devcontainer.jsonにpostCreateCommandプロパティを追加し、そこでnpm run devさせました。

devcontainer.json
{
    "name": "graphql-first-step",
    "overrideCommand": false,
    "build": {
        "dockerfile": "../Dockerfile-dev",
        "context": ".."
    },
    "containerEnv": {
        "CHOKIDAR_USEPOLLING": "true"
    },
    "postCreateCommand": "npm run dev" <-ここ!
}

するとこのコマンドが実行され、reopenと同時にnodemonが起動してる...!

Running the postCreateCommand from devcontainer.json...

[9608 ms] Start: Run in container: /bin/sh -c npm run dev

> graphql-first-step@1.0.0 dev
> nodemon

[nodemon] 3.1.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts
[nodemon] starting `ts-node ./index.ts`
Hello, World!!
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `ts-node ./index.ts`
Hello, World!
[nodemon] clean exit - waiting for changes before restart

これがやりたかったんですわ

原因はわからない

でもこれ、DockerfileのCMDと原理的には同じことしてるんじゃないの?
なんでCMDはだめでpostCreateCommandはありなのか。

ボリュームマウントの設定とかをちゃんとした方がいいんですかね。
正直感覚でdevcontainer使ってたので原因が全く分かりません。
詳しい方教えてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?