0
0

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 1 year has passed since last update.

Docker の実験(その7:イメージの作成)

Last updated at Posted at 2023-02-18

その6:コンテナの自動削除のつづき。

Docker イメージを作成する基本的な方法。
Dockerfile を作成して、docker build でイメージが作成できる。
Google Cloud 上の vm インスタンス(その1を参照)で作業する。
(ファイル名:Dockerfile)

FROM ubuntu
RUN apt-get update
RUN apt-get -y install nginx
CMD ["nginx", "-g", "daemon off;"]

簡単なビルド方法。

$ docker build .
Sending build context to Docker daemon  12.29kB
Step 1/4 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/4 : RUN apt-get update
 ---> Running in 5c39a3c138c0
 ...(snip)...
Step 3/4 : RUN apt-get -y install nginx
 ---> Running in 0ec5bdc68d3f
Reading package lists...
Building dependency tree...
Reading state information...
...(snip)...
Step 4/4 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in 05fcc709fd05
Removing intermediate container 05fcc709fd05
 ---> ede4075dc036
Successfully built ede4075dc036
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    ede4075dc036   3 minutes ago   175MB
nginx        latest    3f8a00f137a0   9 days ago      142MB
ubuntu       latest    58db3edaf2be   3 weeks ago     77.8MB

Google Cloud で作成していた Firewall Rule で、ポートのところを修正する(その3参照)。

Protocols and ports:Specified protocols and ports:TCP:80,8080,8081

ポート番号を 8081 にして立ち上げてみる。

$ docker container run -d -p 8081:80 ede4075dc036
aa3715e9eca099dd12b233dee5085bbad5b63b0a81be81ab244756502b476dbb

curl コマンド、webからも確認する。
vm のグローバル IP の確認方法
(参考)
https://cloud.google.com/compute/docs/instances/view-ip-address?hl=ja#gcloud

$ gcloud compute instances list
$ gcloud compute instances describe <instance-name> --zone=asia-northeast1-b --format='get(networkInterfaces[0].accessConfigs[0].natIP)'
xxx.xxx.xxx.xxx

ちなみに、プロジェクトのデフォルトゾーンに存在しているインスタンスならば、ゾーンオプションは省略できる。

$ curl -I 127.0.0.1:8081
http:<vm のグローバルIP>:8081

ubuntu イメージから、オリジナルの inex.html を持った nginx イメージを作成する。
ファイルを作成して、Dockerfile でコピーして、docker build でイメージを作る。

$ mkdir demo
$ cd demo
$ vim index.nginx-debian.html
(以下、ファイルの内容)
Welcome to Nginx Container intrajp!
$ vim Dockerfile
(以下、ファイルの内容)
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
COPY index.nginx-debian.html /var/www/html
CMD nginx -g 'daemon off;'

ビルドする。

$ docker build .
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM ubuntu
 ---> 58db3edaf2be
Step 2/5 : RUN apt-get update
 ---> Using cache
 ---> 4b3e66e5edd1
Step 3/5 : RUN apt-get -y install nginx
 ---> Using cache
 ---> 60ea0cbe0bb1
Step 4/5 : COPY index.nginx-debian.html /var/www/html
 ---> f2b014774e62
Step 5/5 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in bc84395523f5
Removing intermediate container bc84395523f5
 ---> 5e443cc82858
Successfully built 5e443cc82858

イメージが出来たかどうか確認する。

$ docker iamges
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
<none>       <none>    5e443cc82858   About a minute ago   175MB
<none>       <none>    ede4075dc036   30 minutes ago       175MB
nginx        latest    3f8a00f137a0   9 days ago           142MB
ubuntu       latest    58db3edaf2be   3 weeks ago          77.8MB

出来ていた。

Google Cloud で作成していた Firewall Rule で、ポートのところを修正する(その3参照)。

Protocols and ports:Specified protocols and ports:TCP:80,8080,8081,8082

ポート番号を 8082 にして立ち上げてみる。

$ docker container run -d -p 8082:80 --name mycustomnginx 5e4
e6e93cd9f5c5f5b3bc68e5e6fd068d5eb49585d53d46190249e8b141f1408664

$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                   NAMES
e6e93cd9f5c5   5e4            "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:8082->80/tcp, :::8082->80/tcp   mycustomnginx
4348ace32de7   ubuntu         "/bin/bash"              17 minutes ago       Up 17 minutes       0.0.0.0:80->80/tcp, :::80->80/tcp       mynginx2
aa3715e9eca0   ede4075dc036   "nginx -g 'daemon of…"   29 minutes ago       Up 29 minutes       0.0.0.0:8081->80/tcp, :::8081->80/tcp   keen_lamarr
67df4d402280   nginx          "/docker-entrypoint.…"   44 hours ago         Up 44 hours         0.0.0.0:8080->80/tcp, :::8080->80/tcp   mynginx

確認する。

$ curl 127.0.0.1:8082
Welcome to Nginx container intrajp!

webからも確認できた。
vm のグローバル IP の確認方法
(参考)
https://cloud.google.com/compute/docs/instances/view-ip-address?hl=ja#gcloud

$ gcloud compute instances list
$ gcloud compute instances describe <instance-name> --zone=asia-northeast1-b --format='get(networkInterfaces[0].accessConfigs[0].natIP)'
xxx.xxx.xxx.xxx

ちなみに、プロジェクトのデフォルトゾーンに存在しているインスタンスならば、ゾーンオプションは省略できる。

http://<vm のグローバルIP>:8082

その8:Dockerfile のオプションにつづく。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?