はじめに
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を自分で作成するのは大変なため、これを使用して楽をしたい!ということから試してみる。
環境
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
のプロジェクトを動かすつもりで使う。
実施
- ディレクトリを作成する。
~/work/docker-init-nodejs
としてプロジェクトフォルダを作成した。
docker init
を実行する。
2-1. Nodeを選択する。
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を使用する。
? What application platform does your project use? Node
? What version of Node do you want to use? 18.16.0
2-3. パッケージマネージャの選択
npm
にする。
? Which package manager do you want to use? npm
2-4. アプリケーションを起動するときのコマンド
npm run serve
にした。
? What command do you want to use to start the app? npm run serve
2-5. どのポートでサーバにアクセスするか
8888
にした。
? What port does your server listen on? 8888
これで完成
$ 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
- 直下のファイルの確認
$ ls
Dockerfile compose.yaml
上記のファイルができているようだった。
Dockerfile
,compose.yaml
の確認
# 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
# 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
- ビルドしてみる。
docker compose up -d --build
failed to solve: failed to compute cache key: failed to calculate checksum of ref moby::ntgwio6umva0ica7fl12wc17l: "/package.json": not found
空のプロジェクトなので、当然怒られた。
逆にあとから Node.js
の環境を作ってみる。
npm init
を実行する。
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)
npm install
を実行する。
npm install
- 再度
docker compose up -d --build
を実行する。
✔ Container docker-init-nodejs-server-1 Started
作成はできている。
何かを確認したいので、 express
を入れる。
npm install express --save
を実行する。index.js
を作成する。
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);
Dockerfile
を書き換える。
-CMD npm run serve
+CMD node index.js
docker compose up -d --build
を実行する。
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
、まだサポートしている言語が少ないが、業務でよく使用しているのが増えたらかなり楽になりそう。