2
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?

More than 3 years have passed since last update.

WSL上のubuntuにDockerを入れてみる

Last updated at Posted at 2020-11-23

WSL2上のUbuntu18.04にDockerをインストールし触ってみたので、メモを残しておきます。

作業環境

Ubuntuセットアップ

WSL2上のUbuntu構築は以下の記事にまとめています。
WSL上にUbuntu18.04を構築してみた

Dockerバージョン

19.03.13

流れ

  1. Dockerインストール
  2. Dockerコマンド諸々

1.Dockerインストール

以下サイトを参考にインストールを行います。
Ubuntu 18.04 LTS に Docker をインストールする

Dockerのリポジトリを設定する

パッケージのアップデート、必要なパッケージのインストールを実行し、Docker 公式の GPG 公開鍵をインストールします。

$ sudo apt update
$ sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

Dockerインストール

最新版のDockerをインストールします。

$ sudo apt update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

インストールの確認をします。

$ sudo docker --version
$ sudo systemctl status docker

以下はProxy設定を行う場合に実施してください。

proxy設定用ファイルを作成し、必要な設定を書き込みます。

$ sudo mkdir -p /etc/systemd/system/docker.service.d
$ sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
http-proxy.conf
[Service]
  Environment="HTTP_PROXY=http://proxy.example.com:8080/"
  Environment="HTTPS_PROXY=http://proxy.example.com:8080/"

変更を適用し、設定を反映させる。最後に設定できていることを確認します。

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
$ systemctl show --property=Environment docker

設定ファイルに記載した情報が表示されれば完了です。

Dockerコマンドをsudo無しで実行するための設定を行います。

まずは現状の確認をします。

$ docker images

Permission Deniedでエラーになります。

Dockerグループに対象ユーザーを入れます。
一度対象ユーザーにログインしなおしてください。

$ sudo usermod -aG docker ${USER}
$ su - ${USER}

設定できたことを確認します。

$ docker images

エラーが起きなくなっていれば完了です。

2.Dockerコマンド諸々

Docker起動・停止・ステータスチェック

$ sudo systemctl start docker
$ sudo systemctl stop docker
$ sudo systemctl status docker

イメージ検索

$ docker search [OPTIONS] TERM

TERMには、httpd、mysqlなどを入れることで、提供されているレポジトリが表示されます。

例えばhttpdで検索してみます。

$ docker search httpd

以下のような結果が返ってきました。

NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                   The Apache HTTP Server Project                  3261                [OK]
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36
centos/httpd                                                                            33                                      [OK]
arm32v7/httpd                           The Apache HTTP Server Project                  9
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                                       [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                                       [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   1                                       [OK]
publici/httpd                           httpd:latest                                    1                                       [OK]
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser…   1
hypoport/httpd-cgi                      httpd-cgi                                       1                                       [OK]
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                                       [OK]
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent…   1                                       [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                                       [OK]
dockerpinata/httpd                                                                      0
interlutions/httpd                      httpd docker image with debian-based config …   0                                       [OK]
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FPM …   0                                       [OK]
amd64/httpd                             The Apache HTTP Server Project                  0
manasip/httpd                                                                           0
trollin/httpd                                                                           0
e2eteam/httpd                                                                           0
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                                       [OK]
itsziget/httpd24                        Extended HTTPD Docker image based on the off…   0                                       [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   0                                       [OK]
ppc64le/httpd                           The Apache HTTP Server Project                  0

The Apache HTTP Server Projectが公式でhttpdという名前のイメージを公開していることがわかります。

イメージ取得

$ docker pull [OPTIONS] NAME[:TAG|@DIGEST]

先ほど検索したhttpdのイメージを取得してみます。

$ docker pull httpd

TAGを指定しなければ、latest(最新版)がpullされます。

Using default tag: latest
latest: Pulling from library/httpd
852e50cd189d: Pull complete
67d51c33d390: Pull complete
b0ad2a3b9567: Pull complete
136f1f71f30c: Pull complete
01f8ace29294: Pull complete
Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

イメージ一覧表示

$ docker images [OPTIONS] [REPOSITORY]

先ほどpullしたイメージを見てみます。

$ docker images

以下の通り表示されます。

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              0a30f4c29d25        4 days ago          138MB

コンテナの起動

$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

先ほどのhttpdを起動してみます。

$ docker run -d -p 8080:80 httpd

docker runコマンドを実行した際に下に表示される英数字が、コンテナIDとなり、今後このコンテナを指定する際には、このIDを指定します。
オプションの解説をしておきます。
-d:バックグラウンド実行(つけなければ、プロンプトがコンテナに奪われます。ログアウトすればホスト側に戻ってきます。)
-p:ポート指定(今回はローカルポートが8080でコンテナポートが80になっています)

別の起動方法。これで起動した場合は、プロンプトがコンテナ内のプロンプトになる。

$ docker run -p 8080:80 --name test01 -it httpd /bin/bash

--name:コンテナ名を指定
-it:image名を指定
/bin/bash:ログイン時のコマンドを指定

コンテナプロセスの確認

$ docker ps [OPTIONS]

停止中のプロセスも含めて確認するには、-aオプションを付けます。
基本的に以下のコマンドを打てばいいと思います。

$ docker ps -a

実行結果は以下のように表示されます。

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS                  NAMES
cf4beeb4be04        httpd               "httpd-foreground"   3 seconds ago       Up 2 seconds                0.0.0.0:8080->80/tcp   pedantic_cohen
168d4374aa73        httpd               "/bin/bash"          7 minutes ago       Exited (0) 4 minutes ago                           test01

見方は以下の通りです。

カラム名 説明
CONTAINER ID コンテナIDの最初の12桁
IMAGE コンテナの元イメージ名
COMMAND 起動時に与えられたコマンド(起動時に末尾で指定したもの)
CREATED コンテナの作成日からの経過時間
STATUS 起動・停止のステータス
PORTS バインドしているポート(ホスト側8080:コンテナ側80)
NAMES コンテナの名前(起動時に--nameで指定したもの)

起動中コンテナへのログイン

$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

上記のcf4beeb4be04コンテナにログインしてみます。

$ docker exec -it cf4beeb4be04 /bin/bash

コンテナの停止

$ docker stop [OPTIONS] CONTAINER [CONTAINER...]

先ほど起動したコンテナを停止してみます。

$ docker stop 5b43901894115d72415d265488225b957050accffcb27ce16382c97479be1d75

停止中コンテナの起動

$ docker start [OPTIONS] CONTAINER [CONTAINER...]

先ほど停止したコンテナを起動してみます。

$ docker start 5b43901894115d72415d265488225b957050accffcb27ce16382c97479be1d75

コンテナの削除

$ docker rm [OPTIONS] CONTAINER [CONTAINER...]

test01コンテナを削除してみます。

$ docker rm test01

再度docker psコマンドを実行してみます。

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
cf4beeb4be04        httpd               "httpd-foreground"   17 minutes ago      Exited (0) 4 minutes ago                        pedantic_cohen

削除されていることがわかります。

イメージの削除

docker rmi [OPTIONS] IMAGE [IMAGE...]

先ほどのhttpdイメージを削除してみます。
docker imagesで表示されるコンテナIDを指定します。

docker rmi 0a30f4c29d25

再度docker imagesで削除されていれば完了です。

最後に

今回はUbuntu 18.04上に、Dockerをインストールし、Dockerの基本の「キ」レベルのコマンドをまとめてみました。
今後は構築をしながら気づいたことをまとめていきたいなと思います。

追加

genieが利用できなくなったので、dockerの起動方法を変更しておきます。
最初からこちらにしておけばよかったです。。。

sudo /etc/init.d/docker start

記事のsystemctlはすべて/etc/init.d/に読み替えてもらえれば実行できます。失礼いたしました。

2
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
2
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?