SZPP
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Docker 入門 ## もくじ ## 1. Docker って何? > Docker は、アプリケーションをすばやく構築、テスト、デプロイできるソフトウェアプラットフォームです。 - [AWS](https://aws.amazon.com/jp/docker/) より引用 らしいです。 あとマスコットキャラクターが可愛いです。 ![](https://i.imgur.com/IIxD4AC.png) 可愛いね ## 2. Hyper-V とかとの違い よく仮想化技術なんて言われ方をしていますが、では Hyper-V(WSL2) とはどのように異なるのでしょうか?それは以下の図を見れば一目瞭然です。 <div style="text-align: center"> <img src="https://i.imgur.com/ZyCSKgG.png"> <p> (出典: https://www.itmedia.co.jp/enterprise/articles/1612/19/news041.html) </p> </div> このように、Hyper-V なんかはカーネル(OS)単位で仮想化がされますが、Docker では1つの OS 上で複数のコンテナが動作します。これにより、1つの1つのコンテナのサイズが軽く、起動・停止等も高速にすることができるようになります。Hyper-V も仮装環境としては確実ですが、これはサイズが大きくなってしまい、また起動・停止も時間を要するので Docker を使うことが多いのです。 ## 2. Docker で何ができるの? 大きく分けて(私の経験上)以下の3つです 1. Ubuntu や Arch Linux などの OS の仮装環境を立て、その OS 上でのアプリの挙動を確かめることができる 2. devcontainer で開発環境を一発で立てることができる(1の応用) 3. docker-compose で複数のコンテナ(サービス)から成るシステムを一発で立ち上げることができる 今回はこれら3つを実際に体験し、Docker の知識を深めてみましょう。 ## 3. 演習 ### (0) Docker のインストール(for ubuntu) インストールされてるか確認 ```console $ docker run hello-world ``` されてなかったら https://hackmd.io/@szpp/rkM66Dg5d#1-Docker ### (1) Alpine Linux を docker で動かしてみよう #### 1 Alpine Linux とは?  Linux ディストリビューション(Ubuntu とか Arch Linux とか)の1つで、めちゃくちゃ軽量であることが特徴です。Ubuntu が 700MB であるのに対して Alpine Linux はたったの 100MB で、コンテナのサイズも軽くすることが多いので Linux のコンテナを作りたいときは使われることが結構多いです。 #### 2. alpine linux の立ち上げ ```console $ docker run -it alpine:3.14 sh ``` - docker ... docker のコマンド - run ... 新しいコンテナを実行する - -i ... ホストの入力をコンテナの標準出力をつなげる => キーボードから入力したときにそれがコンテナ上の画面に表示されるようになる - -t ... コンテナの標準出力とホストの出力をつなげる => コンテナの出力が見れるようになる - sh ... コンテナが起動したときに実行するコマンド。今回はシェルを起動したいので `sh` #### 3. コンテナの OS 情報確認 ```console $ uname -a Linux c08f93fba5c8 5.10.47-linuxkit #1 SMP PREEMPT Sat Jul 3 21:50:16 UTC 2021 x86_64 Linux ``` ### (2) szpp-todo-app-sample の開発環境を Docker で整えてみよう #### 0-1. szpp-todo-app-sample の clone ```console $ git clone https://gitlab.com/arumakan1727/szpp-todo-app-sample.git $ code szpp-todo-app-sample ``` #### 0-2. Remote Development 拡張機能のインストール ![](https://i.imgur.com/x0veNyz.png) #### 1. szpp-todo-app-sample の開発において必要なもの - go - node.js - npm - yarn 1からやる人だと開発環境構築に1時間ぐらいかかりそうなので docker で整えてあげましょう。 #### 2. devcontainer とは VScode の標準機能で devcontainer というものがあります。devcontainer は docker のコンテナを作成してそこに VScode のサーバーをインストールし、コンテナ上で VScode を動かすことができるよーというものです。 これは以下の点でかなり嬉しいサービスです。 - コンテナ上で VScode を動かせる - ローカルの ssh の設定がコンテナ上に引き継がれる - VScode の拡張機能までインストールさせることができる 今回はこれを使って開発環境を構築していきます。 #### 3. `.devcontainer` の準備 devcontainer はプロジェクト内に `.devcontainer/` があれば動かすことができます。`.devcontainer/` は `Dockerfile` と `devcontainer.json` の2つのファイルで構成されるので、それぞれを準備していきましょう。 ##### 3-1. Dockerfile Dockerfile は Docker のイメージの内容を記述するためのファイルです。 Docker には Ubuntu とか Alpine とか大元となる OS のイメージは転がっていますが、あくまでそれらはバニラの OS に過ぎないので、Go や Node.js など自分が欲しいアプリケーションは自分で用意しなければなりません。そんなときに使うのがこの Dockerfile ということです。 では、Dockerfile を記述してみましょう。...と言いたいところですが、一つ一つ説明していると時間がなくなってしまうので以下の Dockerfile を `.devcontainer/Dockerfile` にコピペしてください。 ```dockerfile= FROM ubuntu:20.04 ENV DEBIAN_FRONTEND=noninteractive # 色々なアプリケーションのインストール RUN apt-get update && \ apt-get install -y \ build-essential \ wget \ tar \ git && \ apt-get clean && rm -rf /var/lib/apt/lists/* # タイムゾーンの設定 RUN apt-get update && \ apt-get install -y tzdata && \ apt-get clean && rm -rf /var/lib/apt/lists/* ENV TZ Asia/Tokyo ## Go のインストール RUN \ wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz && \ tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz ## node.js + npm のインストール RUN \ apt-get update && \ apt-get install -y nodejs npm && \ npm install n -g && \ n stable && \ apt purge -y nodejs npm && \ apt-get clean && rm -rf /var/lib/apt/lists/* ## yarn のインストール RUN npm install yarn -g RUN useradd -m -s /bin/bash -U vscode USER vscode ENV PATH /usr/local/go/bin:$PATH ``` 以下に簡単な説明を置いておきます。 - `FROM [イメージ]:[タグ]` ... ベースとなるイメージ:タグ - `ENV [key]=[value]` ... 環境変数(export よりもこっちの方がいい) - `RUN [command]` ... **イメージ作成時(docker build)** に実行するコマンド - `USER [user]` ... ユーザーを切り替える - `ENTRYPOINT [command]` ... **コンテナ起動時(docker run)** に実行するコマンド ##### 3-2. devcontainer.json devcontainer に関するあれこれを設定するファイルです。よくいじるフィールドは `extensions` と `remoteUser` の2つだと思いますが、今回は `extensions` だけ扱います。 `extensions` は VScode の拡張機能を設定するフィールドです。今回の devcontainer で使いたい拡張機能は以下の通りです。 - `golang.go` ... Golang - `johnsoncodehk.volar` ... Vue.js - `mosapride.zenkaku` ... 全角が強調される これらを `extensions` にセットすればコンテナ上の VScode で利用可能となります。 ```json "extensions": [ "golang.go", "johnsoncodehk.volar", "mosapride.zenkaku" ], ``` #### 4. devcontainer の起動 これで準備は整いました。左下の `><` みたいなボタンを押して `Reopen in Container` を選択すれば devcontainer が起動します。(結構時間がかかるかも) #### 5. 開発できる環境であるかを確認 **各種コマンド** ```console $ go version go version go1.17.1 linux/amd64 $ npm -v 6.14.15 $ yarn -v v14.17.6 ``` **拡張機能** go や vue のファイルで補完が出るか(go だと install/update tools しないとかも) ### (3) szpp-todo-app-sample を docker-compose で一発で立ち上げてみよう。 #### docker-compose.yaml ```yaml= version: "3" services: backend: build: context: back-end dockerfile: Dockerfile restart: always container_name: todo-app-backend environment: - HOGE=hogehoge ports: - "8080:8080" frontend: build: context: front-end dockerfile: Dockerfile container_name: todo-app-frontend depends_on: - backend ports: - "5000:5000" ``` 一応 docker-compose.yaml の本質はこれです。  #### front-end/Dockerfile ```dockerfile= FROM node:16 WORKDIR /workdir COPY yarn.lock tsconfig.json package.json /workdir/ RUN yarn global add serve RUN yarn install --no-progress COPY public /workdir/public COPY src /workdir/src COPY index.html /workdir/ COPY vite.config.ts /workdir/ RUN yarn build ENTRYPOINT [ "serve", "-s", "dist" ] ``` #### back-end/Dockerfile ```dockerfile= FROM golang:1.17 AS build WORKDIR /work COPY . . RUN \ CGO_ENABLED=0 \ GOOS=linux \ go build -o app FROM ubuntu:20.04 WORKDIR /work RUN \ apt-get update && \ apt install -y tzdata && \ apt-get clean && rm -rf /var/lib/apt/lists/* ENV TZ Asia/Tokyo RUN \ apt-get update && \ apt install -y ca-certificates && \ apt-get clean && rm -rf /var/lib/apt/lists/* COPY --from=build /work/app app ENTRYPOINT [ "./app" ] ``` #### ビルド & 起動 ```console $ docker-compose up -d --build ``` #### 確認 ##### front-end http://localhost:5000 ##### back-end ```console $ curl localhost:8080/api/todo ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully