1
2

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.

Docker Dockerfile

Last updated at Posted at 2019-03-14

1.命令##

命令 説明
FROM ベースイメージの指定
MAINTAINER 作成者情報を設定
RUN ベースイメージから起動したコンテナ内で実行するコマンドを設定
ENV 環境変数を設定
WORKDIR 場所(ディレクトリ)を移動
USER ユーザ変更設定
LABEL メタ情報(バージョンやコメントなど)設定
EXPOSE 公開ポート番号設定
ADD ファイルやディレクトリを取得(リモート可)
COPY ファイルやディレクトリを取得(ローカルのみ)
VOLUME ボリューム設定
ONBUILD 次のbuild時に実行されるコマンドを設定
CMD 作成したイメージが起動されたら実行するコマンドを設定
ENTRYPOINT 作成したイメージが起動されたら実行するコマンドを設定

2.Dockerfileを作成する##

Dockerfile
FROM centos

LABEL title="test1"\
      version="1.0"\
      description="This is a test1"

RUN mkdir /test1
RUN echo "hello world" > /test1/aaa
VOLUME /test1

ENV hoge=hogehoge

EXPOSE 80

WORKDIR /tmp
RUN ["pwd"]

ADD https://github.com/docker/cli/blob/master/README.md /tmp

COPY sample.txt /tmp

3.Dockerfile からイメージを作成する##

作成された Dockerfile を実行。

$ docker image build -f Dockerfile .

1つずつ実行され完了。
image.png

イメージが作成されたことを確認。

$ docker images

image.png

ちょっと名前を付け忘れたので、tag をつけておく。

$ docker images tag 0025fec43609 test:1

メタ情報が反映されているか確認してみる。

$ docker image inspect test:1

4.docker を実行する##

作成したイメージで docker を実行してみる。

$ docker container run -it test:1

5.内容を確認する##

起動したDockerからひとつずつイメージの内容と合致しているか確認してみましょう。

1.WORKDIR /tmp から開始されていることがわかります。
image.png

2.FROM centos かを確認する。

$ cat /etc/redhat-release

3./test1/aaa で hello world が表示されているか確認する。

$ cat /test1/aaa

4.環境変数が登録されているか確認する。

$ printenv | grep hoge

image.png

5./tmp にGitHubのファイルが保存されているか確認する。

$ docker ls /tmp

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?