【入門】はじめての Docker Desktop for Mac のインストールと CentOS の仮想環境構築のセットアップ
Docker HUBから centsos イメージをダウンロード
Docker image pull centos
centosを起動
docker container run -it -d centos c496e4758b7355eec51e4e3fa27b5ecf597697e3296d87e7aed93663cb39cf1c
実行中のcentosにログイン
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a2c361e1df70 centos "/bin/bash" 6 seconds ago Up 5 seconds ecstatic_kalam docker exec -it a2c361e1df70 /bin/bash [root@a2c361e1df70 /]#
バージョン確認
[root@a2c361e1df70 /]# cat /etc/redhat-release CentOS Linux release 8.4.2105
apacheとphpをインストール
[root@bb6c85f446db /]# yum install httpd php -y Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 42 B/s | 38 B 00:00 Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
centos8に登録されているyumのmirrotlistには使えないサイトが含まれているので更新の必要があるらしい。(docker内でyumがつながらない時)
しかしcentos8はすでにサポートが終了しているので、centos7を使用する。
centos7のイメージをダウンロード
docke image pull centos:centos7
centosのコンテナは単純に起動しただけではsystemctl系のコマンドが使えない。
これはコンテナに全ての権限が与えられていないためらしい。(アクセスできないデバイスがあるとか)
systemctl系を使えるようにするには起動引数に「–privileged」、「/sbin/init」を追加する
例
docker container run -it -d --privileged -p 8080:80 centos:centos7 /sbin/init
centos7のコンテナではyumは期待通り動作するがsystemctlを実行すると以下のエラーが表示される。
Failed to get D-Bus connection: No such file or directory
これはMacだけの現象かも
このエラーの解消方法はDocker for mac (ver 4.3以降) で「Failed to get D-Bus connection」エラーが出たときの対処法に記載がある
~/Library/Group\ Containers/group.com.docker/settings.json { // ︙ "dataFolder": "/Users/xxx/Library/Containers/com.docker.docker/Data/vms/0/data", "deprecatedCgroupv1": false, // ← こちらを true へ "disableHardwareAcceleration": false, "disableTips": true, // ︙
settings.json編集後、Macを再起動。
これでDockerコンテナ上のcentos7でsystemctlが使えるようになる。
コンテナを起動後、コンテナにログイン
docker container run -it -d --privileged -p 8080:80 centos:centos7 /sbin/init docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 75ffbe714b9e centos:centos7 "/sbin/init" 48 seconds ago Up 47 seconds 0.0.0.0:8080->80/tcp strange_benz docker exec -it 75ffbe714b9e /bin/bash
Apache、phpをインストール
yum install httpd php -y Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: ftp.nara.wide.ad.jp * extras: ftp.nara.wide.ad.jp * updates: ftp.nara.wide.ad.jp base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 153 kB 00:00:00 (2/4): extras/7/x86_64/primary_db | 250 kB 00:00:00 (3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:01 (4/4): updates/7/x86_64/primary_db | 22 MB 00:00:03 Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-99.el7.centos.1 will be installed --> Processing Dependency: httpd-tools = 2.4.6-99.el7.centos.1 for package: httpd-2.4.6-99.el7.centos.1.x86_64 --> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-99.el7.centos.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-99.el7.centos.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-99.el7.centos.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-99.el7.centos.1.x86_64 ---> Package php.x86_64 0:5.4.16-48.el7 will be installed --> Processing Dependency: php-common(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64 --> Processing Dependency: php-cli(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-7.el7 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7_9.1 will be installed ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed ---> Package httpd-tools.x86_64 0:2.4.6-99.el7.centos.1 will be installed ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed ---> Package php-cli.x86_64 0:5.4.16-48.el7 will be installed --> Processing Dependency: libedit.so.0()(64bit) for package: php-cli-5.4.16-48.el7.x86_64 ---> Package php-common.x86_64 0:5.4.16-48.el7 will be installed --> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-48.el7.x86_64 --> Running transaction check ---> Package libedit.x86_64 0:3.0-12.20121213cvs.el7 will be installed ---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================== Package Arch Version Repository Size ======================================================================================================== Installing: httpd x86_64 2.4.6-99.el7.centos.1 updates 2.7 M php x86_64 5.4.16-48.el7 base 1.4 M Installing for dependencies: apr x86_64 1.4.8-7.el7 base 104 k apr-util x86_64 1.5.2-6.el7_9.1 updates 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M httpd-tools x86_64 2.4.6-99.el7.centos.1 updates 94 k libedit x86_64 3.0-12.20121213cvs.el7 base 92 k libzip x86_64 0.10.1-8.el7 base 48 k mailcap noarch 2.1.41-2.el7 base 31 k php-cli x86_64 5.4.16-48.el7 base 2.7 M php-common x86_64 5.4.16-48.el7 base 565 k Transaction Summary ======================================================================================================== Install 2 Packages (+9 Dependent packages) Total download size: 29 M Installed size: 49 M Downloading packages: warning: /var/cache/yum/x86_64/7/updates/packages/apr-util-1.5.2-6.el7_9.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for apr-util-1.5.2-6.el7_9.1.x86_64.rpm is not installed (1/11): apr-util-1.5.2-6.el7_9.1.x86_64.rpm | 92 kB 00:00:00 Public key for apr-1.4.8-7.el7.x86_64.rpm is not installed (2/11): apr-1.4.8-7.el7.x86_64.rpm | 104 kB 00:00:00 (3/11): libedit-3.0-12.20121213cvs.el7.x86_64.rpm | 92 kB 00:00:00 (4/11): libzip-0.10.1-8.el7.x86_64.rpm | 48 kB 00:00:00 (5/11): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00 (6/11): httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm | 94 kB 00:00:00 (7/11): php-5.4.16-48.el7.x86_64.rpm | 1.4 MB 00:00:00 (8/11): php-common-5.4.16-48.el7.x86_64.rpm | 565 kB 00:00:00 (9/11): php-cli-5.4.16-48.el7.x86_64.rpm | 2.7 MB 00:00:00 (10/11): httpd-2.4.6-99.el7.centos.1.x86_64.rpm | 2.7 MB 00:00:00 (11/11): centos-logos-70.0.6-3.el7.centos.noarch.rpm | 21 MB 00:00:02 -------------------------------------------------------------------------------------------------------- Total 13 MB/s | 29 MB 00:00:02 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key)" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-7.el7.x86_64 1/11 Installing : apr-util-1.5.2-6.el7_9.1.x86_64 2/11 Installing : httpd-tools-2.4.6-99.el7.centos.1.x86_64 3/11 Installing : centos-logos-70.0.6-3.el7.centos.noarch 4/11 Installing : libzip-0.10.1-8.el7.x86_64 5/11 Installing : php-common-5.4.16-48.el7.x86_64 6/11 Installing : libedit-3.0-12.20121213cvs.el7.x86_64 7/11 Installing : php-cli-5.4.16-48.el7.x86_64 8/11 Installing : mailcap-2.1.41-2.el7.noarch 9/11 Installing : httpd-2.4.6-99.el7.centos.1.x86_64 10/11 Installing : php-5.4.16-48.el7.x86_64 11/11 Verifying : httpd-tools-2.4.6-99.el7.centos.1.x86_64 1/11 Verifying : mailcap-2.1.41-2.el7.noarch 2/11 Verifying : apr-1.4.8-7.el7.x86_64 3/11 Verifying : libedit-3.0-12.20121213cvs.el7.x86_64 4/11 Verifying : php-cli-5.4.16-48.el7.x86_64 5/11 Verifying : httpd-2.4.6-99.el7.centos.1.x86_64 6/11 Verifying : libzip-0.10.1-8.el7.x86_64 7/11 Verifying : php-common-5.4.16-48.el7.x86_64 8/11 Verifying : php-5.4.16-48.el7.x86_64 9/11 Verifying : apr-util-1.5.2-6.el7_9.1.x86_64 10/11 Verifying : centos-logos-70.0.6-3.el7.centos.noarch 11/11 Installed: httpd.x86_64 0:2.4.6-99.el7.centos.1 php.x86_64 0:5.4.16-48.el7 Dependency Installed: apr.x86_64 0:1.4.8-7.el7 apr-util.x86_64 0:1.5.2-6.el7_9.1 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-99.el7.centos.1 libedit.x86_64 0:3.0-12.20121213cvs.el7 libzip.x86_64 0:0.10.1-8.el7 mailcap.noarch 0:2.1.41-2.el7 php-cli.x86_64 0:5.4.16-48.el7 php-common.x86_64 0:5.4.16-48.el7 Complete!
Apacheのインストールを確認後、起動、phpのバージョン確認
ps -ax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 /sbin/init 17 ? Ss 0:00 /usr/lib/systemd/systemd-journald 31 ? Ss 0:00 /usr/lib/systemd/systemd-udevd 53 ? Ss 0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --syste 61 ? Ss 0:00 /usr/lib/systemd/systemd-logind 62 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux 64 pts/1 Ss 0:00 /bin/bash 160 pts/1 R+ 0:00 ps -ax systemctl start httpd php -v PHP 5.4.16 (cli) (built: Apr 1 2020 04:07:17) Copyright (c) 1997-2013 The PHP Group
/var/www/html/index.phpを作成してブラウザで表示
Apacheを自動起動設定後、イメージを保存
systemctl enable httpd exit
docker container stop 2336ba31d7b4 docker commit 2336ba31d7b4 centos:centos7_httpd_php