LoginSignup
0
3

More than 5 years have passed since last update.

Docker入門

Last updated at Posted at 2018-07-29

主に「ドットインストール/Docker入門」をまとめています。

Dockerとは

  • 軽量な仮想化環境
  • 環境の違いを意識することなくアプリケーションを導入できる

環境

  • Docker version 18.06.0-ce
  • Vagrant 2.0.3
  • Ubuntu 14.04

vagrant-ubuntu-trust64でログインまで

vagrantを使ってUbuntuでログインする

vagrantboxの公式サイトhttp://www.vagrantbox.es/のリストからUbuntuを追加する
https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box」

$ vagrant box add trusty64 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

※Vagrantをinstallしてなければ公式サイトでinstall

初期化/vagrantfile編集

直下にVagrant/Dockerディレクトリ作成後、初期化

~/Vagrant/Docker $ vagrant init trusty64

vagrantfileを編集

~/Vagrant/Docker $ vi vagrantfile

「config.vm.network "private_network"」のコメントアウトを外す

vagrant立ち上げ/ubuntu-trust64にログイン

~/Vagrant/Docker $ vagrant up
~/Vagrant/Docker $ vagrant ssh

Docker install

vagrant-ubuntu-trust64にログインしている状態

Docker公式サイトの手順を元にinstall
Get Docker CE for Ubuntu

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get update

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

vagrant@vagrant-ubuntu-trusty-64:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-key fingerprint 0EBFCD88

実行で以下の出力になります

pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22

リポジトリ設定

vagrant@vagrant-ubuntu-trusty-64:~$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

DockerCEをinstallしてHello World

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get update
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install docker-ce
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run hello-world

以下のようなメッセージが表示されることを確認

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

作業の流れ

  1. OSにDockerをinstall
  2. DockerHub」からImageを検索
  3. OSのDockerにimageをdocker pull
  4. docker runで実行。Containerが作られる
  5. Containerでアプリケーションを作ったりする
  6. docker commitでImageを作る
  7. 6のImageをdocker pushで「DockerHub」にpush
  8. 他のOSで「DockerHub」からImageをdocker pull

DockerHubからImageを取得

DockerHubからImageを検索

  • DockerHubから検索する
  • terminalでsudo docker search hoge | more

Image「centos」をpull

$ sudo docker pull centos

Image一覧を確認

$ sudo docker images

Imageの詳細を確認

$ sudo docker inspect [IMAGE ID]

Imageの削除

$ sudo docker rmi [IMAGE ID]

Containerを作る

docker runでContainerを作る
centosからContainerを作って「hello world」実行

$ sudo docker run centos echo "hello world"
hello world

実行中のContainerを確認

$ sudo docker ps

動作が終了したContainerを確認

$ sudo docker ps -a

最新の5件を表示

$ sudo docker ps -a -n=5

Containerを削除

$ sudo docker rm [CONTAINER ID]

Containerを操作

  • backgroundを走らせる-d
  • freeというメモリを表示させるコマンドを3秒ごとに走らせる
$ sudo docker run -d centos free -s 3

実行中のContainerを確認

$ sudo docker ps

logを表示

$ sudo docker logs [CONTAINER ID]

プログラム実行

$ sudo docker attach --sig-proxy=false [CONTAINER ID]

ctrl Cでプログラム停止

実行中のContainerを停止

$ sudo docker stop [CONTAINER ID]

Containerを再開

$ sudo docker start [CONTAINER ID]

ContainerからImageを作る

  • interactive modeにする
  • Containerのなかにterminalを立ち上げる
$ sudo docker run -i -t centos /bin/bash

hello.txtを作成

# touch hello.text
exit

Imageにcommit

$ sudo docker commit [Container ID] hoge/hello

Image一覧で確認

$ sudo docker images

Containerで確認

$ sudo docker run -i -t hoge/hello /bin/bash

Dockerfileを使ってbuild/command作成

Image/Containerの一連の作業をスクリプトにして自動化

Dockerfile作成

$ vi Dockerfile
Dockerfile
FROM centos
MAINTAINER [NAME] <MAIL-ADDRESS>
# RUN: buildする時に実行される
RUN echo "now building..."
# CMD: runするときに実行される
CMD ["echo", "now running..."]

build

$ sudo docker build -t hoge/echo .
Sending build context to Docker daemon  14.34kB
Step 1/4 : FROM centos
.
.
.

buildされたImage確認(hoge/echo)

$ sudo docker images

Container実行

$ sudo docker run hoge/echo
now running...

Webサーバーを立ち上げる

Dockerfileで設定

$ vi Dockerfile
Dockerfile
FROM centos:centos6
MAINTAINER [NAME] <MAIL-ADDRESS>
RUN yum install -y httpd
ADD ./index.html /var/www/html/
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

index.html作成

$ vi index.html
index.html
<html>
hello from Docker!
</html>

build

$ sudo docker build -t hoge/httpd .
.
.
.
Successfully tagged hoge/httpd:latest

Container実行

$ sudo docker run -p 8080:80 -d hoge/httpd

ページ確認

vagrantfileのIPアドレスからページ確認

vagrantfile
config.vm.network "private_network", ip: "192.168.55.44"

192.168.55.44:8080
hello from Docker!と表示される

Imageをpush

Imageをpushする前にDockerHubでsignupが必要

login

$ sudo docker login

DockerHubにpush

下記のサイトを元に行いました。
Docker Hub にpushする方法

  1. DockerHubのアカウント作成
  2. DockerHubで「Create Repository」
  3. Repository名や説明文を入力
  4. Image名とDockerHubのRepository名を紐づけ

紐づけ

$ sudo docker tag hoge/httpd [DockerHubのユーザー名/リポジトリ名]

Image一覧確認

$ sudo docker images

REPOSITORYにDockerHubで作成したRepositoryを確認

push

$ sudo docker push [ユーザ名/リポジトリ名]

DockerHubのRepositoryに反映

参考サイト

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