はじめに

docker init というコマンドが Docker Desktop 4.18以降で Docker init プラグインとして提供されているようだ。

Docker Desktop 4.18 and later provides the Docker Init plugin with the docker init CLI command.

このコマンドを実行することで、既存のプロジェクトに対して、 .dockerignore, Dockerfile, compose.yaml のファイルを生成してくれる。
Dockerfileを自分で作成するのは大変なため、これを使用して楽をしたい!ということから試してみる。

環境

1
2
3
4
5
Windows 10 Professional
WSL2 (Ubuntu22.04 LTS)
Docker Desktop 4.19.0 (106363) 
docker version 23.0.5
Docker Compose version v2.17.3

試す

https://docs.docker.com/engine/reference/commandline/init/ のページを参考に実際に適当なディレクトリで使ってみる。

要件

  • Node.js のプロジェクトを動かすつもりで使う。

実施

  1. ディレクトリを作成する。
1
2
~/work/docker-init-nodejs
としてプロジェクトフォルダを作成した。
  1. docker init を実行する。

2-1. Nodeを選択する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

? What application platform does your project use?  [Use arrows to move, type to filter]
  Go - suitable for a Go server application
  Python - suitable for a Python server application
  Node - suitable for a Node server application
> Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit

Nodeにカーソルを合わせて、Enterする。

2-2. バージョンを入力する。 今回は、Node.js 18.16.0を使用する。

1
2
? What application platform does your project use? Node
? What version of Node do you want to use? 18.16.0

2-3. パッケージマネージャの選択 npm にする。

1
? Which package manager do you want to use? npm

2-4. アプリケーションを起動するときのコマンド npm run serve にした。

1
? What command do you want to use to start the app? npm run serve

2-5. どのポートでサーバにアクセスするか 8888 にした。

1
? What port does your server listen on? 8888

これで完成

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$ docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

? What application platform does your project use? Node
? What version of Node do you want to use? 18.16.0
? Which package manager do you want to use? npm
? What command do you want to use to start the app? npm run serve
? What port does your server listen on? 8888

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: compose.yaml

 Your Docker files are ready!

Take a moment to review them and tailor them to your application.

WARNING: The following files required to run your application were not found. Be sure to create them before running your application:
  - package.json
  - package-lock.json

When you're ready, start your application by running: docker compose up --build

Your application will be available at http://localhost:8888
  1. 直下のファイルの確認
1
2
$ ls
Dockerfile  compose.yaml

上記のファイルができているようだった。

  1. Dockerfile, compose.yaml の確認
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

ARG NODE_VERSION=18.16.0

FROM node:${NODE_VERSION}-alpine

# Use production node environment by default.
ENV NODE_ENV production


WORKDIR /usr/src/app

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
    --mount=type=bind,source=package-lock.json,target=package-lock.json \
    --mount=type=cache,target=/root/.npm \
    npm ci --omit=dev

# Run the application as a non-root user.
USER node

# Copy the rest of the source files into the image.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8888

# Run the application.
CMD npm run serve
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker compose reference guide at
# https://docs.docker.com/compose/compose-file/

# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
  server:
    build:
      context: .
    environment:
      NODE_ENV: production
    ports:
      - 8888:8888

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker-compose up`.
#     depends_on:
#       db:
#         condition: service_healthy
#   db:
#     image: postgres
#     restart: always
#     user: postgres
#     secrets:
#       - db-password
#     volumes:
#       - db-data:/var/lib/postgresql/data
#     environment:
#       - POSTGRES_DB=example
#       - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
#     expose:
#       - 5432
#     healthcheck:
#       test: [ "CMD", "pg_isready" ]
#       interval: 10s
#       timeout: 5s
#       retries: 5
# volumes:
#   db-data:
# secrets:
#   db-password:
#     file: db/password.txt
  1. ビルドしてみる。
1
docker compose up -d --build
1
failed to solve: failed to compute cache key: failed to calculate checksum of ref moby::ntgwio6umva0ica7fl12wc17l: "/package.json": not found

空のプロジェクトなので、当然怒られた。

逆にあとから Node.js の環境を作ってみる。

  1. npm init を実行する。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (docker-init-nodejs)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to work/docker-init-nodejs/package.json:

{
  "name": "docker-init-nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)
  1. npm install を実行する。
1
npm install
  1. 再度 docker compose up -d --build を実行する。
1
✔ Container docker-init-nodejs-server-1  Started

作成はできている。

何かを確認したいので、 express を入れる。

  1. npm install express --save を実行する。

  2. index.js を作成する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const http = require('http');
const express = require('express');

const app = express();

app.get("/", function(req, res){
    return res.send("Hello World");
});

const server = http.createServer(app);
server.listen(8888);
  1. Dockerfileを書き換える。
1
2
-CMD npm run serve
+CMD node index.js
  1. docker compose up -d --build を実行する。
1
2
3
4
5
docker compose up -d --build

NAME                          IMAGE                       COMMAND                  SERVICE             CREATED
   STATUS              PORTS
docker-init-nodejs-server-1   docker-init-nodejs-server   "docker-entrypoint.s…"   server              5 seconds ago       Up 3 seconds        0.0.0.0:8888->8888/tcp

できた。

↓アクセスできることも確認

docker-init

参考

おわりに

docker init、まだサポートしている言語が少ないが、業務でよく使用しているのが増えたらかなり楽になりそう。