LoginSignup
0
0

docker でexpressアプリケーションを作成して、imageをbuild します。

Posted at

expressアプリケーションを作成します。

localhost:3000ポートにHello worldを表示させるコードです。

indexjs
    const express = require("express");
    const app = express();
    const PORT = 3000;
    
    app.get("/", (req, res) => {
      res.send("hello world");
    });
    
    app.listen(PORT, () => {
      console.log(`server is running on port ${PORT}`);
    });

ブラウザには、下記の様に表示されます。
image.png

Dockerfileを記述します。

Dockerfile
    # ベースイメージを設定します。
    FROM node:lts-alpine
    # 実行ディレクトリを作成します。
    WORKDIR /usr/src/app
    # ローカルのカレントディレクトリをapp/ディレクトリにコピーします。
    COPY . /usr/src/app/
    # npm install を実行します。
    RUN npm install
    # Dockerのポートを設定します。
    EXPOSE 3000
    # コンテナを実行するコマンドです。
    CMD ["node", "index.js"]
    

Imageをbuildする

terminal
    docker build -t node-app:1.0 .

最後の.は、Dockerfileの場所を指定します。カレントディレクトリーの場合は、"."と記述する。

image.png

最後に、imageファイルを実行して、shellから接続してみます。

terminal
    docker run -d --name test -p 3000:3000 node-app:1.0
    docker exec -it test sh 

image.png

実行したコンテナのappディレクトリにすべてコピーされていることがわかります。

image.png

最後に

今回は、expressのイメージをビルドすることができました。次は、mysqlのコンテナを作成してみます。

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