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?

More than 1 year has passed since last update.

【Docker入門】Dockerチュートリアル(Hands-on)

Posted at

はじめに

Docker入門。とりあえず、手を動かして理解する。

課題

10秒に1回HelloWorldを出力するDockerコンテナを作る。

ディレクトリ一覧
~/develop/docker/cron_task$ tree
.
├── Dockerfile
├── cronjob
├── docker-compose.yml
└── my_script.ts

1 directory, 4 files
Dockerfile
FROM node:14

# ソースコードをコピー
COPY my_script.ts /app/my_script.ts

# ワーキングディレクトリの設定
WORKDIR /app

# 必要なパッケージのインストール
RUN npm install typescript -g

# TypeScriptのコンパイル
RUN tsc my_script.ts

# cronジョブの設定ファイルをコピー
COPY cronjob /etc/cron.d/my_cronjob

# 設定ファイルの権限を変更
RUN chmod 0644 /etc/cron.d/my_cronjob

# cronデーモンを起動
CMD cron && tail -f /var/log/cron.log

docker-compose.yml
version: '3'
services:
  my_container:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./my_script.ts:/app/my_script.ts
    command: ["node", "my_script.js"]
app.ts
const interval = process.env.INTERVAL || 10;

const helloWorld = () => {
  console.log('Hello, World!');
};

const helloWorldInterval = setInterval(helloWorld, parseInt(interval) * 1000);

const main = () => {
  console.log('===============start=================');
  helloWorldInterval;
};

// main 関数を実行する
main();
#動作確認のため、app.tsを実行
~/develop/docker/cron_task$ tree
.
├── Dockerfile
├── cronjob
├── docker-compose.yml
└── my_script.ts

1 directory, 4 files
my_script.ts
function sayHello() {
    console.log('Hello, World!');
  }

setInterval(sayHello, 10000);
cronjob
* * * * * root node /app/my_script.js
Dockerfile
FROM node:14

# ソースコードをコピー
COPY my_script.ts /app/my_script.ts

# ワーキングディレクトリの設定
WORKDIR /app

# 必要なパッケージのインストール
RUN npm install typescript -g

# TypeScriptのコンパイル
RUN tsc my_script.ts

# cronジョブの設定ファイルをコピー
COPY cronjob /etc/cron.d/my_cronjob

# 設定ファイルの権限を変更
RUN chmod 0644 /etc/cron.d/my_cronjob

# cronデーモンを起動
CMD cron && tail -f /var/log/cron.log
docker-compose.yml
version: '3'
services:
  my_container:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./my_script.ts:/app/my_script.ts
    command: ["node", "my_script.js"]
実行結果
~/develop/docker/cron_task$ docker-compose up --build
[+] Building 5.6s (12/12) FINISHED                                                                                                                                                     
 => [internal] load .dockerignore                                                                                                                                                 0.0s
 => => transferring context: 2B                                                                                                                                                   0.0s
 => [internal] load build definition from Dockerfile                                                                                                                              0.0s
 => => transferring dockerfile: 581B                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/node:14                                                                                                                        1.7s
 => CACHED [1/7] FROM docker.io/library/node:14@sha256:a158d3b9b4e3fa813fa6c8c590b8f0a860e015ad4e59bbce5744d2f6fd8461aa                                                           0.0s
 => [internal] load build context                                                                                                                                                 0.0s
 => => transferring context: 233B                                                                                                                                                 0.0s
 => [2/7] COPY my_script.ts /app/my_script.ts                                                                                                                                     0.0s
 => [3/7] WORKDIR /app                                                                                                                                                            0.0s
 => [4/7] RUN npm install typescript -g                                                                                                                                           2.5s
 => [5/7] RUN tsc my_script.ts                                                                                                                                                    1.0s
 => [6/7] COPY cronjob /etc/cron.d/my_cronjob                                                                                                                                     0.0s
 => [7/7] RUN chmod 0644 /etc/cron.d/my_cronjob                                                                                                                                   0.3s
 => exporting to image                                                                                                                                                            0.1s
 => => exporting layers                                                                                                                                                           0.1s
 => => writing image sha256:5318c08da26326f10d55efca2fe0ac2fa2483ec28b6c92884efa3ee2cf2e1004                                                                                      0.0s
 => => naming to docker.io/library/cron_task-my_container                                                                                                                         0.0s
[+] Running 1/0
 ✔ Container cron_task-my_container-1  Recreated                                                                                                                                  0.1s 
Attaching to cron_task-my_container-1
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!
cron_task-my_container-1  | Hello, World!

→一応できた。ただ、アプリケーション側で10秒に1回実行している。うーん、、いまいち。次回以降で綺麗にしていこうと思います。

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?