Anuj TBE
Anuj TBE

Reputation: 9798

Cannot start service web: OCI runtime create failed. No such file or directory

I have following Dockerfile content

FROM python:3.7-slim

# Install packages needed to run your application (not build deps):

RUN mkdir /code/
WORKDIR /code/
COPY Pipfile Pipfile.lock /code/

# Install build deps, then run `pip install`, then remove unneeded build deps all in a single step.

COPY ./src scripts /code/

EXPOSE 8000

ENTRYPOINT ["/code/entrypoint.sh"]

Removed other lines not related to the question.

My directory structure in development is

\
|- scripts
    |- entrypoint.sh
|- src
    |- # Application files
|- Dockerfile
|- docker-compose.yml

The entrypoint.sh file is in scripts/ directory and is being copied to /code/ in the docker daemon. Same is verified by executing

docker run -it my_image ls -la 

Which lists files as

drwxr-xr-x 1 root root  4096 Dec 28 19:57 .
drwxr-xr-x 1 root root  4096 Dec 28 19:59 ..
-rw-r--r-- 1 root root   545 Dec 28 19:24 Pipfile
-rw-r--r-- 1 root root 21517 Dec 28 19:24 Pipfile.lock
-rwxr-xr-x 1 root root   499 Dec 28 18:47 entrypoint.sh
-rwxr-xr-x 1 root root   540 Jan  3  2019 manage.py

But when I run the image using docker-compose.yml (docker-compose up) file with content

version: '3.7'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: my_image:latest
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
    - .:/code/

It gives error as

ERROR: for myproj_py_web_1  Cannot start service web: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/code/entrypoint.sh\": stat /code/entrypoint.sh: no such file or directory": unknown

The contents of entrypoint.sh

#!/bin/sh
set -e

if [ "x$DJANGO_MANAGE_COLLECTSTATIC" = 'xon' ]; then
  echo "Collecting static files"
  python manage.py collectstatic --noinput
  echo "Done: Collecting static files"
fi

if [ "x$DJANGO_MANAGE_MIGRATE" = 'xon' ]; then
  echo "Migrating database"
  python manage.py migrate --noinput
  echo "Done: Migrating database"
fi

exec "$@"

Upvotes: 0

Views: 2355

Answers (1)

David Maze
David Maze

Reputation: 159732

Your volumes: declaration hides the contents of /code inside the image, including the /code/entrypoint.sh script. When you launch a container Docker constructs a single command from both the entrypoint and command parts combined, so your two containers have combined commands like

/code/entrypoint.sh ls -la
/code/entrypoint.sh python manage.py runserver 0.0.0.0:8000

(Entrypoint scripts typically end with a line like exec "$@" that launches the command part to facilitate this pattern.)

In particular you have this problem because the filesystem layouts don't match; you rearrange things in your Dockerfile. If you were to run

docker run --rm -it --entrypoint /bin/bash -v $PWD:/code image

you'd see a /code/scripts/entrypoint.sh; if you ran it without the -v option you'd see /code/entrypoint.sh.

The straightforward solution to this is to delete the volumes: directive. Then Docker Compose will run the code that's actually built into the image, and you won't have this conflict.

Upvotes: 1

Related Questions