2
3

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 3 years have passed since last update.

【Docker】nginx起動/停止/ログ【備忘録】

Last updated at Posted at 2020-12-07

絶対忘れるので、備忘録
インストールまでの流れは下記参照のこと
macOS High SierraにDockerをインストールする。【旧バージョン】
[zsh]補完機能を有効化する[Docker]

##Docker Version

yutakaf@mi  ~/Desktop/git  docker version
Client: Docker Engine - Community
 Azure integration  0.1.15
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:41:33 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:49:27 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

##Docker でnginxを起動
--publish , -p コンテナのポートをホストに公開します
docker run

✘ yutakaf@mi  ~/Desktop/git  docker container run --publish 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
852e50cd189d: Pull complete
571d7e852307: Pull complete
addb10abd9cb: Pull complete
d20aa7ccdb77: Pull complete
8b03f1e11359: Pull complete
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

初回はダウンロードから始まり、localhostへアクセスすると
下記のようにアクセスログが発生する。

スクリーンショット 2020-12-07 23.25.16.png

172.17.0.1 - - [07/Dec/2020:08:13:01 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" "-"
2020/12/07 08:13:01 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/"
172.17.0.1 - - [07/Dec/2020:08:13:01 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" "-"

##バックグラウンド実行 --detach
--detach , -d コンテナをバックグラウンドで実行し、コンテナIDを出力します
docker container run

 yutakaf@mi  ~/Desktop/git  docker container run --publish 80:80 --detach nginx
1920f9a96bb38d5cefe54f81dddb40ccc918dff9b7a7dea40c132fa986762ea6

##コンテナの一覧を表示 ls
docker container ls

yutakaf@mi  ~/Desktop/git  docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
1920f9a96bb3        nginx               "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes        0.0.0.0:80->80/tcp   cranky_moore

##コンテナを指定して停止
docker container stop

 yutakaf@mi  ~/Desktop/git  docker container stop 1920f9a96bb3
1920f9a96bb3
 yutakaf@mi  ~/Desktop/git  docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
1920f9a96bb3        nginx               "/docker-entrypoint.…"   8 minutes ago       Exited (0) 40 seconds ago                       cranky_moore

STATUSがExitedになっていることがわかる。

##コンテナに名前をつける
--name コンテナに名前を割り当てます
docker container run

 yutakaf@mi  ~/Desktop/git  docker container run --publish 80:80 --detach --name webhost nginx
4ee13d2807b491d3d8306bb0b3e43003b7e6916fc39d87e281c671e688faa595
 yutakaf@mi  ~/Desktop/git  docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
4ee13d2807b4        nginx               "/docker-entrypoint.…"   5 seconds ago       Up 5 seconds                0.0.0.0:80->80/tcp   webhost

NAMESにwebhostと名前がついた。

##コンテナのログを取得する。
docker container logs

 yutakaf@mi  ~/Desktop/git  docker container logs webhost
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [07/Dec/2020:12:08:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" "-"

IDで指定もできる。--followもしくは-fで後追いもできる。
yutakaf@mi  ~/Desktop/git  docker container logs 4e -f

##コンテナ消去
docker container rm

 yutakaf@mi  ~/Desktop/git  docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
4ee13d2807b4        nginx               "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes                0.0.0.0:80->80/tcp   webhost
1920f9a96bb3        nginx               "/docker-entrypoint.…"   14 minutes ago      Exited (0) 6 minutes ago                         cranky_moore
e24d37aaf015        nginx               "/docker-entrypoint.…"   4 hours ago         Exited (0) 14 minutes ago                        pedantic_liskov
65b36297ac90        210daa099678        "docker-entrypoint.s…"   4 hours ago         Exited (1) 4 hours ago                           vigorous_goodall
715fd822bd82        docker101tutorial   "/docker-entrypoint.…"   2 days ago          Exited (0) 2 days ago                            docker-tutorial
 yutakaf@mi  ~/Desktop/git  docker container rm 192 e24 65b 715
192
e24
65b
715
yutakaf@mi  ~/Desktop/git  docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
4ee13d2807b4        nginx               "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   webhost
 yutakaf@mi  ~/Desktop/git 

一度に指定できる。
起動中は消去できない。-fで強制消込はできる。

 yutakaf@mi  ~/Desktop/git  docker container rm ba
Error response from daemon: You cannot remove a running container ba0ef64dd1bc94888772847bcd9419e565950c9d9af3f594f18838aa20e7b124. Stop the container before attempting removal or force remove

2020/12/15追記

##コンテナへのログイン
dockerではsshなどを使用することなく、シェル画面へ入ることができる。

起動してログイン→run
起動中の端末にログイン→exec

###docker container runでのログイン
-it から コマンドbashを入力

 yutakaf@mi  ~/Desktop/git  docker container run -it --name nginx nginx bash
root@fba5b0408dee:/# pwd
/
root@fba5b0408dee:/# ls -a
.  ..  .dockerenv  bin	boot  dev  docker-entrypoint.d	docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@fba5b0408dee:/# exit

ログアウトはexitの実行でできる。

 yutakaf@mi  ~/Desktop/git  docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS               NAMES
fba5b0408dee        nginx               "/docker-entrypoint.…"   About a minute ago   Exited (0) 3 seconds ago                       nginx
 yutakaf@mi  ~/Desktop/git 

ログアウトすると停止する。

###起動中のコンテナにログイン

 yutakaf@mi  ~/Desktop/git  docker container start nginx
nginx
 yutakaf@mi  ~/Desktop/git  docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
fba5b0408dee        nginx               "/docker-entrypoint.…"   3 minutes ago       Up 3 seconds        80/tcp              nginx
 yutakaf@mi  ~/Desktop/git 

起動後にdocker container exec
-it後にbashを選択するのを忘れない。

yutakaf@mi  ~/Desktop/git  docker container exec -it nginx bash
root@fba5b0408dee:/# pwd
/
root@fba5b0408dee:/# ls -a
.  ..  .dockerenv  bin	boot  dev  docker-entrypoint.d	docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@fba5b0408dee:/# exit
exit
 yutakaf@mi  ~/Desktop/git  docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
fba5b0408dee        nginx               "/docker-entrypoint.…"   4 minutes ago       Up About a minute   80/tcp              nginx
 yutakaf@mi  ~/Desktop/git 

###余談
-itはitオプションだと思っていたが、よくよくちゃんとみると-i,-tオプションを複合して指定しているだけだった恥ずかしい。
恥ずかしいが、どこかに日本語で書いてあれば参考にする人もいるかも知れないので、記述しておく。

 yutakaf@mi  ~/Desktop/git  docker container run --help
Usage:	docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
  -i, --interactive                    Keep STDIN open even if not attached
  -t, --tty                            Allocate a pseudo-TTY
yutakaf@mi  ~/Desktop/git  docker container exec --help
Usage:	docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
  -i, --interactive          Keep STDIN open even if not attached
  -t, --tty                  Allocate a pseudo-TTY

アタッチされていなくても STDIN を開いたままにしておき、ssh と同じように、実際の端末をシミュレートします。
という話になる。
上記Usage部分のCOMMANDに当たる場所にbashが指定される。alpinのようにbashがないものはshなどで起動ができる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?