見出し画像

Dockerを使ってみよう!

仮想化したUbuntuで試して見ます。

まずインストールします。

docker -v
Docker version 18.06.1-ce, build e68fc7a

と確認できればOKです。

使い方としては、公開されているイメージを使う方法と、自分でイメージを作って使う方法がありますが、最初は公開されているコンテナーイメージを使って見ましょう!


$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

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/get-started/

こんな感じで表示されていると思います。

もしくは

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

この文字列がないものが表示されていればコンテナ作成成功です。

確認して見ましょう。


$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
2a34edc64809        hello-world         "/hello"                 3 minutes ago       Exited (0) 3 minutes ago                       sharp_spence

イメージの確認をして見ましょう。

$ sudo docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE

hello-world             latest              4ab4c602aa5e        26 hours ago        1.84kB

となっていると思います。

イメージをダウンロードしてコンテナーが作られています。

次にnginxコンテナーとポート公開してみましょう。

$ sudo docker run -d -p 8080:80 --name webserver nginx

とするとnginxが起動します。

ps コマンドで確認します。ちなみにps -aコマンドは起動してないもの全部が表示されます。

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
7efd517eee78        nginx               "nginx -g 'daemon of…"   40 seconds ago      Up 39 seconds       0.0.0.0:8080->80/tcp   webserver

Webブラウザーでhttp://localhost:8080/にアクセスし、nginxのデフォルトのトップページが表示されることを確認してみましょう。

コンテナ停止します。

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

動いているコンテナはなくなりました。

この記事が気に入ったらサポートをしてみませんか?