1
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 5 years have passed since last update.

底辺プログラマがサーバ管理者を目指す その3 Dockerイメージの作成

Last updated at Posted at 2019-07-29

Dockerイメージの作成

今回は前回の通りhelllo-worldイメージを自作していく

イメージの作成方法

Dockerイメージを作成する方法は以下の2通りある。

1.Dockerfileで作成する方法

Dockerfileと呼ばれるスクリプトファイルを作成し、
それをDockerに実行させることによってDockerイメージが作成される

2.コンテナからイメージを作成する方法

存在するコンテナに対して変更を行いそれをイメージとして保存する。
Dockerfileでイメージを作成するのが難しい場合などこちらの方法が使用される。

どちらにするか?

基本はコンテナで作業しながらうまく行ったのをDockerfileに書くのが良さそう。
参考)効率的に安全な Dockerfile を作るには

hello-worldイメージの作成

まずはDockerfileを作成する

# Dockerfileを作成
$ touch Dockerfile

次に作成したDockerfileにコードを書いていく

Dockerfile
# ベースにするイメージの指定
FROM ubuntu

# hello world!のメッセージを表示する
CMD ["echo", "hello world!"]

あとはこれをビルドするだけ

# カレントディレクトリにあるDockerfileを元にイメージhelloを作成する
$ sudo docker image build -t hello .

# 実行結果
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM ubuntu
 ---> 3556258649b2
Step 2/2 : CMD ["echo", "hello world!"]
 ---> Using cache
 ---> 9db3c91189ab
Successfully built 9db3c91189ab
Successfully tagged hello:latest

# イメージ作成されているか確認
$ sudo docker image ls

# 実行結果
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello               latest              9db3c91189ab        26 hours ago        64.2MB
ubuntu              latest              3556258649b2        5 days ago          64.2MB
hello-world         latest              fce289e99eb9        6 months ago        1.84kB

そして正しくイメージが作成されているか確認

# 作成したhelloイメージをコンテナとして起動
$ sudo docker run hello

# 実行結果
hello world!

hello world!が無事表示された。
次回はとりあえずhttpサーバを動かしてみようと思う。

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