LoginSignup
0
0

More than 1 year has passed since last update.

docker-compose コマンドでstartappを実施

Last updated at Posted at 2021-07-05

概要

・Dockerに慣れるため、djangoの環境構築を実施した時に失敗したこと調べたことを記載。

状況

Djangoの環境構築を実施。Dockerfileとdoocker-compose.ymlを以下のように記載。

Dockerfile.
FROM python:3.8-alpine

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

ADD requirements.txt /code/
RUN pip install -r requirements.txt

ADD . /code/
docker-compose.yml
version: '3' 
services:
  web:  
    build: .  
    volumes:
      - .:/code  
    ports:
      - "8000:8000"
    command: python manage.py runserver 0.0.0.0:8000

その後、django-admin startprojectを実施して下記コマンドを実施するも
「Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"manage.py\": executable file not found in $PATH": unknown」のエラー。

実施コマンド.
docker-compose run python manage.py startapp app名

解決策

正しいコマンド.
docker-compose run web python manage.py startapp app名

runコマンドを実施するときは、ymlの「services:」で定義したもので実施する必要があった。
(今回で言うとweb)
参考 : https://docs.docker.jp/compose/reference/run.html

メリット

こうすることでDockerfileに記載のある「python3.8」でmanage.pyのstartappを実施してくれる。
そのためDocker のホストのコンピューターにpythonのインストールの必要がなくなるため、開発環境に左右されずに同じ結果を保証しやすくなる。(pythonが自動or手動更新されて急にコマンドでエラーが出ることがなくなる)

参考:https://teratail.com/questions/275283

認識違いがあればコメントお願いします。

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