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.

Dockerでubuntu とMongoDBを起動する

Last updated at Posted at 2019-05-20

Dockerを理解する上での基礎コマンドなど、勉強時のメモです。

1.Dockerの起動

ubuntuイメージを起動

//ubuntuイメージ。topコマンドを実行
$docker container run -t ubuntu top

ホスト(ローカル)にイメージがダウンロードされ、Topコマンドが表示されます。
→ubuntuはインストールしていないが、ホストのカーネルを用いてコンテナが起動し、ubuntuの"top”コマンドを実行していることがわかります。

image.png

Dockerにログイン

$ docker exec -it [CONTAINER ID] bash
root@[CONTAINER ID]:/#  //コンテナへのターミナル画面が出てきます。
root@[CONTAINER ID]:/# ps  -ef //コンテナのプロセスのみが表示
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:35 pts/0    00:00:00 top
root        18     0  1 04:55 pts/1    00:00:00 bash
root        28    18  0 04:55 pts/1    00:00:00 ps -ef

※SSHではなくカーネルの機能を使って操作されている。

2.複数のコンテナ起動

nginxコンテナ起動

※Docker Hubからイメージがダウンロードされます。

$docker container run --detach --publish 8080:80 --name nginx nginx

[dockerコマンドオプション]
--detach : バックグラウンド起動

nginxにアクセス

http://localhost:8080

image.png

MongoDBコンテナ起動

$docker container run --detach --publish 8081:27017 --name mongo mongo
$docker ps //起動中のコンテナ確認
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                     NAMES
56d7b0e62ee5        mongo:3.4           "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:8081->27017/tcp   mongo
99750ad2b8a7        nginx               "nginx -g 'daemon of…"   7 minutes ago        Up 7 minutes        0.0.0.0:8080->80/tcp      nginx
d9b218aee00a        ubuntu              "top"                    34 minutes ago       Up 34 minutes                                 admiring_swanson

→複数のコンテナが起動しているよ!

コンテナの停止

$ docker ps //稼働しているコンテナの確認
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                     NAMES
56d7b0e62ee5        mongo:3.4           "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:8081->27017/tcp   mongo
99750ad2b8a7        nginx               "nginx -g 'daemon of…"   7 minutes ago        Up 7 minutes        0.0.0.0:8080->80/tcp      nginx
d9b218aee00a        ubuntu              "top"                    34 minutes ago       Up 34 minutes                                 admiring_swanson

$ docker stop 56d 997 d9b //停止コマンド
56d
997
d9b
$ docker ps //コンテナが停止
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ docker system prune //残っているプロセスなども削除
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache
Are you sure you want to continue? [y/N] y
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?