0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerの基礎を学ぶ

Last updated at Posted at 2025-04-30

Dockerの基礎を学ぶ

Dockerの個人学習で学んだことをアウトプットするために作成した記事です。
インストールするところから基礎的な使い方まで学んだことを順番に書いていきます。

目次

  • 準備(Dockerのインストール)
  • 基礎練習(Hello World)
  • 基礎練習(ubuntuコンテナの作成・起動)
  • 基礎練習(コンテナの基本操作)

準備(Dockerのインストール)

まずはDockerをインストールします。
インストール方法はDockerマニュアルにも記載されていますがこのページではコマンドと簡単な説明だけメモしておきます。

インストールに必要なパッケージのインストール

curlなど Dockerのインストールで利用するパッケージ類をあらかじめインストールします。

apt update

apt install ca-certificates
apt install curl
apt install gnupg

GPG鍵の取得

Dockerの公式GPG鍵をcurlコマンドで追加します

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

リポジトリの追加

Dockerリポジトリをセットアップします。(aptコマンド実行時に参照するファイルにDockerリポジトリのURLを追加)

 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のインストール

aptコマンドでDockerをインストールします。

apt update
apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

インストール確認

正常にインストールされていればバージョンが表示されます。

docker -v

基礎練習(Hello World)

Dockerの公開レジストリで公開されているイメージをダウンロードして
アプリケーションを実行するところまで実施します。
ここではHello Worldというイメージを公式のレジストリからダウンロードして実際に実行するところまでの流れを書きます。

docker search

まずはdocker serch [registory name]コマンドでレジストリーの内容を確認してみます。
今回はlibraryというレジストリの内容を確認しています。
実際に実行してみると以下のような出力結果になりました。

root@ubuntu-64bit:~# docker search library
NAME          DESCRIPTION                                     STARS     OFFICIAL
memcached     Free & open source, high-performance, distri…   2362      [OK]
busybox       Busybox base image.                             3415      [OK]
alpine        A minimal Docker image based on Alpine Linux…   11269     [OK]
redis         Redis is the world’s fastest data platform f…   13281     [OK]
ubuntu        Ubuntu is a Debian-based Linux operating sys…   17556     [OK]
nginx         Official build of Nginx.                        20769     [OK]
python        Python is an interpreted, interactive, objec…   10086     [OK]
node          Node.js is a JavaScript-based platform for s…   13889     [OK]
hello-world   Hello World! (an example of minimal Dockeriz…   2440      [OK]
mariadb       MariaDB Server is a high performing open sou…   5960      [OK]
httpd         The Apache HTTP Server Project                  4868      [OK]
rabbitmq      RabbitMQ is an open source multi-protocol me…   5254      [OK]
traefik       Traefik, The Cloud Native Edge Router           3446      [OK]
centos        DEPRECATED; The official build of CentOS.       7773      [OK]
sonarqube     Official images for SonarQube, code analysis…   2495      [OK]
php           While designed for web development, the PHP …   7738      [OK]
haproxy       HAProxy - The Reliable, High Performance TCP…   1975      [OK]
telegraf      Telegraf is an agent for collecting metrics …   708       [OK]
registry      Distribution implementation for storing and …   4127      [OK]
nextcloud     Nextcloud manual docker image                   4312      [OK]
vault         Vault is a tool for securely accessing secre…   1156      [OK]
ruby          Ruby is a dynamic, reflective, object-orient…   2380      [OK]
caddy         Caddy 2 is a powerful, enterprise-ready, ope…   871       [OK]
consul        Consul is a datacenter runtime that provides…   1455      [OK]
kong          The Cloud-Native API Gateway for APIs and Mi…   822       [OK]

docker pull

レジストリに公開されているイメージをダウンロードする際にはdocker pull [image name]コマンドを利用します。
今回は公式レジストリにあるhello worldをpullしてきます。

docker pull hello-world

ダウンロードした内容はdocker imagesコマンドで確認できます。
docker pullが成功していれば以下のような出力結果になるかと思います。

root@ubuntu-64bit:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    74cc54e27dc4   3 months ago   10.1kB

docker run

dockerイメージの用意ができたので、これを使ってコンテナを作成します。
コマンドはdocker run [IMAGE ID or REPOSITORY Name]です。

以下のような出力結果が得られれば成功です。

root@ubuntu-64bit:~# docker run 74cc54e27dc4

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/

基礎練習(ubuntuコンテナの作成・起動)

別のイメージファイルからコンテナを作成し、そのコンテナ環境でbashを起動するところまで実施します。コマンド実施後はコンテナの標準入力に接続されます。

①ubuntuのイメージをpullします

docker pull ubuntu

②IMAGE IDを確認します

root@ubuntu-64bit:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        latest    602eb6fb314b   3 weeks ago    78.1MB

③コンテナを作成し、作成したコンテナ環境に入ります

docker run -it --name test_ubuntu 602eb6fb314b /bin/bash

コマンドが成功すると、コンテナが立ち上がるとともにコンテナの標準入力に接続されます。プロンプトのホスト名などが変わるので、うまくいったかどうかはすぐにわかると思います。

コマンドに色々なオプションをつけていますが、各オプションの意味は次の表にまとめています。

option 説明
-i コンテナで実施したコマンドの標準入力に接続する
-t コンテナに疑似TTY端末を割り当てます
--name 起動するコンテナに名前を付けます。省略すると適当な名前が付きます
/bin/bash コンテナ内で最初に実行するコマンドを指定します。(今回は/bin/bashを指定)

④コンテナから抜ける
exitコマンドでコンテナ内のbashコマンドプロンプトを抜けて、ホストのプロンプトに戻ります。exitで抜けたあとdocker psコマンドを実行するとコンテナが停止していることが確認できます。

基礎練習(コンテナの基本操作)

コンテナの作成はdocker runコマンドを利用します。
当然コンテナにも作った後ずっと起動しておきたいものや、一瞬起動させて用が済んだら停止させたいものなどいろいろあるかと思います。
このセクションには作った後のコンテナを操作する方法を書いていきます。
実際にコンテナの起動、停止、バックグラウンド実行などdockerサブコマンドを使ってみます。

コンテナの起動

停止しているコンテナを起動させるコマンドはdocker startです。
以下操作例です。

### docker psでコンテナが停止していることを確認しています
root@ubuntu-64bit:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS                         PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   50 minutes ago      Exited (127) 18 minutes ago              test_ubuntu


### CONTAINER IDを指定してコンテナの起動を試みています
root@ubuntu-64bit:~# 
root@ubuntu-64bit:~# docker start 15f81be4e406
15f81be4e406


### docker psでコンテナが起動していることを確認しています
root@ubuntu-64bit:~# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS         PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   51 minutes ago   Up 5 seconds             test_ubuntu
root@ubuntu-64bit:~# 

コンテナの停止

起動しているコンテナを停止させるコマンドはdocker stopです。
以下操作例です。

### docker psでコンテナが起動していることを確認しています
root@ubuntu-64bit:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS         PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   About an hour ago   Up 9 minutes             test_ubuntu


### CONTAINER IDを指定してコンテナの停止を試みています
root@ubuntu-64bit:~# docker stop 15f81be4e406 
15f81be4e406


### docker psでコンテナが停止していることを確認しています
root@ubuntu-64bit:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS                       PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   About an hour ago   Exited (137) 9 seconds ago             test_ubuntu
root@ubuntu-64bit:~# 
root@ubuntu-64bit:~# 
root@ubuntu-64bit:~# 

起動中のコンテナに接続する

起動しているコンテナに接続するコマンドはdocker attachです。
以下操作例です。

### docker psでコンテナが起動していることを確認しています
root@ubuntu-64bit:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS         PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   About an hour ago   Up 7 minutes             test_ubuntu


### docker attachコマンドでコンテナのbashに接続を試みています
root@ubuntu-64bit:~# docker attach 15f81be4e406


### プロンプトがコンテナのbashに変わることが確認できます
root@15f81be4e406:/# 
root@15f81be4e406:/# 

コンテナを停止させることなくコンテナのbashから抜ける

普通にexitなどのコマンドを入力すると、bashから抜けた時点でコンテナも停止してしまいます。
コンテナを停止させることなく、コンテナのbashから抜ける方法は以下です。

コンテナ内に入っている状態で、[Ctrl]+[P]、[Ctrl]+[Q]を押下する。
以下操作例です。(コマンドではないのでわかりづらいですが画面のイメージを載せておきます。)

### 出力例
root@15f81be4e406:/# 
root@15f81be4e406:/# read escape sequence
root@ubuntu-64bit:~# 
root@ubuntu-64bit:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED       STATUS          PORTS     NAMES
15f81be4e406   602eb6fb314b   "/bin/bash"   2 hours ago   Up 20 minutes             test_ubuntu
root@ubuntu-64bit:~# 

参考文献

この記事は以下の文献を参考に執筆しました。

Docker Engine インストール(Ubuntu 向け)
Dockerコンテナのアタッチとデタッチについて
書籍:中井悦治, Docker実践入門 -コンテナ技術の基礎から応用まで, 株式会社技術評論社

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?