IT × NZ生活

Dockerに入門する

こんにちは、とのっちょです。

ちょっとは技術者らしいアウトプットもしないとなので、軽くやってみます。Dockerはなかなか手が出ないでずっと少し触る程度だったのですが、いい加減逃げ回っててもキャリア的に首がしまってくるので、プライベートで少しいじってみようと思います。

そもそもDockerってどうやって使うものなの?

今どきITやっててDockerの名前を知らないほうがレアかもしれません。私も名前くらいは知っています。

要は仮想のPC的なもの(ここでの理解で書いている)をローカルに構築して便利に使いましょう、ということなのもわかっています。

Virtualboxなんかだと、GUIから仮想PCを作ってそこにOS入れて、みたいな流れなんですが、Dockerはコンテナというものを立ち上げてその中にOSだとかが入っているイメージなので、だいぶ違いますし技術的にもだいぶ違います。

じゃぁDockerってどうやって使えばいいの?というところをまずは書いてみようと思います。

Dockerの実行単位はコンテナ

DockerではDBサーバーとかアプリケーションサーバーとかそういったものを動かすものをコンテナ、といい、コンテナはイメージという形式で配布されています。

じゃぁ、イメージを取り込んでコンテナを起動してみます。DockerをインストールしたあとでHello worldしてみるとインストールに成功したかまでわかります。

> docker pull hello-world

Using default tag: latest
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

> docker run hello-world

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/

こんな感じなのでインストール自体は成功したようです。「Hello from Docker!」が出るかどうかのようですね。

では、hello worldにかかれていた以下のコマンドを試してみましょう。

> docker run -it ubuntu bash

root@897cd8afdc01:/# uname -r
6.8.0-45-generic
root@897cd8afdc01:/# cat /etc/os-release 
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo

一瞬でコンテナが起動してUbuntuにログインできました。バージョンを確認したところ、24.04でした。これはイメージを事前にインポートしていたので早かったのでしょう。

DBサーバーを立ち上げてみる

ここまでは、イメージからコンテナを起動するだけなのですが、じゃぁ、Webサーバーを起動してみましょう。

https://hub.docker.com/_/mysql

イメージを読み込みます。

> docker pull mysql

Using default tag: latest
latest: Pulling from library/mysql
eba3c26198b7: Pull complete 
fc6c33853069: Pull complete 
f1fa3ee22bea: Pull complete 
5b8b24615ae8: Pull complete 
cded0449fb1a: Pull complete 
095378692b4a: Pull complete 
110d87e5d2a3: Pull complete 
bd1dbbbda514: Pull complete 
982f92841ea3: Pull complete 
de34c1fda3aa: Pull complete 
Digest: sha256:92dc869678019f65d761155dacac660a904f6245bfe1b7997da0a73b2bfc68c9
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

起動してみます

docker run --name tonoccho-mysql -e MYSQL_ROOT_PASSWORD=******** -d mysql:9.0.1

Unable to find image 'mysql:9.0.1' locally
9.0.1: Pulling from library/mysql
Digest: sha256:92dc869678019f65d761155dacac660a904f6245bfe1b7997da0a73b2bfc68c9
Status: Downloaded newer image for mysql:9.0.1
1ff0177f464257cd23f7162f4228270613716d341f228a8d09bccebde89b138d

起動したのでコンテナにログインして、MySQLクライアントで接続してみます。

docker exec -it tonoccho-mysql bash

bash-5.1# mysql -uroot -p********

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 9.0.1 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql>

こんな感じでMySQLに接続することができました。

まとめ

こんな感じでDockerの最初の一歩をやってみました。年のせいか疲れてきたのでこの辺で終わりにします。

ABOUT ME
tonoccho
50代を目前にした父親であり夫でありプログラマーです。2014年からニュージーランドに在住、ニュージーランドでの暮らしや趣味としてのDIY、写真、家庭菜園、そして仕事でもある技術系の話題を発信しています。