<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>docker</title>
	<atom:link href="https://techgrowup.net/tag/docker/feed/" rel="self" type="application/rss+xml" />
	<link>https://techgrowup.net</link>
	<description>エンジニアを強くする</description>
	<lastBuildDate>Sat, 05 Feb 2022 11:00:00 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://techgrowup.net/wp-content/uploads/2021/05/hp-icon-150x150.png</url>
	<title>docker</title>
	<link>https://techgrowup.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>DockerでNode×PostgreSQL×Typescript環境を作る</title>
		<link>https://techgrowup.net/create-env-docker-node-postgres/</link>
					<comments>https://techgrowup.net/create-env-docker-node-postgres/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[techgrowup]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 10:59:59 +0000</pubDate>
				<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Typescript]]></category>
		<guid isPermaLink="false">https://techgrowup.net/?p=1498</guid>

					<description><![CDATA[はじめに 今回Dockerを利用して、Node×PostgreSQL×Typescript環境を作るときの手順を解説します。私自身バックエンドエンジニアとしては初学者レベルですが学んだことのアウトプットとして記載したいと [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="はじめに">はじめに</h2>



<p> 今回Dockerを利用して、Node×PostgreSQL×Typescript環境を作るときの手順を解説します。私自身バックエンドエンジニアとしては初学者レベルですが学んだことのアウトプットとして記載したいと思います。主な開発環境としては以下となります。</p>



<div class="wp-block-cocoon-blocks-blank-box-1 blank-box block-box">
<ul class="wp-block-list"><li><strong>Mac M1</strong></li><li><strong>Docker version 20.10.11</strong></li><li><strong>Postgres: 13</strong></li><li><strong>Node: 14</strong></li><li><strong>Typescript(4.5.5), Express(4.17.2) &lt;- 今回ここは大して関係ありません</strong></li></ul>
</div>



<h2 class="wp-block-heading" id="ゴール">ゴール</h2>



<p> 今回のゴールはDockerでNode環境、Postgres環境を作成し、http://localhost:3000を叩くと下記のような結果をPostgreSQLを通して取得できるところまでとなります。実際に開発する場合はセキュリティやDB設計、API設計などが重要になってくると思いますが、今回はその辺は特に記載しません。</p>



<pre class="wp-block-code"><code>&#91;
  {
    "id": 1,
    "created_at": "2022-02-05T10:22:44.994Z",
    "updated_at": "2022-02-05T10:22:44.994Z",
    "email": "test@gmail.com",
    "password": "password"
  }
]</code></pre>



<p> またフォルダ構成としては最終的に下記のような形になります。<span class="marker-under">appフォルダ、initdbフォルダ、docker-compose.ymlファイルを作成しておきましょう。</span></p>



<pre class="wp-block-code"><code>.
├── app
│   ├── Dockerfile
│   ├── package.json
│   └── src
│       └── index.ts
├── docker-compose.yml
└── initdb
    └── setup.sql</code></pre>



<h2 class="wp-block-heading" id="node環境の作成">Node環境の作成</h2>



<p> 次にnode環境を作るために<span class="marker-under">appフォルダに移動して</span>、下記コマンドを打ちましょう。「-y」コマンドはnpm init時に出てくる質問に全てyesで回答するためのコマンドです。今回は特別設定することは無いので、-yをつけています。</p>



<pre class="wp-block-code"><code>cd app
npm init -y </code></pre>



<p> これでpackage.jsonが作られたと思いますので、次は必要なパッケージ類をインストールしましょう。<br>1行目の<a rel="noopener" target="_blank" href="https://expressjs.com/ja/">express<span class="fa fa-external-link external-icon anchor-icon"></span></a>はWebアプリケーションを作成するためのフレームワークで、pgはPostgreSQLを利用するためのライブラリとなります。また、2行目はTypescriptを利用するためのライブラリとなります。</p>



<pre class="wp-block-code"><code>npm install --save express pg 
npm install --save-dev typescript ts-node @types/express @types/node @types/pg</code></pre>



<p> 次にappフォルダ配下に、<span class="marker-under">src/index.tsファイルを作成</span>し、下記コードを記載しましょう。ある程度コードを見れば分かると思いますが、expressを利用して&#8217;/&#8217;にリクエストが来た際に、PostgreSQLのusersテーブルを読込、レスポンスとして返すシンプルなルーティングです。</p>



<pre class="wp-block-code"><code>import express from 'express'
import { Pool } from 'pg'
const app: express.Express = express()

const pool = new Pool({
  host: 'postgres',
  database: 'postgres_db',
  user: 'postgres',
  password: 'password',
  port: 5432
})

// ルーティングの設定
app.get('/', async (req, res) => {
  const { rows } = await pool.query('select * from users')
  return res.send(rows)
})

const PORT = 3000
app.listen(PORT, () => {
  console.log(`server started on port ${PORT}`)
})
</code></pre>



<p> 最後にnodeを実行するために、package.jsonファイルのscriptsの部分に以下を付け足しましょう。これでTypescriptのnodeを実行できます。</p>



<pre class="wp-block-code"><code>"start": "npx ts-node ./src/index.ts"</code></pre>



<h2 class="wp-block-heading" id="docker環境の作成">Docker環境の作成</h2>



<p> appフォルダと同じ階層に、docker-compose.ymlを作成したと思いますので、中身に下記を記載しましょう。構成としてはnode環境とpostgers環境を作るためのコードを記載しています。<br> ちなみに後にinitdb内にsqlファイルを作成しますが、docker-entrypoint-initdb.dという場所にsqlファイルを置くと初期設定時に自動でsqlを実行してくれます。</p>



<pre class="wp-block-code"><code>version: "3.7"
services:
  node:
    container_name: node_express
    build: ./app
    volumes:
      - ./app:/app
    tty:  true
    ports:
      - 3000:3000

  postgres:
    container_name: postgres
    image: postgres:13
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: postgres_db
    volumes:
      - db_data:/var/lib/posrgresql/data
      - ./initdb:/docker-entrypoint-initdb.d

volumes:
  db_data: {}</code></pre>



<p> 次にappフォルダ内に<span class="marker-under">Dockerfileという名前のファイルを作成</span>し、以下を記載しましょう。下記でdocker起動時に[npm run start]コマンドを実行し、nodeを実行するようにしています。</p>



<pre class="wp-block-code"><code>FROM node:14-slim
RUN npm install
WORKDIR /app
CMD &#91;"npm", "run","start"]</code></pre>



<h2 class="wp-block-heading" id="初期db設定">初期DB設定</h2>



<p> docker作成時にdb設定を行うため、initdbフォルダに<span class="marker-under">setup.sqlというファイルを作成</span>しましょう。そしてその中に下記を記載しましょう。内容としてはシンプルで、usersテーブルを作成しサンプルとして1つINSERTを行っています。</p>



<pre class="wp-block-code"><code>set
    client_encoding = 'UTF8';

create table users(
    id serial primary key,
    created_at timestamp not null default current_timestamp,
    updated_at timestamp not null default current_timestamp,
    email text not null,
    password text not null
);

INSERT INTO
    users (email, password)
VALUES
    ('test@gmail.com', 'password');</code></pre>



<h2 class="wp-block-heading" id="実行してみる">実行してみる</h2>



<p> 最後に、docker-compose.ymlと同じ階層に戻り下記コマンドを打ちましょう。実行するとimageのダウンロードとdockerコンテナが起動します。</p>



<pre class="wp-block-code"><code>docker-compose up -d</code></pre>



<p> 無事に終わりましたら、ブラウザで<a rel="noopener" target="_blank" href="http://localhost:3000">http://localhost:3000<span class="fa fa-external-link external-icon anchor-icon"></span></a>を開くと下記が返されるようになったと思います。</p>



<pre class="wp-block-code"><code>&#91;
  {
    "id": 1,
    "created_at": "2022-02-05T10:22:44.994Z",
    "updated_at": "2022-02-05T10:22:44.994Z",
    "email": "test@gmail.com",
    "password": "password"
  }
]</code></pre>



<h2 class="wp-block-heading" id="まとめ">まとめ</h2>



<p> Dockerやnodeもまだまだ経験不足でしたが、自在に使えるようになるとかなり便利だなと思いました。次はPrismaなどのORマッパーも利用してみたいと思います。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techgrowup.net/create-env-docker-node-postgres/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Amazon SQSをDockerを使ってローカルで実行</title>
		<link>https://techgrowup.net/amazon-sqs/</link>
					<comments>https://techgrowup.net/amazon-sqs/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[techgrowup]]></dc:creator>
		<pubDate>Tue, 17 Aug 2021 14:20:24 +0000</pubDate>
				<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[sqs]]></category>
		<guid isPermaLink="false">https://techgrowup.net/?p=1356</guid>

					<description><![CDATA[Docker構築 Dockerfile作成 　今回はSQSと互換性のある、ElasticMQというものを使ってSQSをローカルで実現します。下記のコードをDockerfileにコピペしてください。　※最新のelastic [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Docker構築</h2>



<h3 class="wp-block-heading">Dockerfile作成</h3>



<p>　今回はSQSと互換性のある、ElasticMQというものを使ってSQSをローカルで実現します。下記のコードをDockerfileにコピペしてください。<br>　※最新のelasticmq-serverのバージョンが変わっているかもしれないので、変わっている場合はバージョンを変更してください。</p>





<a rel="noopener" target="_blank" href="https://github.com/softwaremill/elasticmq" title="GitHub - softwaremill/elasticmq: In-memory message queue with an Amazon SQS-compatible interface. Runs stand-alone or embedded." class="blogcard-wrap external-blogcard-wrap a-wrap cf"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://opengraph.githubassets.com/b7832420313e0d3bdf34a9e035a4136578d0a90a0438e05898fb4c62461aa71e/softwaremill/elasticmq" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">GitHub - softwaremill/elasticmq: In-memory message queue with an Amazon SQS-compatible interface. Runs stand-alone or embedded.</div><div class="blogcard-snippet external-blogcard-snippet">In-memory message queue with an Amazon SQS-compatible interface. Runs stand-alone or embedded. - softwaremill/elasticmq</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=https://github.com/softwaremill/elasticmq" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">github.com</div></div></div></div></a>




<pre class="wp-block-code"><code>FROM java:8

ADD https://s3-eu-west-1.amazonaws.com/softwaremill-public/elasticmq-server-1.2.0.jar /elasticmq/elasticmq-server-1.2.0.jar
EXPOSE 9324
ENTRYPOINT &#91;"java","-jar","/elasticmq/elasticmq-server-1.2.0.jar"]</code></pre>



<h3 class="wp-block-heading">Dockerのビルド</h3>



<p>　Dockerfileがあるディレクトリで、下記コマンドを叩きます。 [-t]はタグ名をつけるためのオプションなので、好きなものに変更して大丈夫です。</p>



<pre class="wp-block-code"><code>docker build -t sqs/elasticmq:1.2.0 .</code></pre>



<p>　これで、Dockerの構築が終わったので、次からAWS SQSのCLIで確認していきます。</p>



<h2 class="wp-block-heading">AWS SQSのコマンド実行</h2>



<h3 class="wp-block-heading">SQSの動作確認</h3>



<p>　まず下記コマンドを叩いてみましょう。もし何も表示されなければ動作しているので問題ありません。</p>



<pre class="wp-block-code"><code>aws sqs list-queues --endpoint-url http://localhost:9324</code></pre>



<p>　下記が表示されてしまっている場合は、AWSの公式を参考に[aws configure]で設定しましょう。</p>





<a rel="noopener" target="_blank" href="https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html" title="Configuring settings for the AWS CLI - AWS Command Line Interface" class="blogcard-wrap external-blogcard-wrap a-wrap cf"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://s.wordpress.com/mshots/v1/https%3A%2F%2Fdocs.aws.amazon.com%2Fja_jp%2Fcli%2Flatest%2Fuserguide%2Fcli-configure-quickstart.html?w=160&#038;h=90" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Configuring settings for the AWS CLI - AWS Command Line Interface</div><div class="blogcard-snippet external-blogcard-snippet">AWS CLI が AWS とやり取りするのに使用する設定を構成します。</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">docs.aws.amazon.com</div></div></div></div></a>




<pre class="wp-block-code"><code>You must specify a region. You can also configure your region by running "aws configure".</code></pre>



<h3 class="wp-block-heading">キューの作成</h3>



<p>　キューの作成は[<meta charset="utf-8">create-queue]で実行します。 キューの名前を[<meta charset="utf-8">&#8211;queue-name]で設定するので、好きなものをつけてください。今回は[queue]という名前を設定しています。</p>



<pre class="wp-block-code"><code>aws sqs create-queue --queue-name queue --endpoint-url http://localhost:9324</code></pre>



<p>　キューの作成に成功したら、QueueUrlが返されるので、こちらは後ほども使うので取っておきましょう。</p>



<pre class="wp-block-code"><code>{
    "QueueUrl": "http://localhost:9324/000000000000/queue"
}</code></pre>



<h3 class="wp-block-heading">メッセージの作成</h3>



<p>　メッセージの作成は[<meta charset="utf-8">send-message]コマンドで実行します。また、[<meta charset="utf-8">&#8211;queue-url]には先ほどキューを作成したときのURLを代入しましょう。</p>



<pre class="wp-block-code"><code>aws sqs send-message --queue-url http://localhost:9324/000000000000/queue --message-body "123456789012345" --endpoint-url http://localhost:9324</code></pre>



<p>　メッセージが作成されると下記のようなレスポンスが返ってきます</p>



<pre class="wp-block-code"><code>{
    "MD5OfMessageBody": "ad5222e305a2d133d529f038caa88e1a",
    "MessageId": "38a69463-55be-46ab-9e59-81357491a8df"
}</code></pre>



<h3 class="wp-block-heading">メッセージの取得</h3>



<p>　メッセージの取得には[<meta charset="utf-8">receive-message]コマンドを使います。</p>



<pre class="wp-block-code"><code>aws sqs receive-message --queue-url http://localhost:9324/000000000000/queue --endpoint-url http://localhost:9324</code></pre>



<p>　レスポンスは下記のようになります。メッセージは配列で返されます。また、<meta charset="utf-8">ReceiptHandleはメッセージを一意に特定するもので、メッセージの削除などにも利用されます。</p>



<pre class="wp-block-code"><code>{
    "Messages": &#91;
        {
            "MessageId": "38a69463-55be-46ab-9e59-81357491a8df",
            "ReceiptHandle": "38a69463-55be-46ab-9e59-81357491a8df#83dc385b-9cf9-4d39-8312-5881d683d40c",
            "MD5OfBody": "ad5222e305a2d133d529f038caa88e1a",
            "Body": "<meta charset="utf-8">123456789012345"
        }
    ]
}</code></pre>



<p>　<span class="marker">※SQSは可視性タイムアウトがデフォルトで30秒のため、一度取得したメッセージは30秒経つまで取得できないので、ご注意ください。</span></p>



<h3 class="wp-block-heading">メッセージの削除</h3>



<p>　メッセージの削除は[<meta charset="utf-8">delete-message]コマンドを使います。[<meta charset="utf-8">&#8211;receipt-handle]にメーセージの<meta charset="utf-8">ReceiptHandleを代入するとメッセージが削除できます。</p>



<pre class="wp-block-code"><code>aws sqs delete-message --queue-url http://localhost:9324/000000000000/queue --receipt-handle <meta charset="utf-8">38a69463-55be-46ab-9e59-81357491a8df#83dc385b-9cf9-4d39-8312-5881d683d40c --endpoint-url http://localhost:9324</code></pre>



<p>　削除後のレスポンスはありません。</p>



<h3 class="wp-block-heading">メッセージの可視性タイムアウトの変更</h3>



<p>　メッセージの可視性タイムアウトを変更したい場合は、[<meta charset="utf-8">change-message-visibility]コマンドを使います。[<meta charset="utf-8">&#8211;visibility-timeout]に変更する値をいれましょう。</p>



<pre class="wp-block-code"><code>aws sqs change-message-visibility --queue-url http://localhost:9324/000000000000/queue --visibility-timeout 0 --receipt-handle <meta charset="utf-8">38a69463-55be-46ab-9e59-81357491a8df#83dc385b-9cf9-4d39-8312-5881d683d40c --endpoint-url http://localhost:9324</code></pre>



<p>　削除後のレスポンスはありません。</p>



<h3 class="wp-block-heading">キューのURLを忘れた場合</h3>



<p>　キューのURLを忘れた場合は、[get-queue-url]を使います。[&#8211;queue-name]に知りたいキューの名前を入れます。</p>



<pre class="wp-block-code"><code>aws sqs get-queue-url --queue-name queue --endpoint-url http://localhost:9324</code></pre>



<p>　レスポンスは下記のようになります。</p>



<pre class="wp-block-code"><code>{
    "QueueUrl": "http://localhost:9324/000000000000/queue"
}</code></pre>



<h3 class="wp-block-heading">queue を消したい場合</h3>



<p>　キューを消したい場合は、[delete-queue]を使います。</p>



<pre class="wp-block-code"><code>aws sqs delete-queue --queue-url http://localhost:9324/000000000000/queue --endpoint-url http://localhost:9324</code></pre>



<p><meta charset="utf-8">　削除後のレスポンスはありません。</p>



<h3 class="wp-block-heading">queue の一覧を表示したい場合</h3>



<p>　キューの一覧を表示したい場合は、[list-queues]コマンドを使います。</p>



<pre class="wp-block-code"><code>aws sqs list-queues --endpoint-url http://localhost:9324</code></pre>



<p>　QueueUrlsの中に、配列でキューのURLの一覧が返されます。</p>



<pre class="wp-block-code"><code>{
    "QueueUrls": &#91;
        "http://localhost:9324/000000000000/queue"
    ]
}</code></pre>



<h3 class="wp-block-heading"><strong>キューのattributeを取得したい時</strong></h3>



<p>　キューのattributeを取得したいときは、[<meta charset="utf-8">get-queue-attributes]を使用します。</p>



<pre class="wp-block-code"><code>aws sqs get-queue-attributes --queue-url http://localhost:9324/000000000000/queue --attribute-names All --endpoint-url http://localhost:9324</code></pre>



<pre class="wp-block-code"><code>{
    "Attributes": {
        "VisibilityTimeout": "30",
        "DelaySeconds": "0",
        "ReceiveMessageWaitTimeSeconds": "0",
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesNotVisible": "1",
        "ApproximateNumberOfMessagesDelayed": "0",
        "CreatedTimestamp": "1629253476",
        "LastModifiedTimestamp": "1629253476",
        "QueueArn": "arn:aws:sqs:elasticmq:000000000000:queue"
    }
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://techgrowup.net/amazon-sqs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: techgrowup.net @ 2026-05-10 11:41:45 by W3 Total Cache
-->