0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Play with DockerでUbuntuを使う (1) rootでログインするまで

Last updated at Posted at 2025-04-02

この記事の対象者

  • 高校生から大学1年生くらいの人。
  • Dockerを初めて使ってみる人。
  • Linux (Ubuntu)を試してみたい人。
  • Windowsを利用しているがWSLが利用できない環境にある人。(MacOSはUNIX系OSです)

投稿者にとっては備忘録です。

Dockerのアカウントを取得する

省略。ググるか生成AIさんに聞いてください。

Play with Dockerにログインする

URL

URLは以下の通り。

ユーザー名、パスワードを入力してログイン

+ ADD NEW INSTSNCE

をクリック

sshをコピー

ウインドウ右上に表示されるsshの行をコピーする。

例: ssh ip172-18-0-48-hoge@direct.labs.play-with-docker.com

PowerShellを起動

Windows上でPowerShellを起動する。

先ほどコピーしたssh ip172-18-0-48-hoge@direct.labs.play-with-docker.com を入力(コピぺ)して[Enter]

PowerShell 7.5.0
PS C:\Users\momoz> ssh ip172-18-0-48-hoge@direct.labs.play-with-docker.com
ip172-18-0-48-hoge@direct.labs.play-with-docker.com: Permission denied (publickey).
PS C:\Users\momoz>

Permission denied (publickey).と表示されたため、公開キーを作る必要がある。警告が出なかった人はログインできている(と思います)。

公開キーを作成する

PowerShellでssh-keygenと入力して、あとは何回か[Enter]を押す。

PS C:\Users\momoz> ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\momoz/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\momoz/.ssh/id_ed25519
Your public key has been saved in C:\Users\momoz/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:hogehoge momoz@DESKTOP-UE3U5NE
The key's randomart image is:
+--[ED25519 256]--+
|   .o    +oo  .o |
|                 |
+----[SHA256]-----+
PS C:\Users\momoz>

PowerShellで接続

先ほどコピーしたssh ip172-18-0-48-hoge@direct.labs.play-with-docker.com をPowerShellに入力(コピペ)して[Enter]

PowerShell 7.5.0
PS C:\Users\momoz> ssh ip172-18-0-48-hoge@direct.labs.play-with-docker.com
Connecting to 20.25.0.130:8022
###############################################################
#                          WARNING!!!!                        #
# This is a sandbox environment. Using personal credentials   #
# is HIGHLY! discouraged. Any consequences of doing so are    #
# completely the user's responsibilites.                      #
#                                                             #
# The PWD team.                                               #
###############################################################
-sh: disown: not found
[node1] (local) root@192.168.0.13 ~
$
[1]+  Done(1)                    (while !docker info 1>&/dev/null; do sleep 1; done; docker network create -d bridge docker_gwbridge 1>&/dev/null)
[node1] (local) root@192.168.0.13 ~
$

接続できた。

コマンドを入力するときは$の後に入れる。

(参考)PWDのインスタンスを切断する場合

PWDのインスタンスを切断するときは、PowerShellで
$ exit
を入力する。(今はしなくていい)

$ exit
Connection to direct.labs.play-with-docker.com closed.
PS C:\Users\momoz>

Dockerを使ってみる

日付の取得

たとえば、日付を取得してみる。

$ dateを使う。

$ date
Fri Mar 21 07:18:50 UTC 2025
[node1] (local) root@192.168.0.13 ~
$

日付と時刻が表示された

Dockerのコマンド

書式は以下の通り。

「Docker コマンド (オプション) 対象 (引数)」

Dockerのバージョン確認

$ docker version

$ docker version
Client:
 Version:           27.3.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        ce12230
 Built:             Fri Sep 20 11:39:44 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       41ca978
  Built:            Fri Sep 20 11:41:02 2024
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.7.22
  GitCommit:        hoge
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[node1] (local) root@192.168.0.13 ~

Linuxが使われているのがわかる。

コンテナの起動

hello-worldというdockerのコンテナを起動してみる。

$ docker container run hello-worldと入力する。

$ docker run hello-worldと省略してもよい

$ docker container run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:hoge
Status: Downloaded newer image for hello-world:latest

コンテナの停止

「docker stop コンテナ名」

ダウンロードしたイメージを表示

  • イメージ: コンテナを実行するために必要なファイルと設定をまとめたパッケージ

$ docker imagesと入力する。

$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    74cc54e27dc4   8 weeks ago   10.1kB
[node1] (local) root@192.168.0.13 ~

hello-worldというimageがあることがわかる

$ docker image lsでも同じ

$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    74cc54e27dc4   8 weeks ago   10.1kB
[node1] (local) root@192.168.0.13 ~
$

起動中のコンテナを確認

$ docker container ps
又は
$ docker ps

$ docker container ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[node1] (local) root@192.168.0.13 ~
$ docker  ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[node1] (local) root@192.168.0.13 ~
$

何も起動していないことがわかる。

Linuxのdockerを起動

ここでは、Ubuntuを起動してみる。UbuntuはWSLでもよく利用されるディストリビューションである。

$ docker run -it ubuntu bashと入力すると、Ubuntuの管理者ユーザー(root)としてログインできる。パスワードは設定されていない。

$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5a7813e071bf: Pull complete
Digest: sha256:省略
Status: Downloaded newer image for ubuntu:latest
root@bb3a8e1614b2:/#

rootとしてLinuxにログインしているときは、#の後にコマンドを入力する。

Ubuntuからログアウトする

# exitでログアウトできる。

root@bb3a8e1614b2:/# exit
exit

LinuxをログアウトしてDockerの表示に戻る。

その他

コンテナの削除

$ docker rm CONTAINER_ID

現在のコンテナの一覧

$ docker ps -a

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
6f6135270c5a   ubuntu        "bash"     13 seconds ago   Exited (0) 11 seconds ago             hungry_kepler
bb3a8e1614b2   ubuntu        "bash"     14 minutes ago   Exited (0) 12 minutes ago             fervent_euclid
44554272a30e   hello-world   "/hello"   22 minutes ago   Exited (0) 22 minutes ago             clever_blackwell
82a014054cb3   hello-world   "/hello"   38 minutes ago   Exited (0) 38 minutes ago             heuristic_benz
[node1] (local) root@192.168.0.13 ~

hello-worldを1つ削除してみる

$ docker rm 82a014054cb3
82a014054cb3
[node1] (local) root@192.168.0.13 ~
$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
6f6135270c5a   ubuntu        "bash"     About a minute ago   Exited (0) About a minute ago             hungry_kepler
bb3a8e1614b2   ubuntu        "bash"     15 minutes ago       Exited (0) 14 minutes ago                 fervent_euclid
44554272a30e   hello-world   "/hello"   23 minutes ago       Exited (0) 23 minutes ago                 clever_blackwell
[node1] (local) root@192.168.0.13 ~

次回は、Ubuntuをログアウトして再度ログインするやり方について述べる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?