Dockerコンテナ内の名前解決をプライベートIPアドレスにする

Docker Engine 24.0 Docker Compose 2.21 プライベートDNSを運用しているネットワークで、 Dockerコンテナからネットワーク内の別のホストに通信したいとき、 デフォルトではGoogle Public DNS8.8.8.8でホスト名がグローバルIPアドレスに解決されるため、 ISPやCloudflare Tunnelなどの外部を経由して通信することになり、非効率で危険な通信経路になります。 また、IPアドレスによるアクセス制限を設けている場合、 プライベートIPアドレスがソースとなる通信になるように通信経路を制御したい場合があります。 以下の設定により、コンテナ内でexample.comが192.168.0.50に解決されるようになります。 Docker sudo docker run --rm --add-host "example.com:192.168.0.50" hello-world Managing /etc/hosts - Docker run reference | Docker Docs Docker Compose services: app: image: hello-world extra_hosts: - "example.com:192.168.0.50" extra_hosts - Services top-level element | Docker Docs

2023年12月9日 · aoirint

Docker Registryをホストしてhtpasswdで認証する

registry - Official Image | Docker Hub Native basic auth - Restricting access - Deploy a registry server | CNCF Distribution htpasswdファイルの作成 htpasswdファイルを作成します。 registryイメージ(Distribution Registry)はbcrypt形式のパスワードのみサポートしているため、パスワードをbcrypt形式でハッシュ化する必要があります。 sudo apt install apache2-utils mkdir auth cd auth htpasswd -cB htpasswd myuser 永続化ディレクトリの作成 mkdir data docker-compose.ymlファイルの作成 services: registry: image: registry:2 restart: always environment: REGISTRY_AUTH: htpasswd REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm # ports: # - "0.0.0.0:5000:5000" volumes: - ./data:/var/lib/registry - ./auth:/auth コンテナの実行 sudo docker compose up -d コンテナのTCP 5000番ポートでDocker Registry HTTP APIがリッスンします。 このポート宛にCloudflaredやリバースプロキシを設定して、https://docker.example.comのようにサービスを公開します。 ...

2023年12月9日 · aoirint

Dockerコンテナのメモリ使用量を制限する

Docker Engine 24.0 Docker Compose 2.21 Docker # 100 MB sudo docker update --memory "100m" "$CONTAINER_ID" # 1 GB sudo docker update --memory "1g" "$CONTAINER_ID" # 制限を解除 sudo docker update --memory "0" "$CONTAINER_ID" Docker Compose app: image: hello-world deploy: resources: limits: memory: '1g' 参考 Update the limitation of memory/CPU for existing container in docker - Stack Overflow Runtime options with Memory, CPUs, and GPUs | Docker Docs Compose Deploy Specification | Docker Docs

2023年12月9日 · aoirint

DockerコンテナのCPU使用量を制限する

Docker Engine 24.0 Docker Compose 2.21 Docker # CPU 1個 sudo docker update --cpus "1" "$CONTAINER_ID" # CPU 0.01個(最小) sudo docker update --cpus "0.01" "$CONTAINER_ID" # 制限を解除 sudo docker update --cpus "0" "$CONTAINER_ID" Docker Compose app: image: hello-world deploy: resources: limits: cpus: '0.1' 参考 Update the limitation of memory/CPU for existing container in docker - Stack Overflow Runtime options with Memory, CPUs, and GPUs | Docker Docs Compose Deploy Specification | Docker Docs

2023年12月9日 · aoirint

自宅サーバのDocker NetworkのIPアドレスプールが枯渇した

https://twitter.com/aoirint/status/1617993934625722368 弊自宅サーバでは、サーバソフトウェアや定期実行プログラムのデプロイに、基本的にDocker Composeを使用しています。 先日、新しいソフトウェアをDocker Composeでデプロイしようとして、以下のエラーが発生しました。 Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network Docker Networkが使用するIPアドレスプールが枯渇し、新しいIPアドレスブロックを確保できなかったという内容のエラーです。 環境 Ubuntu 20.04 $ docker -v Docker version 20.10.23, build 7155243 $ docker compose version Docker Compose version v2.15.1 原因 このエラーは、30個程度のDocker Networkを作成すると発生するようです。 Docker Composeでは通常、プロジェクト1つあたり、1つのDocker Networkが作成されます(複雑なプロジェクトでは、2つ以上使用することがあるかもしれません)。 手元の環境では、Docker Composeのプロジェクトが27個稼働しており、28個目のプロジェクトを起動したときにエラーが発生しました。 Docker Networkの一覧は、以下のコマンドで確認できます。 docker network ls Docker NetworkのIPアドレスには、いわゆるプライベートIPアドレスの範囲が使用されます。 プライベートIPアドレスの範囲は、以下のようになります(CIDR表記)。 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 作成されたDocker Networkに割り当てられたIPレンジはdocker network inspectコマンドで確認できます。 以下の記事で紹介されているコマンドが便利です。 ...

2023年1月26日 · aoirint

Mastodon on Docker Composeで全文検索エンジンElasticsearchを有効化する

Mastodon on Docker Composeを立てる記事: https://blog.aoirint.com/entry/2020/mastodon_docker/ 上の記事では、諸般の事情(主にスペック不足)によりElasticsearchを無効化した状態でMastodonインスタンスを立てていた。 2021年のElasticsearchのライセンス変更 クラウド事業者によるマネージドサービス提供における、オープンソースコミュニティへのコントリビューションの不足等を背景として、 2021年のバージョン7.11リリース以降、ElasticsearchのライセンスはApache 2.0から独自のElastic License 2.0(およびServer Side Public Licenseのデュアルライセンス)に変更された。 https://www.elastic.co/jp/pricing/faq/licensing この記事では、Elastic License 2.0によってライセンスされる公式のElasticsearchの配布パッケージを使用する。 Elastic License 2.0の条文: https://www.elastic.co/licensing/elastic-license ちなみにElastic License 2.0は、Elasticsearchをマネージドサービスとして提供することを禁止しているが、SaaSアプリケーションのバックエンドとして使用すること(Elasticsearch APIへの直接アクセスを提供しないサービス提供)に影響しないという立場が示されている。 Elasticsearchをバックエンドで使用するSaaSアプリを開発しているけど、どんな影響が生じる? 今回のソースコードのライセンス変更はお客様に一切影響しません。Elastic Licenseに基づいて、デフォルトの配布パッケージを使用できるほか、このパッケージをベースに無料でアプリケーションを開発することもできます。Elastic Licenseはsource-available license(ソース利用許諾)であり、コピーレフトの側面を持たず、デフォルトの機能を無料とします。具体的な例として、よろしければMagentoプロジェクトに関する質問への回答をご参照ください。 https://www.elastic.co/jp/pricing/faq/licensing#elasticsearch%E3%82%92%E3%83%90%E3%83%83%E3%82%AF%E3%82%A8%E3%83%B3%E3%83%89%E3%81%A7%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8Bsaas%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E9%96%8B%E7%99%BA%E3%81%97%E3%81%A6%E3%81%84%E3%82%8B%E3%81%91%E3%81%A9%E3%80%81%E3%81%A9%E3%82%93%E3%81%AA%E5%BD%B1%E9%9F%BF%E3%81%8C%E7%94%9F%E3%81%98%E3%82%8B%EF%BC%9F I’m using Elasticsearch to put a search box on my cat-picture SaaS product. This is permitted under ELv2. Meow! I am a Managed Service Provider (MSP) running Elasticsearch and Kibana for my customers. If your customers do not access Elasticsearch and Kibana, this is permitted under ELv2. If your customers do have access to substantial portions of the functionality of either Elasticsearch and Kibana as part of your service, this may not be permitted. ...

2022年11月23日 · aoirint

docker-composeでBuildKitを使う

ビルドコマンド https://stackoverflow.com/questions/58592259/how-do-you-enable-buildkit-with-docker-compose DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose build Dockerfile https://www.docker.com/blog/introduction-to-heredocs-in-dockerfiles/ # syntax=docker/dockerfile:1.3-labs

2021年11月8日 · aoirint

Mastodonをdocker-composeで立てる(Ubuntu 18.04)

tootsuite/mastodon: Your self-hosted, globally interconnected microblogging community Mastodon documentation 内容はコミットID44d5c6bc8ffd92cd201380dabe35748e50b6af68、Mastodon Dockerイメージバージョンv3.2.1(Digest:sha256:41cd5fb48d8b15ec806f08ab06fec98df33ec9b83a1f879e0fb30da9994018dc)におけるもの。docker-composeの設定ファイルバージョンは3。 $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.5 LTS Release: 18.04 Codename: bionic $ uname -r 5.4.0-56-generic $ docker -v Docker version 19.03.14, build 5eb3275d40 $ docker-compose -v docker-compose version 1.27.1, build 509cfb99 $ docker images tootsuite/mastodon --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE tootsuite/mastodon v3.2.1 sha256:41cd5fb48d8b15ec806f08ab06fec98df33ec9b83a1f879e0fb30da9994018dc 37ca50fc92bd 6 weeks ago 1.86GB 今回はDocker Hub上のイメージを使用し、ローカルビルドをしない想定でいく(ごちゃごちゃするので)。 Mastodonを改造したい場合など、必要に応じてgithub:tootsuite/mastodonをFork/Cloneし、自分で/CIでビルドして信頼できるDockerレジストリに登録すればいいと思う。 ...

2020年12月6日 · aoirint

Docker + Docker Compose on Vagrant Ubuntu Server

Downloads – Oracle VM VirtualBox Downloads | Vagrant by HashiCorp Vagrant box ubuntu/bionic64 - Vagrant Cloud Install Docker Engine on Ubuntu | Docker Documentation Install Docker Compose | Docker Documentation kvm (qemu)を使うこともできるらしいが、対応したboxを用意する必要があるのでVirtualBoxを使う。 KVM用仮想マシンをVagrantで手軽に作る | さくらのナレッジ Commands # 起動 vagrant up # 削除 vagrant destroy # ssh vagrant ssh # 停止(シャットダウン) vagrant halt Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" config.vm.hostname = "ubuntu-docker" # config.vm.network "forwarded_port", guest: 80, host: 8080 # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" config.vm.provider "virtualbox" do |vb| vb.name = "ubuntu-docker" vb.memory = "1024" # vb.gui = true end config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - add-apt-repository -y \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" apt-get update apt-get install -y \ docker-ce \ docker-ce-cli \ containerd.io curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose adduser vagrant docker SHELL end

2020年10月22日 · aoirint

docker-composeによるPython + Selenium環境

2022-08-06 追記 Seleniumのバージョンが上がって一部非推奨化したりしたので、そちらに対応した版を作成しました。 リポジトリ: https://github.com/aoirint/compose-selenium-python-template 2020-09-28 docker-compose.yml version: '3.8' services: app: build: ./app/ entrypoint: [ "wait-for-it", "selenium:4444", "--", "python3", "/code/main.py" ] volumes: - ./work:/work environment: SELENIUM_URL: http://selenium:4444/wd/hub depends_on: - selenium selenium: image: selenium/standalone-chrome volumes: - /dev/shm:/dev/shm app/Dockerfile FROM python:3 WORKDIR /work RUN apt update && apt install -y \ wait-for-it ADD requirements.txt /tmp/ RUN pip3 install -r /tmp/requirements.txt ADD code/ /code app/requirements.txt requests >= 2.24.0 selenium app/code/main.py from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.desired_capabilities import DesiredCapabilities selenium_url = os.environ['SELENIUM_URL'] website_url: str = None driver = webdriver.Remote( command_executor=selenium_url, desired_capabilities=DesiredCapabilities.CHROME, ) driver.get(website_url)

2020年9月28日 · aoirint