LoginSignup
0
1

More than 3 years have passed since last update.

Docker Composeでnginxサーバを実行するときに犯したアホなミス

Last updated at Posted at 2020-11-06

最初にやったこと

Docker Compose を使って、nginxを起動させる。

docker-compose.yml
version: "3"
services:
  nginx:
    image: nginx:1.19-alpine
    ports:
      - "8080:80"

docker-compose.yml を用意し、

$ docker-compose up -d
Creating network "project_default" with the default driver
Creating project_nginx_1 ... done

コンテナを作成、起動させ、

にアクセスすると、

スクリーンショット 2020-11-06 16.57.53.png

うまく表示された。

次にやったこと

Dockerfile も使って上でやったことと同じことをやってみる。

├── docker
│   └── nginx
│       └── Dockerfile
└── docker-compose.yml

ファイルをこのように配置して、

docker-compose.yml
version: "3"
services:
  nginx:
    build: ./docker/nginx/Dockerfile
    ports:
      - "8080:80"
docker/nginx/Dockerfile
FROM nginx:1.19-alpine

docker-compose.yml および Dockerfile をこのように書き、コンテナを実行すると、

$ docker-compose up -d --build
Creating network "project_default" with the default driver
Building nginx
Traceback (most recent call last):
  File "docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 67, in main
  File "compose/cli/main.py", line 126, in perform_command
  File "compose/cli/main.py", line 1070, in up
  File "compose/cli/main.py", line 1066, in up
  File "compose/project.py", line 615, in up
  File "compose/service.py", line 346, in ensure_image_exists
  File "compose/service.py", line 1125, in build
  File "site-packages/docker/api/build.py", line 148, in build
TypeError: You must specify a directory to build in path
[73736] Failed to execute script docker-compose

失敗した。

何を間違えたか

docker-compose.yml
version: "3"
services:
  nginx:
    build: ./docker/nginx # ここを間違えていた
    ports:
      - "8080:80"

間違えたのは docker-compose.ymlbuild の値。

Dockerfile のパスを書いていたので、Dockerfile を配置しているディレクトリのパスに書き直して実行すると、

$ docker-compose up -d --build
Creating network "project_default" with the default driver
Building nginx
Step 1/1 : FROM nginx:1.19-alpine
 ---> bd53a8aa5ac9

Successfully built bd53a8aa5ac9
Successfully tagged project_nginx:latest
Creating project_nginx_1 ... done

スクリーンショット 2020-11-06 16.57.53.png

成功した。

解説

build は構築時に適用するオプションを指定する。

DockerfileがあるディレクトリのパスやGitリポジトリのURLを指定するのであって、Dockerfileのパスを指定するのではない。

因みにbuildimage は同時に使えない。 ← バージョン1の情報でした。(Compose ファイル・リファレンス

バージョン1のフォーマット では、 build の使い方が異なります:

  • build: . の文字列のみ許可されています。オブジェクトは指定できません。
  • build と image は同時に使えません。指定するとエラーになります。

Compose ファイル・リファレンスより

参考

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