good-arrow’s blog

https://good-arrow.net/

Wordpress を Docker Comporse で立てる for Ubuntu

やること

https://docs.docker.jp/compose/wordpress.html


Docker Engine のインストール

Install Docker Engine on Ubuntu | Docker Documentation

sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get -y update
sudo apt-get -y install ca-certificates curl gnupg lsb-release

以下を1行で実行(GPGキーの追加)

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

同じく以下を1行で実行(リポジトリの追加)

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Docker Engine のインストール

sudo apt-get -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io

Docker Engine の動作テスト

sudo docker run hello-world

実行結果

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/



Docker Comporse のインストール

https://docs.docker.jp/compose/install.html#linux

sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose



Wordpress イメージの取得

https://docs.docker.jp/compose/wordpress.html

Wordpress用のディレクトリを作成

sudo mkdir wp
cd wp

wp 内に docker-compose.yaml をファイル作成し、下記を記述する。

sudo vi docker-compose.yaml


パスワード等は任意の値に変更する。

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:



Wordpress の Docker Compose を取得

sudo docker-compose up -d

ブラウザで http://IPアドレス:8000 にアクセスする。

以上。


Docker Compose のシャットダウン

sudo docker-compose down



イメージの削除

sudo docker-compose down --volumes