LoginSignup
32
37

More than 5 years have passed since last update.

Dockerをはじめからていねいに〜インストールとcentos起動・停止〜

Last updated at Posted at 2018-09-29

Dockerをはじめからていねいに

いまさらですがDockerさわってみました。
基礎知識ゼロからスタートです。

今回は以下を行います。

  • Dockerのインストール
  • centosコンテナイメージの起動
  • コンテナ確認
  • centosでのコマンド実行
  • コンテナ削除

目的は、Dockerのはじめの部分をさわってみてイメージをつかむこと。

Dockerのインストール

まずはインストールから始めていきます!

$ yum install -y docker

$ service docker start

$ service docker status
docker (pid  7438) is running...

dockerがインストールできたら、サービスを起動して確認します。
osによって異なりますが、今回はLinuxで実行しています。

Docker Hubにログイン

Dockerのイメージを落としてくるために、Dockerhubを利用します。

まずは[Dockerhub](https://hub.docker.com/) にアカウントを作成します。

アカウントが作成できたら、ターミナルでログインします。

$ docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxxxxx
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

ユーザ名とパスワードを入力できたら、ログインOKです!

Dockerイメージをダウンロードしてみる

次に、Dockerイメージをダウンロードしてみましょう。
今回はcentosを探します。

$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4756                [OK]
ansible/centos7-ansible            Ansible on Centos7                              118                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   99                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   63                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              45                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      43

いくつか表示されていますね。

Docker pull

docker pullで落としてくることができます。

$ docker pull docker.io/centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest

$ docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos                        latest              5182e96772bf        7 weeks ago         200MB
gitlab/gitlab-runner-helper   x86_64-081978aa     1d85c08aa40e        2 months ago        43.1MB
python                        3.6.5               9a58cce9b09f        3 months ago        912MB

dockerを起動する

dockerを起動してみます。
docker run コマンドを使います。

$ docker run -td --name centos6.8 docker.io/centos:centos6.8
Unable to find image 'centos:centos6.8' locally
centos6.8: Pulling from library/centos
b268bb2f98b2: Pull complete
Digest: sha256:69644693c871886a139d6c1665e48e8a50cbf6da1da159871a8c09a982b68d5a
Status: Downloaded newer image for centos:centos6.8
1ee569cf18036f0b357f876da1506e89f23b3cab1ad967f0e58b7eb3c9050e89

1秒くらいで完了します。早いですね。
以下がオプションです。

-t:コンテナに擬似端末を割り当てる(TTY)
-d:バックグラウンドで実行する
--name:わかりやすいように名前をつける

起動していることを確認

起動の確認を行います。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1ee569cf1803        centos:centos6.8    "/bin/bash"         3 minutes ago       Up 3 minutes                            centos6.8

nameやstatusのカラムをみてみましょう。

centosでOSコマンドを実行してみる

次にOSコマンドを実行していきましょう。

以下は
cat /etc/redhat-release
を実行する例です。

$ docker exec centos6.8 cat /etc/redhat-release
CentOS release 6.8 (Final)

結果が表示されましたね。

centosにログインした状態を作る

OSにログインするためには以下のように実行します。

$ docker exec -it centos6.8 /bin/bash
[root@1ee569cf1803 /]#
[root@1ee569cf1803 /]# pwd
/
[root@1ee569cf1803 /]# exit
exit

execで/bin/bashを実行することでプロンプトが変わり、ログインした状態になります。

オプションは以下です。

-i:対話的なプロンプトモード
-t:擬似端末を割り当てる(TTY)

bash実行後はプロンプトが変わり、OSコマンドが実行できます。
ここではpwdを実行しています。
ログアウトは通常通りexitでOKです。

Dockerを停止する

dockerを停止します。

$ docker stop centos6.8
centos6.8
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

stopコマンドを実行してからプロンプトが戻ってくるまで約10秒くらいでした。
docker psを実行しても何も表示されませんね。

停止中のコンテナを表示するには -aをつけます。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
1ee569cf1803        centos:centos6.8    "/bin/bash"              11 minutes ago      Exited (137) About a minute ago                       centos6.8

停止中のコンテナは、ステータスが「Exited」ですね。

再度起動する場合

再度起動して行きます。

$ docker run centos6.8
Unable to find image 'centos6.8:latest' locally
docker: Error response from daemon: pull access denied for centos6.8, repository does not exist or may require 'docker login'.
See 'docker run --help'.

$ docker start centos6.8
centos6.8

docker run は初回のみです。
2回目以降起動する場合は、docker startを使います。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1ee569cf1803        centos:centos6.8    "/bin/bash"         15 minutes ago      Up About a minute                       centos6.8

起動しました!

コンテナの削除

コンテナの削除を行ってみます。

[deploy@ip-10-0-0-116 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1ee569cf1803        centos:centos6.8    "/bin/bash"         15 minutes ago      Up About a minute                       centos6.8

[deploy@ip-10-0-0-116 ~]$ docker rm -f centos6.8
centos6.8

[deploy@ip-10-0-0-116 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[deploy@ip-10-0-0-116 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES

docker rm -f centos6.8
で削除しています。

オプションはこれです。

-f:強制的に削除する

docker ps -aでも何も表示されなくなりました。

これでDockerはじめの部分は完了です!

この内容に沿ってやっていくだけでできると思います。
はじめてやってみましたが、なんとなくイメージつかめました。

こう少し細かいところは次回以降やっていこうと思います。

32
37
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
32
37