レジストリについて
作成したDokerイメージを共有するための場所。
デフォルトのレジストリはDockerHub(http://registry.hub.docker.com)
DockerHubにイメージをアップロードすることで他の人とたちとイメージ共有することが可能。
イメージのアップロード
- アカウントの登録
DockerHubにイメージアップロードするにはアカウント登録が必要。
Webもしくはdocker login
コマンドで登録する -
Dockerfileに
MAINTAINER
を追加
$ vi Dockerfile
FROM debian
MAINTAINER hogeo <hogehoge@example.com>
RUN apt-get update && apt-get install -y cowsay fortune
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
- イメージ再構築してアップロード
$ docker build -t hogeo/cowsay
ry
$ docker login
ユーザ名とパスワード入力する
$ docker push hogeo/cowsay
The push refers to repository [docker.io/hogeo/cowsay]
03696e38f7b1: Pushed
bbe8be1cef30: Pushed
e1df5dc88d2c: Mounted from library/debian
latest: digest: sha256:92001d62c76b567b3ac290e0309ccd26002b989aaabbce2b1b446e9b7ad8ddda size: 948
イメージのダウンロード
下記はレジストリ(DockerHub(デフォルト))のhoge/cowsayリポジトリから、latest(タグ指定なければlatestがデフォルト)というタグのついたイメージをダウンロードする。
$ docker pull hogeo/cowsay:latest
Using default tag: latest
latest: Pulling from hogeo/cowsay
c73ab1c6897b: Pull complete
50648d94825f: Pull complete
866d72f6e1fd: Pull complete
Digest: sha256:92001d62c76b567b3ac290e0309ccd26002b989aaabbce2b1b446e9b7ad8ddda
Status: Downloaded newer image for hogeo/cowsay:lates
- イメージの確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hogeo/cowsay latest 09652148b8b3 33 minutes ago 164MB
- イメージの削除
$ docker rmi 09652148b8b3
Untagged: hogeo/cowsay:latest
Untagged: hogeo/cowsay@sha256:92001d62c76b567b3ac290e0309ccd26002b989aaabbce2b1b446e9b7ad8ddda
Deleted: sha256:09652148b8b30cb7a416013f889606736b64677b770ed36b5c7e700d12060363
Deleted: sha256:6f8c54d2fc1c42dcf1027eadef6495a36fd7dfb4d397d1e2cc87e131245e6b2b
Deleted: sha256:c19c018bf0eca7d7c1f465e0fca9f40a0f199bc963f3d039f84f6b0772b7c4a7
Deleted: sha256:e1df5dc88d2cc2cd9a1b1680ec3cb92a2dc924a0205125d85da0c61083b4e87d
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
公式のイメージ使用
Nginxの公式イメージを使用してみる
検索
- 検索結果の先頭、公式(OFFICIAL)になってるものが公式イメージ
$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 8341 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1325 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 547 [OK]
ry
Nginx公式イメージダウンロード
$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
2a72cbf407d6: Pull complete
04b2d3302d48: Pull complete
e7f619103861: Pull complete
Digest: sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
Status: Downloaded newer image for nginx:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest b175e7467d66 8 days ago 109MB
Nginxコンテナ起動
- バックグランドオプション(-d)で起動する。
$ docker run --name mynginx -d nginx
01caa213f9d4a69a03c10f9a7070446d3640645ca3b083de484c421aa975ac7d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01caa213f9d4 nginx "nginx -g 'daemon of…" 16 seconds ago Up 15 seconds 80/tcp mynginx
別のコンテナから接続して確認
- centosイメージダウンロード
$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
469cfcc7a4b3: Pull complete
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Downloaded newer image for centos:latest
- centosコンテナを
--link
オプション(<名前 or id>:エイリアス)付きで起動
--link
オプションでcentos
コンテナの/etc/hostsにmynginx
コンテナを追加してくれる。
$ docker run --rm -it --link mynginx:nginx centos /bin/bash
[root@855198802cd2 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 nginx 01caa213f9d4 mynginx
172.17.0.3 855198802cd2
[root@855198802cd2 /]#
- centosコンテナからmynginxコンテナにcurlで接続(--linkで指定したエイリアス名で接続できる)
[root@855198802cd2 /]# curl http://nginx/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@855198802cd2 /]#
[root@855198802cd2 /]# exit
exit
- nginxコンテナのログを確認
$ docker logs mynginx
172.17.0.3 - - [18/Apr/2018:20:51:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"