My開発メモ

(Docker入門3) Dockerのリポジトリを利用する

Dockerfileでイメージを構築する

まず、作成するコンテナのためのディレクトリを用意して、そこで作業する。

$ mkdir cowsay
$ cd cowsay

以下のような Dockfile を作成する。

Dockfile

FROM debian

MAINTAINER Seiichi Nukayama <billie175@gmail.com>
RUN apt-get update && apt-get install -y cowsay fortune
COPY entrypoint.sh /

ENTRYPOINT ["/entrypoint.sh"]

コンテナ内で実行すべく、以下のようなスクリプトを用意する。

entrypoint.sh

#!/bin/bash
if [ $# -eq 0 ]; then
    /usr/games/fortune | /usr/games/cowsay
  else
    /usr/games/cowsay "$@"
fi
$ chmod +x entrypoint.sh

として、実行属性をつける。

Docker Hub にリポジトリを作成し、アップする。

https://hub.docker.com/ にアクセスして、アカウントを作成する。

$ docker login

として、ログインする。

ユーザー名やパスワードを尋ねられるので、入力する。

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: <ユーザー名>
Password:

続いて、以下のようなメッセージが出力される。

WARNING! Your password will be stored unencrypted in /home/se-ichi/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

これについては、以下のところに解説がある。

証明書ストア

まあ、以下のようなメッセージが出るので、今回はそれでよしとする。

Login Succeeded

イメージを作成する。

$ docker build . -t <ユーザー名>/<リポイトリ名> .

たとえば、以下のようになる。

$ docker build . -t seiichin/cowsay .

レジストリにアップする。

$ docker push <ユーザー名>/<リポイトリ名>

たとえば、以下のようになる。

$ docker push seiichin/cowsay
The push refers to repository [docker.io/seiichin/cowsay] 7884d02f0668: Pushed 36920b73884c: Pushed 0f3a12fef684: Mounted from library/debian latest: digest: sha256:c6f98f889ccc0bce206707522841ac10d0ac3179b991f63bb797a55ed67b6e4a size: 948

これで、イメージをアップできた。

イメージの実行

今作成したイメージでの実行は、以下のようになる。

$ docker run seiichin/cowsay

あるいは、

$ docker run seiichin/cowsay Do you have a pen?

カテゴリー: Docker, memo

タグ: Docker, repository

カウント: 214