PR
スポンサーリンク
スポンサーリンク
スポンサーリンク

[Docker]LinuxにDockerをインストールしてWebサーバを立てて削除するまで丁寧に解説

スポンサーリンク
スポンサーリンク
スポンサーリンク
記事内に広告が含まれています。
スポンサーリンク
スポンサーリンク
スポンサーリンク
スポンサーリンク
スポンサーリンク
スポンサーリンク

はじめに

Dockerを使用して簡単なWebサーバを構築してみました。

Dockerのインストール、イメージの取得、コンテナの起動、ApacheによるWebサーバのデプロイ、コンテナの削除まで行ってみました。

使用するサーバ

[admin@RockyLinux9 ~]$ arch
x86_64
[admin@RockyLinux9 ~]$ cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.5 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.5"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.5 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
VENDOR_NAME="RESF"
VENDOR_URL="https://resf.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.5"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.5"

dockerの初期セットアップ

dokcerの公式レポジトリを設定

sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo

追加されたレポジトリを確認

dnf repolist

出力例

repo id repo の名前
appstream Rocky Linux 9 - AppStream
baseos Rocky Linux 9 - BaseOS
docker-ce-stable Docker CE Stable - x86_64
extras Rocky Linux 9 - Extras

コンテナランタイムなどのインストール

sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

docker-ce:docker community edition。

docker-ce-cli:ターミナルからdockerを操作するためのコマンドツール。

Containerd.io:Containerd は、Dockerが内部的に使用するコンテナランタイム。

docker-buildx-plugin:docker-buildより高性能なビルドツール。

docker-compose-plugin:Docker Compose のプラグイン版。複数のコンテナを定義して一括管理できる便利なツール。例えば docker-compose.yml を使ってWebサーバーとデータベースを連携させることが可能。

dockerの起動、自動起動の設定

sudo systemctl --now enable docker

確認

sudo systemctl is-enabled docker

sudo systemctl status docker

出力例

[admin@RockyLinux9 ~]$ sudo systemctl is-enabled docker
enabled
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: disabled)
Active: active (running) since Mon 2025-05-05 00:39:46 JST; 53s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 52426 (dockerd)
Tasks: 11
Memory: 22.2M
CPU: 173ms
CGroup: /system.slice/docker.service

Dockerグループに非rootユーザーの追加

非rootユーザーがDockerコマンドをsudoなしで操作することができるように、dockerグループに追加します。

sudo usermod -aG docker $(whoami)

確認

cat /etc/group | grep docker

出力例

docker:x:980:admin

動作確認

次のコマンドを実行して「Hello from Docker!」と表示されれば成功です。

docker run --rm hello-world

出力例

[admin@RockyLinux9 ~]$ sudo docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:c41088499908a59aae84b0a49c70e86f4731e588a737f1637e73c8c09d995654
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:

Docker Hub Container Image Library | App Containerization
Welcome to the world's largest container registry built for developers and open source contributors to find, use, and sh...
For more examples and ideas, visit:
Get started
Get started with Docker
[admin@RockyLinux9 ~]$

dockerhubとの連携

docker hubにログインしておきます。

アカウント作成していない方は作成してください。

docker login

出力例

[admin@RockyLinux9 ~]$ docker login

USING WEB-BASED LOGIN

i Info → To sign in with credentials on the command line, use 'docker login -u <username>'


Your one-time device confirmation code is: XXXX-XXXX ※⇩の行にアクセスするとコードの入力が求められるので投入。
Press ENTER to open your browser or submit your device code here: https://login.docker.com/activate

Waiting for authentication in the browser…

WARNING! Your credentials are stored unencrypted in '/home/admin/.docker/config.json'.
Configure a credential helper to remove this warning. See

docker login
Login Succeeded ※ログインに成功すると出力される [admin@RockyLinux9 ~]$

イメージの取得

dockerでubuntuを動かしてみたいと思います。

次のコマンドで、最新バージョンのubuntuイメージを取得します。

docker pull ubuntu:latest

出力例

[root@RockyLinux9 ~]# docker pull ubuntu:latest
latest: Pulling from library/ubuntu
2726e237d1a3: Pull complete
Digest: sha256:1e622c5f073b4f6bfad6632f2616c7f59ef256e96fe78bf6a595d1dc4376ac02
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

イメージの確認

docker images

出力例

[root@RockyLinux9 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 602eb6fb314b 3 weeks ago 78.1MB
hello-world latest 74cc54e27dc4 3 months ago 10.1kB

コンテナーの作成

コンテナーを作成する方法にはdocker createコマンドを使用します。

ただし、docker runを使用することで、イメージの取得、コンテナーの作成・起動までを一貫して行うことが可能です。

構文:docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

docker create — Docker-docs-ja 24.0 ドキュメント

今回は、docker runを使用します。

次のコマンドを使用して、最新バージョンのubuntuイメージを使用してコンテナーの作成から起動までを行います。

docker run -it -d --name test -p 127.0.0.1:10080:80 ubuntu:latest

-i:コンテナーの標準入力に接続します。
-t:疑似ターミナルを割り当てます。
-d:コンテナをデタッチモード、つまりバックグラウンドで動作させます。
-p:ポートフォワーディングします。127.0.0.1へポート番号10080でアクセスすると、80番へフォワーディングします。
–name:コンテナーに任意の名前を付与します。ubuntu:latestがイメージとなります。

出力例

[root@RockyLinux9 ~]# docker run -i -t -d --name test -p 127.0.0.1:10080:80 ubuntu:latest
a4c75887600502937dc475d4246a735b609d84dc765f353b7892368228499cc4

全てのコンテナーの確認

起動の有無に関わらず全てのコンテナーを確認するには次のコマンドを使用します。

docker ps -a

出力例

[root@RockyLinux9 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4c758876005 ubuntu:latest "/bin/bash" 2 minutes ago Up 2 minutes 127.0.0.1:10080->80/tcp test

起動しているコンテナーの確認

起動しているコンテナーのみ確認するには次のコマンドを使用します。

docker ps

出力例

[root@RockyLinux9 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4c758876005 ubuntu:latest "/bin/bash" 2 minutes ago Up 2 minutes 127.0.0.1:10080->80/tcp test

コンテナの操作

起動したコンテナーにアクセスする方法は2つあります。

docker attach コンテナID
docker exec -it コンテナ名 bash

しかし、attachは実運用では使用されません。exec -itを使用します。

docker container exec — Docker-docs-ja 24.0 ドキュメント

次のコマンドで作成したコンテナーのbashを起動します。

また、Apacheのインストールから起動まで行います。

docker exec -it test bash
apt-get update
apt-get install apache2 -y
service apache2 status
service apache2 start

出力例

[root@RockyLinux9 ~]# docker exec -it test bash
root@a4c758876005:/#
root@a4c758876005:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:3 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [1178 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [19.3 MB]
Get:7 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [986 kB]
Get:8 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [21.9 kB]
Get:9 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [1077 kB]
Get:10 http://archive.ubuntu.com/ubuntu noble/restricted amd64 Packages [117 kB]
Get:11 http://archive.ubuntu.com/ubuntu noble/main amd64 Packages [1808 kB]
Get:12 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [331 kB]
Get:13 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1377 kB]
Get:14 http://archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [26.7 kB]
Get:15 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [1348 kB]
Get:16 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1341 kB]
Get:17 http://archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [48.0 kB]
Get:18 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [31.8 kB]
Fetched 29.6 MB in 6s (5279 kB/s)
Reading package lists... Done
root@a4c758876005:/#
root@a4c758876005:/# apt-get install apache2 -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
done.
root@a4c758876005:/# service apache2 status
* apache2 is not running
root@a4c758876005:/# service apache2 start
* Starting Apache httpd web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
*
root@a4c758876005:/#
root@a4c758876005:/#

Webサーバにアクセス

Rocky Linuxからブラウザで127.0.0.1:10080を入力することで、静的コンテンツが表示されます。

面倒くさい方はRocky Linuxからcurlコマンドでも大丈夫です。

コンテナーの停止と削除

コンテナーを削除するには、一旦コンテナーを停止する必要があります。

起動したままで削除しようとすると、次のように注意書きが表示されます。

[root@RockyLinux9 ~]# docker rm a4c758876005
Error response from daemon: cannot remove container "/test": container is running: stop the container before removing or force remove

コンテナーの停止、停止後の削除まで行ってみたいと思います。

docker stop コンテナID
docker rm コンテナID

出力例

[root@RockyLinux9 ~]# docker stop a4c758876005
a4c758876005
[root@RockyLinux9 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@RockyLinux9 ~]# docker rm a4c758876005
a4c758876005
[root@RockyLinux9 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@RockyLinux9 ~]#

無事にコンテナーが削除されました。

参考

Dockerコンテナ内で操作 attachとexecの違い - Qiita
7/16 hidekuroサンからのコメントを受けて修正Dockerのコンテナで動作中のシェルへの接続方法sshdをコンテナ内に立てることなしにシェルに接続する方法として以下の2つがある。do…
DockerコンテナでApacheサーバーを立てる [初級編] - Qiita
まずはじめにもし気に入っていただけたり、役に立ったらぜひ ”いいね!” お待ちしております!投稿のモチベーションに繋がります^^¥この記事の目的Dockerコンテナを利用してapacheサー…
dockerでコンテナが立ち上がらないときやってみること - Qiita
コンテナのログ確認コンテナが立ち上がらずexitedする場合、ログを見てみる$ docker logs { コンテナID }## exitedしたコンテナに入って調べるそれでもわからない場…
【Docker】docker run及びdocker startコマンドを実行してもコンテナが立ち上がらない時の解決策 - Qiita
まだまだ初学者なものでDockerの仕組みはだいたい分かったものの、docker runコマンドやdocker startコマンドを実行してもコンテナが立ち上がらない問題に衝突しました。ここではいろ…
Docker run リファレンス — Docker-docs-ja 24.0 ドキュメント

コメント

タイトルとURLをコピーしました