【Docker入门】Docker教程(亲自动手)

首先

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

学习主题

创建一个 Docker 容器,在每秒钟输出一次 HelloWorld。

~/develop/docker/cron_task$ tree
.
├── Dockerfile
├── cronjob
├── docker-compose.yml
└── my_script.ts

1 directory, 4 files
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

version: '3'
services:
  my_container:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./my_script.ts:/app/my_script.ts
    command: ["node", "my_script.js"]
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
function sayHello() {
    console.log('Hello, World!');
  }

setInterval(sayHello, 10000);
* * * * * root node /app/my_script.js
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
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秒執行一次。嗯…感覺還不夠好。下次將在以後改進。

广告
将在 10 秒后关闭
bannerAds