Ubuntu22.04にdockerをインストール

1. Set up Docker’s apt repository.
# Add Docker’s official GPG key:
$ sudo apt-get update
$ sudo apt-get install ca-certificates curl gnupg
$ sudo install -m 0755 -d /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /etc/apt/keyrings/docker.gpg
$ sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
$ echo \
“deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo “$VERSION_CODENAME”) stable” | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update

2. Install the Docker packages.
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
$ sudo docker -v
Docker version 24.0.7, build afdd53b
$ sudo docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
478afc919002: Pull complete
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Status: Downloaded newer image for hello-world:latest

Hello from Docker!

[docker] amazonlinux2でPHP8.1を動かしたい

Dockerfile

FROM amazonlinux:2

RUN yum update -y

RUN amazon-linux-extras install -y epel
RUN yum -y install \
http://rpms.remirepo.net/enterprise/remi-release-7.rpm

RUN yum -y install \
    pcre-devel \
    php81 \
    php81-php-cli \
    php81-php-common \
    php81-php-devel \
    php81-php-gd \
    php81-php-intl \
    php81-php-mbstring \
    php81-php-mysqlnd \
    php81-php-pear \
    php81-php-pecl-apcu \
    php81-php-process \
    php81-php-opcache \
    php81-php-redis \
    php81-php-soap \
    php81-php-pecl-xdebug3 \
    php81-php-xml \
    php81-php-zip

 RUN alternatives --install /usr/bin/php php /usr/bin/php81 1

$ sudo docker build . -t amzn2php8.1
extrasではなくepelでremiでインストールする。
なるほどー

docker-compose up -dとdown –rmi all

$ docker-compose up -d
docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:80"

http://192.168.56.10:8080/
$ sudo docker images;
dockerhttpd_web latest 71323a09a84b 2 minutes ago 56.9MB
$ sudo docker-compose down
$ sudo docker images;
dockerhttpd_web latest 71323a09a84b 4 minutes ago 56.9MB
-> docker-compose downだけだと、imageは残ったまま

$ sudo docker-compose up -d
$ sudo docker-compose down –rmi all
$ sudo docker images;

imageも削除される
なるほど、概要は理解した

[Docker] CodeBuildを使いたい

$ tree
.
├── Dockerfile
└── index.html

0 directories, 2 files

$ sudo docker build -t myapache .
$ sudo docker run -dit –name myapp -p 8080:80 myapache

buildspec.yml

version: 0.2

env:
  variables:
    AWS_REGION_NAME: ap-northeast-1
    ECR_REPOSITORY_NAME: hoge
    DOCKER_BUILDKIT: "1"

phases:
  install:
    runtime-versions:
      docker: 19

  pre_build:
    commands:
      - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
      - aws ecr --region ap-northeast-1 get-login-password | docker login --username AWS --password-stdin https://${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hpscript
      - REPOSITORY_URI=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION_NAME}.amazonaws.com/${ECR_REPOSITORY_NAME}
      - IMAGE_TAG=$(echo ${CODEBUILD_RESOLVED_SOURCE_VERSION} | cut -c 1-7)
  
  build:
    commands:
      - docker image build -t ${REPOSITORY_URI}:${IMAGE_TAG} .
  post_build:
    commands:
      - docker image push ${REPOSITORY_URI}:${IMAGE_TAG}  

### AWS CodeBuild
– Project configuration
Project name: test
Build Badge: Enable build badge

– Source
Source provider: AWS CodeCommit
Repository: 指定
Reference type: Branch
Branch: 指定

– Environment
Environment image: Managed image
Operationg system: Amazon Linux2
Runtime(s): Standard
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
Image version: Always use the latest image for this runtime
Environment type: Linux
Privileged: on
Service Role: New service role
Additional configuration
Timeout: 1 Hour 0 Minutes
Queued timeout: 8 Hour 0 Minutes
Certificate: Do not install any certificate
Compute: 3 GB memory, 2 vCPUs

– Buildspec
Use a buildspec file

– Artifact
Type: No artifacts

– Log
CloudWatch logs: CloudWatch logs

OK ここまでは出来た。次はcode deploy, codepipelineか。

[docker] nginxをリバースプロキシ

$ sudo docker run –rm -v /home/vagrant/dev/docker/basic:/tmp -it nginx /bin/bash
$ cp /etc/nginx/conf.d/default.conf /tmp/

$ sudo docker build -t rproxy-image .
$ sudo docker build -t web-image .

$ sudo docker network create web-network
$ sudo docker run –name=web-container –net=web-network -d web-image
$ sudo docker run –name=rproxy-container –net=web-network -p 8080:80 -d rproxy-image

うむ、OK

[docker] docker-compose.yml その2

docker-compose.yml

version: "2"
services:
  db-container:
    image: busybox
    volumes:
      - db-volume: /var/lib/mysql/

  mysql-container:
    image: mysql:8
    environment:
      - MYSQL_ROOT_PASSWORD=dbpass01
      - MYSQL_DATABASE=redmine
    command: --character-set-server-utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
    volumes_from:
      - db-container
    dpends_on:
      - db-container
    restart: always

  pma-container:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_HOST=mysql-container
      - PMA_USER=root
      - PMA_PASSWORD=dbpass01
    ports:
      - 8080:80
    depends_on:
      - mysql-container
    restart: always
  wp-container:
    image: wordpress
    environent:
      - WORDPRESS_DB_HOST=mysql-container
      - WORDPRESS_DB_USER=root
      - WORDPRESS_DB_PASSWORD=dbpass01
    ports:
      - 8080:80
    depends_on:
      - mysql-container
    restart: always

  rm-container:
    image: redmine
    environment:
      - REDMINE_DB_MYSQL=mysql-container
      - REDMINE_DB_USERNAME=root
      - REDMINE_DB_PASSWORD=dbpass01
    ports:
      - 3000:3000
    depends_on:
      - mysql-container
    restart: always

volumes:
  db-volume:

[docker] docker-composeとapache

$ pwd
/home/vagrant/dev/docker/basic

$ sudo docker run –rm -v /home/vagrant/dev/docker/basic/:/tmp/ -it httpd /bin/bash
$ cp /usr/local/apache2/conf/httpd.conf /tmp/

httpd.conf

    # CustomLog "logs/access_log" combined
    CustomLog "logs/access_log" combined
</IfModule>

$ echo “Hello World!” > index.html

docker-compose.yml

version: "2"
services:
  web-container:
    image: httpd
    volumes:
      - ./index.html:/usr/local/apache2/htdocs/index.html
      - ./httpd.conf:/usr/local/apache2/conf/httpd.conf
    ports:
      - 8080:80
    expose:
      - 80

バックグラウンドで指定する場合は -d をつける
$ sudo docker-compose up -d
http://192.168.56.10:8080/

$ sudo docker-compose exec web-container /bin/bash
# more /usr/local/apache2/logs/access_log
$ sudo docker-compose stop
$ sudo docker-compose rm

docker-comoseの停止や削除はdocker-compose.ymlで指示している
docker-composeでdockerfileで起動する場合は image: httpd としていたところを build: ./rproxy などとする

なるほど、buildとimageのところやdocker-compose.ymlが何を見ているかなどが明確になった。

[docker] docker-composeを始める

version: "2"
services:
  ub-test:
    image: ubuntu

$ sudo docker-compose up
$ sudo docker compose logs
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d08f238d9f1 ubuntu “bash” About a minute ago Exited (0) About a minute ago basic_ub-test_1

version: "2"
services:
  ub-test:
    image: ubuntu
    tty: true
    stdin_open: true

    command: /bin/bash

$ sudo docker-compose run ub-test
root@dfbf7a227bd6:/# exit

[Docker] MySQLのコンテナ構築

$ sudo docker network create mysql-nw

データボリュームコンテナを作成する
データディレクトリは/var/lib/mysql/ とする
$ sudo docker run -v db-volume:/var/lib/mysql/ –name=db-container busybox

mysql/Dockerfile

FROM mysql:8

ENV MYSQL_ROOT_PASSWORD=dbpass01

CMD ["mysqld", \
"--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci",\
"--default-authentication-plugin=mysql_native_password"]

$ sudo docker build -t mysql-image .
※DockerfileのポートはEXPOSEで3306

$ sudo docker run –volumes-from=db-container –name=mysql-container –net=mysql-nw -d mysql-image
$ sudo docker exec -it mysql-container /bin/bash
root@ae32b7c17975:/# mysql -uroot -pdbpass01
mysql> status
————–
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server – GPL)

$ sudo docker run –rm –volumes-from db-container -it ubuntu /bin/bash
root@6a77206db029:/# ls -l /var/lib/mysql/

Dockerfile

#phpmyadminのイメージ取得
FROM phpmyadmin/phpmyadmin

ENV PMA_HOST=mysql-container \
	PMA_USER=root \
	PMA_PASSWORD=dbpass01

$ sudo docker build -t pma-image .
$ sudo docker run –net=mysql-nw –name=pma-container -p 8080:80 -d pma-image

[Docker] nginxをリバースプロキシにしたApache

中継役としてリバースプロキシが間に立ってリクエストを受け、Webサーバにデータを取りに行く仕組み
Webサーバに接続できるのがリバースプロキシに限定できることから、セキュリティ上の理由でよく使われる
nginxは大量アクセスを処理するのに強みがある
nginxはhtmlなど軽量なリクエストの処理に対応させ、PHPやデータベース処理などはApacheにリクエストを回して処理をする

$ sudo docker pull nginx
$ sudo docker run –rm -p 8080:80 -d nginx
$ sudo docker stop sad_edison

### nginxをリバースプロキシとしたApacheを作る
ホストOS側でポートを利用できるのは1つのポートにつき1つのコンテナのみ
nginxにホストOSの80番ポートを割り当てる

$ tree
.
├── rproxy
│   ├── Dockerfile
│   └── default.conf
└── web
├── Dockerfile
└── index.html

nginxには幾つかconfファイルがある
nginxのプロキシ部分のconfファイルは /etc/nginx/conf.d/default.conf

$ sudo docker run –rm -v /home/vagrant/dev/docker/basic/rproxy:/tmp -it nginx /bin/bash
$ cp /etc/nginx/conf.d/default.conf /tmp/
$ ls rproxy/
Dockerfile default.conf

rproxy/default.conf

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://web-container/;
    }

$ sudo docker build -t rproxy-image .

### httpdのDockerfile作成
$ echo “hello world” > index.html

Dockerfile

FROM httpd

COPY ./index.html /usr/local/apache2/htdocs/

EXPOSE 80

$ sudo docker build -t web-image .

### Docker上でnetworkを作成する
$ sudo docker network create web-network
$ sudo docker run –name=rproxy-container –net=web-network -p 8080:80 -d rproxy-image

なるほどー