LoginSignup
22
17

More than 3 years have passed since last update.

Docker上のAmazonLinux2でLaravelの環境構築

Last updated at Posted at 2019-06-17

はじめに

 Docker for MacでAWS向けのディストリビューションであるAmazonlinux2を動かして、そこでPHPのフレームワークであるLaravelの環境を構築する手順です。
 Docker for Macのインストールは終わっているという前提で進めます。

手順

1. ローカルでの作業

Amazon Linux2のイメージのダウンロード

 docker pullコマンドでamazonlinux:2のイメージをローカルにダウンロードします。
 docker imagesコマンドでローカルにあるイメージ一覧を表示すると、amazonlinux:2がダウンロードされていることが分かります。

% docker pull amazonlinux:2
2: Pulling from library/amazonlinux
Digest: sha256:730fae68c6a180e8006443b0f090e56419da5ceb5d11250d96e627660ffbc674
Status: Downloaded newer image for amazonlinux:2

% docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
amazonlinux         2                   b94321659aca        3 weeks ago         162MB

イメージからコンテナの立ち上げ

 docker runコマンドで、amazonlinux:2イメージ上にtest_amazonlinuxコンテナを立ち上げます。
 docker psコマンドで実行中のコンテナを表示すると、立ち上がっていることが分かります。

% docker run -it -d --privileged --name test_amazonlinux -p 8081:80 amazonlinux:2 /bin/bash
c79c8f630da9f1b6457a1118f20675a3b98d8ae1359f7a27e312be3a8318b1d8

% docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                  NAMES
c79c8f630da9        amazonlinux:2        "/bin/bash"         17 seconds ago      Up 16 seconds       0.0.0.0:8081->80/tcp   test_amazonlinux

 以下がコマンドのオプションの意味です。

  • -it : コンテナ内でインタラクティブなbashシェルを作成するオプション
    • -i : コンテナ内で操作できるようにするオプション
    • -t : tty(端末)を割り当てるオプション
  • -d : コンテナをバックグラウンドで実行するオプション
  • -privileged : コンテナに対して全ての能力を与えるオプション(Dockerの中でDockerを動かすとかもできるらしい)
  • --name : コンテナに名前を割り当てるオプション
  • -p : コンテナのポートを公開するオプション(外部からアクセスするポート番号:コンテナ側のポート番号)

コンテナに入る

 最後に以下のdocker execコマンドでtest_amazonlinuxコンテナに新しいbashセッションを作成しコンテナ内に入ります。

$ docker exec -it test_amazonlinux /bin/bash

2. コンテナ内での作業

 rootユーザで作業を進めていきます。

諸々のインストール

bash-4.2# cat /etc/system-release
Amazon Linux release 2 (Karoo)

bash-4.2# yum update -y
Loaded plugins: ovl, priorities
amzn2-core                                                                                                                                                 | 2.4 kB  00:00:00
(1/3): amzn2-core/2/x86_64/group_gz                                                                                                                        | 2.4 kB  00:00:00
(2/3): amzn2-core/2/x86_64/updateinfo                                                                                                                      | 115 kB  00:00:00
(3/3): amzn2-core/2/x86_64/primary_db
.....(中略).....
Updated:
  amazon-linux-extras.noarch 0:1.6.8-1.amzn2

Complete!

bash-4.2# yum install -y vim systemd yum-utils unzip wget sudo
Loaded plugins: ovl, priorities
Resolving Dependencies
.....(中略).....

Installed:
  sudo.x86_64 0:1.8.19p2-14.amzn2        systemd.x86_64 0:219-57.amzn2.0.9 unzip.x86_64 0:6.0-19.amzn2 vim-enhanced.x86_64 2:7.4.160-4.amzn2.0.16 wget.x86_64 0:1.14-18.amzn2.1
  yum-utils.noarch 0:1.1.31-46.amzn2.0.1

Dependency Installed:
  acl.x86_64 0:2.2.51-14.amzn2                               audit-libs.x86_64 0:2.8.1-3.amzn2.1                        cracklib.x86_64 0:2.9.0-11.amzn2.0.2
  cracklib-dicts.x86_64 0:2.9.0-11.amzn2.0.2                 cryptsetup-libs.x86_64 0:1.7.4-4.amzn2                     dbus.x86_64 1:1.10.24-7.amzn2
  dbus-libs.x86_64 1:1.10.24-7.amzn2                         device-mapper.x86_64 7:1.02.146-4.amzn2.0.2                device-mapper-libs.x86_64 7:1.02.146-4.amzn2.0.2
  elfutils-default-yama-scope.noarch 0:0.170-4.amzn2         elfutils-libs.x86_64 0:0.170-4.amzn2                       gpm-libs.x86_64 0:1.20.7-15.amzn2.0.2
  groff-base.x86_64 0:1.22.2-8.amzn2.0.2                     gzip.x86_64 0:1.5-10.amzn2                                 kmod.x86_64 0:25-3.amzn2.0.2
  kmod-libs.x86_64 0:25-3.amzn2.0.2                          libcap-ng.x86_64 0:0.7.5-4.amzn2.0.4                       libfdisk.x86_64 0:2.30.2-2.amzn2.0.4
  libidn.x86_64 0:1.28-4.amzn2.0.2                           libpwquality.x86_64 0:1.2.3-5.amzn2                        libsemanage.x86_64 0:2.5-11.amzn2
  libsmartcols.x86_64 0:2.30.2-2.amzn2.0.4                   libutempter.x86_64 0:1.1.6-4.amzn2.0.2                     libxml2-python.x86_64 0:2.9.1-6.amzn2.3.2
  lz4.x86_64 0:1.7.5-2.amzn2.0.1                             pam.x86_64 0:1.1.8-22.amzn2                                perl.x86_64 4:5.16.3-294.amzn2
  perl-Carp.noarch 0:1.26-244.amzn2                          perl-Encode.x86_64 0:2.51-7.amzn2.0.2                      perl-Exporter.noarch 0:5.68-3.amzn2
  perl-File-Path.noarch 0:2.09-2.amzn2                       perl-File-Temp.noarch 0:0.23.01-3.amzn2                    perl-Filter.x86_64 0:1.49-3.amzn2.0.2
  perl-Getopt-Long.noarch 0:2.40-3.amzn2                     perl-HTTP-Tiny.noarch 0:0.033-3.amzn2                      perl-PathTools.x86_64 0:3.40-5.amzn2.0.2
  perl-Pod-Escapes.noarch 1:1.04-294.amzn2                   perl-Pod-Perldoc.noarch 0:3.20-4.amzn2                     perl-Pod-Simple.noarch 1:3.28-4.amzn2
  perl-Pod-Usage.noarch 0:1.63-3.amzn2                       perl-Scalar-List-Utils.x86_64 0:1.27-248.amzn2.0.2         perl-Socket.x86_64 0:2.010-4.amzn2.0.2
  perl-Storable.x86_64 0:2.45-3.amzn2.0.2                    perl-Text-ParseWords.noarch 0:3.29-4.amzn2                 perl-Time-HiRes.x86_64 4:1.9725-3.amzn2.0.2
  perl-Time-Local.noarch 0:1.2300-2.amzn2                    perl-constant.noarch 0:1.27-2.amzn2.0.1                    perl-libs.x86_64 4:5.16.3-294.amzn2
  perl-macros.x86_64 4:5.16.3-294.amzn2                      perl-parent.noarch 1:0.225-244.amzn2.0.1                   perl-podlators.noarch 0:2.5.1-3.amzn2.0.1
  perl-threads.x86_64 0:1.87-4.amzn2.0.2                     perl-threads-shared.x86_64 0:1.43-6.amzn2.0.2              python-chardet.noarch 0:2.2.1-1.amzn2
  python-kitchen.noarch 0:1.1.1-5.amzn2                      qrencode-libs.x86_64 0:3.4.1-3.amzn2.0.2                   shadow-utils.x86_64 2:4.1.5.1-24.amzn2.0.2
  systemd-libs.x86_64 0:219-57.amzn2.0.9                     ustr.x86_64 0:1.0.4-16.amzn2.0.3                           util-linux.x86_64 0:2.30.2-2.amzn2.0.4
  vim-common.x86_64 2:7.4.160-4.amzn2.0.16                   vim-filesystem.x86_64 2:7.4.160-4.amzn2.0.16               which.x86_64 0:2.20-7.amzn2.0.2

Complete!

ユーザ作成

 ユーザtestを作成し、sudo権限を与えます。

bash-4.2# useradd test

bash-4.2# ls /home
test

bash-4.2# visudo
    // 最後に以下を追加
    test ALL=(ALL)       NOPASSWD: ALL

 Ctrl+PCtrl+Qでデダッチして一旦コンテナから抜けます。(Ctrl+Dで抜けるとコンテナが終了してしまう)

3. ローカルでの作業

ユーザを指定してコンテナに入る

% docker exec -u test -it test_amazonlinux /bin/bash
[test@c79c8f630da9 ~]$

4. コンテナ内での作業

Extra Packages for Enterprise Linux (EPEL)のインストール

 Amazon Linux2にはExtras Library と呼ばれるパッケージ群があるのですが使用できるパッケージが限られるらしいので、代わりにEPELをインストールします。
 EPELとは標準で提供されていないパッケージをyumコマンドでインストールできるようにするリポジトリのことです。
 epelのバージョンについて、Amazon Linux2はCentOS7系なのでバージョン7を指定します。

[test@c79c8f630da9 ~]$ sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Loaded plugins: ovl, priorities
epel-release-latest-7.noarch.rpm                                                                                                                           |  15 kB  00:00:00

.....(中略).....

Installed:
  epel-release.noarch 0:7-11

Complete!

EPEL/remiリポジトリのインストール

 サードパーティ製のリポジトリのEPEL/remiをインストールします。
 Extras Libraryは使わないので、優先度を10から99に下げます。

[test@c79c8f630da9 ~]$ sudo yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Loaded plugins: ovl, priorities
remi-release-7.rpm                                                                                                                                         |  16 kB  00:00:00

.....(中略).....

Installed:
  remi-release.noarch 0:7.6-2.el7.remi

Complete!

[test@c79c8f630da9 ~]$ sudo yum-config-manager --enable remi-php73

[test@c79c8f630da9 ~]$ sudo sed -i -e "s/priority=10/priority=99/g" /etc/yum.repos.d/amzn2-core.repo

[test@c79c8f630da9 ~]$ sudo cat /etc/yum.repos.d/amzn2-core.repo
[amzn2-core]
name=Amazon Linux 2 core repository
mirrorlist=http://amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list
priority=99
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2
enabled=1
metadata_expire=300
mirrorlist_expire=300
report_instanceid=yes

[amzn2-core-source]
name=Amazon Linux 2 core repository - source packages
mirrorlist=http://amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/SRPMS/mirror.list
priority=99
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2
enabled=0
metadata_expire=300
mirrorlist_expire=300
report_instanceid=yes

[amzn2-core-debuginfo]
name=Amazon Linux 2 core repository - debuginfo packages
mirrorlist=http://amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/debuginfo/$basearch/mirror.list
priority=99
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2
enabled=0
metadata_expire=300
mirrorlist_expire=300
report_instanceid=yes

PHP関連のインストール

PHP7.3とその他のパッケージをインストールします。

[test@c79c8f630da9 ~]$ sudo yum install -y --enablerepo=remi,remi-php73 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt php-zip
Loaded plugins: ovl, priorities
amzn2-core                                                                                                                                                 | 2.4 kB  00:00:00
epel/x86_64/metalink                                                                                                                                       | 7.7 kB  00:00:00
epel                                                                                                                                                       | 5.3 kB  00:00:00
remi                                                                                                                                                       | 3.0 kB  00:00:00
remi-php73                                                                                                                                                 | 3.0 kB  00:00:00
remi-safe                                                                                                                                                  | 3.0 kB  00:00:00
(1/6): epel/x86_64/group_gz                                                                                                                                |  88 kB  00:00:00
(2/6): epel/x86_64/updateinfo                                                                                                                              | 976 kB  00:00:00
(3/6): epel/x86_64/primary_db                                                                                                                              | 6.7 MB  00:00:00
(4/6): remi-safe/primary_db                                                                                                                                | 1.6 MB  00:00:03
(5/6): remi/primary_db                                                                                                                                     | 2.5 MB  00:00:03
(6/6): remi-php73/primary_db                                                                                                                               | 198 kB  00:00:04
Package php-mcrypt is obsoleted by php-pecl-mcrypt, trying to install php-pecl-mcrypt-1.0.2-2.el7.remi.7.3.x86_64 instead

.....(中略).....

Installed:
  php.x86_64 0:7.3.6-3.el7.remi        php-devel.x86_64 0:7.3.6-3.el7.remi              php-gd.x86_64 0:7.3.6-3.el7.remi               php-mbstring.x86_64 0:7.3.6-3.el7.remi
  php-pdo.x86_64 0:7.3.6-3.el7.remi    php-pecl-mcrypt.x86_64 0:1.0.2-2.el7.remi.7.3    php-pecl-zip.x86_64 0:1.15.4-1.el7.remi.7.3    php-xml.x86_64 0:7.3.6-3.el7.remi

Dependency Installed:
  apr.x86_64 0:1.6.3-5.amzn2.0.2                                apr-util.x86_64 0:1.6.1-5.amzn2.0.2                     apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2
  autoconf.noarch 0:2.69-11.amzn2                               automake.noarch 0:1.13.4-3.1.amzn2                      binutils.x86_64 0:2.29.1-27.amzn2.0.2
  cpp.x86_64 0:7.3.1-5.amzn2.0.2                                fontconfig.x86_64 0:2.10.95-11.amzn2.0.2                fontpackages-filesystem.noarch 0:1.44-8.amzn2
  freetype.x86_64 0:2.4.11-15.amzn2.0.2                         gcc.x86_64 0:7.3.1-5.amzn2.0.2                          gcc-c++.x86_64 0:7.3.1-5.amzn2.0.2
  gd-last.x86_64 0:2.2.5-8.el7.remi                             generic-logos-httpd.noarch 0:18.0.0-4.amzn2             glibc-devel.x86_64 0:2.26-32.amzn2.0.1
  glibc-headers.x86_64 0:2.26-32.amzn2.0.1                      httpd.x86_64 0:2.4.39-1.amzn2.0.1                       httpd-filesystem.noarch 0:2.4.39-1.amzn2.0.1
  httpd-tools.x86_64 0:2.4.39-1.amzn2.0.1                       jbigkit-libs.x86_64 0:2.0-11.amzn2.0.2                  kernel-headers.x86_64 0:4.14.121-109.96.amzn2
  keyutils-libs-devel.x86_64 0:1.5.8-3.amzn2.0.2                krb5-devel.x86_64 0:1.15.1-20.amzn2.0.1                 libX11.x86_64 0:1.6.5-2.amzn2.0.2
  libX11-common.noarch 0:1.6.5-2.amzn2.0.2                      libXau.x86_64 0:1.0.8-2.1.amzn2.0.2                     libXpm.x86_64 0:3.5.12-1.amzn2.0.2
  libargon2.x86_64 0:20161029-3.el7                             libargon2-devel.x86_64 0:20161029-3.el7                 libatomic.x86_64 0:7.3.1-5.amzn2.0.2
  libcilkrts.x86_64 0:7.3.1-5.amzn2.0.2                         libcom_err-devel.x86_64 0:1.42.9-12.amzn2.0.2           libedit.x86_64 0:3.0-12.20121213cvs.amzn2.0.2
  libedit-devel.x86_64 0:3.0-12.20121213cvs.amzn2.0.2           libgomp.x86_64 0:7.3.1-5.amzn2.0.2                      libitm.x86_64 0:7.3.1-5.amzn2.0.2
  libjpeg-turbo.x86_64 0:1.2.90-5.amzn2.0.3                     libkadm5.x86_64 0:1.15.1-20.amzn2.0.1                   libmcrypt.x86_64 0:2.5.8-13.el7
  libmpc.x86_64 0:1.0.1-3.amzn2.0.2                             libmpx.x86_64 0:7.3.1-5.amzn2.0.2                       libpng.x86_64 2:1.5.13-7.amzn2.0.2
  libquadmath.x86_64 0:7.3.1-5.amzn2.0.2                        libsanitizer.x86_64 0:7.3.1-5.amzn2.0.2                 libselinux-devel.x86_64 0:2.5-12.amzn2.0.2
  libsepol-devel.x86_64 0:2.5-8.1.amzn2.0.2                     libtiff.x86_64 0:4.0.3-27.amzn2.0.2                     libtool.x86_64 0:2.4.2-22.2.amzn2.0.2
  libverto-devel.x86_64 0:0.2.5-4.amzn2.0.2                     libwebp7.x86_64 0:1.0.2-1.el7.remi                      libxcb.x86_64 0:1.12-1.amzn2.0.2
  libxml2-devel.x86_64 0:2.9.1-6.amzn2.3.2                      libxslt.x86_64 0:1.1.28-5.amzn2.0.2                     libzip5.x86_64 0:1.5.2-1.el7.remi
  lyx-fonts.noarch 0:2.2.3-1.el7                                m4.x86_64 0:1.4.16-10.amzn2.0.2                         mailcap.noarch 0:2.1.41-2.amzn2
  mod_http2.x86_64 0:1.14.1-1.amzn2                             mpfr.x86_64 0:3.1.1-4.amzn2.0.2                         ncurses-c++-libs.x86_64 0:6.0-8.20170212.amzn2.1.2
  ncurses-compat-libs.x86_64 0:6.0-8.20170212.amzn2.1.2         ncurses-devel.x86_64 0:6.0-8.20170212.amzn2.1.2         oniguruma5.x86_64 0:6.9.2-1.el7.remi
  openssl-devel.x86_64 1:1.0.2k-16.amzn2.1.1                    pcre-devel.x86_64 0:8.32-17.amzn2.0.2                   perl-Data-Dumper.x86_64 0:2.145-3.amzn2.0.2
  perl-Test-Harness.noarch 0:3.28-3.amzn2                       perl-Thread-Queue.noarch 0:3.02-2.amzn2                 php-cli.x86_64 0:7.3.6-3.el7.remi
  php-common.x86_64 0:7.3.6-3.el7.remi                          php-json.x86_64 0:7.3.6-3.el7.remi                      pkgconfig.x86_64 1:0.27.1-4.amzn2.0.2
  tar.x86_64 2:1.26-34.amzn2                                    xz-devel.x86_64 0:5.2.2-1.amzn2.0.2                     zlib-devel.x86_64 0:1.2.7-17.amzn2.0.2

Complete!

[test@c79c8f630da9 ~]$ php -v
PHP 7.3.6 (cli) (built: May 28 2019 09:32:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies

composerのインストール

 PHPのライブラリ管理ツールのcomposerをインストールします。

[test@c79c8f630da9 ~]$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 1.8.6) successfully installed to: /home/test/composer.phar
Use it: php composer.phar

[test@c79c8f630da9 ~]$ sudo mv composer.phar /usr/local/bin/composer

[test@c79c8f630da9 ~]$ composer --version
Composer version 1.8.6 2019-06-11 15:03:05

Laravelのインストール

 composerでLaravelをインストールします。

[test@c79c8f630da9 ~]$ composer global require laravel/installer
Changed current directory to /home/test/.composer
Using version ^2.1 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 13 installs, 0 updates, 0 removals
  - Installing symfony/process (v4.3.1): Downloading (100%)
  - Installing symfony/polyfill-ctype (v1.11.0): Downloading (100%)
  - Installing symfony/filesystem (v4.3.1): Downloading (100%)
  - Installing symfony/service-contracts (v1.1.2): Downloading (100%)
  - Installing symfony/polyfill-php73 (v1.11.0): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.11.0): Downloading (100%)
  - Installing symfony/console (v4.3.1): Downloading (100%)
  - Installing guzzlehttp/promises (v1.3.1): Downloading (100%)
  - Installing ralouphie/getallheaders (2.0.5): Downloading (100%)
  - Installing psr/http-message (1.0.1): Downloading (100%)
  - Installing guzzlehttp/psr7 (1.5.2): Downloading (100%)
  - Installing guzzlehttp/guzzle (6.3.3): Downloading (100%)
  - Installing laravel/installer (v2.1.0): Downloading (100%)
symfony/service-contracts suggests installing psr/container
symfony/service-contracts suggests installing symfony/service-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
symfony/console suggests installing psr/log (For using the console logger)
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files

[test@c79c8f630da9 ~]$ echo export PATH='~/.composer/vendor/bin:$PATH' >> ~/.bash_profile

[test@c79c8f630da9 ~]$ source ~/.bash_profile

[test@c79c8f630da9 ~]$ laravel -V
Laravel Installer 2.1.0

MySQLのインストール

 今回データベースはMySQLを使うのでインストールします。

[test@c79c8f630da9 ~]$ sudo yum remove mariadb-libs
Loaded plugins: ovl, priorities
No Match for argument: mariadb-libs
No Packages marked for removal

[test@c79c8f630da9 ~]$ rm -rf /var/lib/mysql/

[test@c79c8f630da9 ~]$ sudo yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
Loaded plugins: ovl, priorities
mysql57-community-release-el7-7.noarch.rpm                                                                                                                 | 8.8 kB  00:00:00
Examining /var/tmp/yum-root-kl2LMo/mysql57-community-release-el7-7.noarch.rpm: mysql57-community-release-el7-7.noarch
Marking /var/tmp/yum-root-kl2LMo/mysql57-community-release-el7-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================================
 Package                                          Arch                          Version                      Repository                                                      Size
==================================================================================================================================================================================
Installing:
 mysql57-community-release                        noarch                        el7-7                        /mysql57-community-release-el7-7.noarch                        7.8 k

Transaction Summary
==================================================================================================================================================================================
Install  1 Package

Total size: 7.8 k
Installed size: 7.8 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-7.noarch                                                                                                                         1/1
  Verifying  : mysql57-community-release-el7-7.noarch                                                                                                                         1/1

Installed:
  mysql57-community-release.noarch 0:el7-7

Complete!

[test@c79c8f630da9 ~]$ sudo yum -y install mysql-community-server
Loaded plugins: ovl, priorities
amzn2-core                                                                                                                                                 | 2.4 kB  00:00:00
mysql-connectors-community                                                                                                                                 | 2.5 kB  00:00:00
mysql-tools-community                                                                                                                                      | 2.5 kB  00:00:00
mysql57-community                                                                                                                                          | 2.5 kB  00:00:00
(1/3): mysql-tools-community/x86_64/primary_db                                                                                                             |  58 kB  00:00:00
(2/3): mysql-connectors-community/x86_64/primary_db                                                                                                        |  41 kB  00:00:00
(3/3): mysql57-community/x86_64/primary_db                                                                                                                 | 177 kB  00:00:00

.....(中略).....

Installed:
  mysql-community-server.x86_64 0:5.7.26-1.el7

Dependency Installed:
  libaio.x86_64 0:0.3.109-13.amzn2.0.2                    mysql-community-client.x86_64 0:5.7.26-1.el7                   mysql-community-common.x86_64 0:5.7.26-1.el7
  mysql-community-libs.x86_64 0:5.7.26-1.el7              net-tools.x86_64 0:2.0-0.22.20131004git.amzn2.0.2              numactl-libs.x86_64 0:2.0.9-7.amzn2
  procps-ng.x86_64 0:3.3.10-17.amzn2.2.2

Complete!

[test@3194883a46ff /]$ mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper

[test@c79c8f630da9 ~]$ systemctl stop mysqld
Failed to get D-Bus connection: Operation not permitted

ここでsystemctlが使えないと言われるので、Ctrl+PCtrl+Qで一旦コンテナから抜けます。

5. ローカルでの作業

systemtdの入ったコンテナをコミットしてイメージとして保存

 コンテナ内でsystemctlコマンドを使うためにはdocker runの時に、--privilegedコマンドをつけて/sbin/initで立ち上げないといけないみたいです。
 そこで、まず今まで作業してきたコンテナをコミットしてイメージとして保存します。今回はイメージのタグを同じものにして上書きしています。(怖いから他のタグにして分けたほうが良いかも)
 次に今まで作業してきたtest_amazonlinuxコンテナは使わないのでstopします。
 最後に、以下のdocker runコマンドでコンテナを立ち上げます。

% docker commit test_amazonlinux amazonlinux:test
sha256:d0673e80bd8cd1df87c17cef2b823c89291071db5466b0ede694ff7156dda81a

% docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
amazonlinux         test                d0673e80bd8c        2 minutes ago       1.73GB

% docker stop test_amazonlinux
test_amazonlinux

% docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                        PORTS                  NAMES
c79c8f630da9        amazonlinux:2        "/bin/bash"         43 minutes ago       Exited (137) 2 seconds ago                          test_amazonlinux

% docker run -it -d --privileged --name test_amazonlinux2 -p 8081:80 amazonlinux:2 /sbin/init
8bb55cb6d792eeb865ca8eef576242c6326ed7723845ab6a0e0fbc54dab6ab2d

% docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                  NAMES
3194883a46ff        amazonlinux:test     "/sbin/init"        17 seconds ago      Up 16 seconds       0.0.0.0:8081->80/tcp   test_amazonlinux2

% docker exec -u test -it test_amazonlinux2 /bin/bash
[test@3194883a46ff /]$

6. コンテナ内での作業

MySQLの設定と起動

 systemctl enable ...は自動的にMySQL Serverを起動するための設定です。
 また今回はとりあえずsystemctl set-environment ...でパスワードなしでmysqlにログインするように設定しています。

[test@3194883a46ff /]$ sudo systemctl stop mysqld

[test@3194883a46ff /]$ sudo systemctl enable mysqld.service

[test@3194883a46ff /]$ sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

[test@3194883a46ff /]$ sudo systemctl start mysqld

[test@3194883a46ff /]$ systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-06-16 19:03:51 UTC; 4min 56s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 120 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 57 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 122 (mysqld)
   CGroup: /docker/3194883a46ffa199b2f9d2b519d349fc82997b807b650d31b1a94d0ada49643f/system.slice/mysqld.service
           └─122 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
           ‣ 122 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

おわりに

 docker上で環境構築するのは大変ですね。

参考

Dockerでcommitしてみる
AmazonLinux2のイメージを/sbin/initで起こせない!!【oci runtime error】
コンテナに外部からアクセス(ポートフォワード)
[AWS][ansible]Amazon Linux2にEPEL/remiでPHPをインストールするのにハマったお話
MySQL 5.7 を CentOS 7 に yum インストールする手順

22
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
17