Skip to content

Troubleshooting

Jon Froehlich edited this page Oct 27, 2023 · 6 revisions

Run Docker as root to install some packages

Sometimes, you have to run an interactive shell as root into the Docker container to install certain packages.

First, from Ubuntu (WSL2), run docker ps (e.g., jonf@jonfhome:~/makeabilitylabwebsite$ docker ps):

jonf@jonfhome:~/makeabilitylabwebsite$ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED       STATUS       PORTS                      NAMES
3a590ba5ba3c   makeabilitylabwebsite-website   "./docker-entrypoint…"   2 weeks ago   Up 8 hours   127.0.0.1:8571->8000/tcp   makeabilitylabwebsite-website-1
45385f23b4f6   postgres:16                     "docker-entrypoint.s…"   2 weeks ago   Up 8 hours   0.0.0.0:50892->5432/tcp    makeabilitylabwebsite-db-1

Then run the interactive shell as root: docker exec -it -u root 3a590ba5ba3c /bin/bash

ModuleNotFoundError: No module named...

If you receive a ModuleNotFoundError like ModuleNotFoundError: No module named 'sortedm2m_filter_horizontal_widget' then the requirements.txt is not being fully executed. You can solve this manually by running an interactive shell into the container as root and manually installing the missing module into the container.

First, open requirements.txt in the root folder of makeabilitylabwebsite. Figure out which version of the missing module to get. In this case, it's django-sortedm2m-filter-horizontal-widget==2.0.1

Second, open up an interactive shell as root docker exec -it -u root 3a590ba5ba3c /bin/bash

Third, install the missing module: pip3 install django-sortedm2m-filter-horizontal-widget==2.0.1

Here's the output for the above:

jonf@jonfhome:~/makeabilitylabwebsite$ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                      NAMES
00496813ffb5   makeabilitylabwebsite-website   "./docker-entrypoint…"   7 minutes ago   Up 7 minutes   127.0.0.1:8571->8000/tcp   makeabilitylabwebsite-website-1
fd509ce466df   postgres:16                     "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   0.0.0.0:6543->5432/tcp     makeabilitylabwebsite-db-1
jonf@jonfhome:~/makeabilitylabwebsite$ docker exec -it -u root 00496813ffb5 /bin/bash
root@00496813ffb5:/code# pip3 install django-sortedm2m-filter-horizontal-widget==2.0.1
Collecting django-sortedm2m-filter-horizontal-widget==2.0.1
  Obtaining dependency information for django-sortedm2m-filter-horizontal-widget==2.0.1 from https://files.pythonhosted.org/packages/5b/9e/169586846c827509387c0f5bfb454c385c3a7ca67d10daa2d30af8f10512/django_sortedm2m_filter_horizontal_widget-2.0.1-py3-none-any.whl.metadata
  Downloading django_sortedm2m_filter_horizontal_widget-2.0.1-py3-none-any.whl.metadata (2.0 kB)
Requirement already satisfied: django-sortedm2m<4.0.0,>=3.1.1 in /usr/local/lib/python3.11/site-packages (from django-sortedm2m-filter-horizontal-widget==2.0.1) (3.1.1)
Downloading django_sortedm2m_filter_horizontal_widget-2.0.1-py3-none-any.whl (41 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.9/41.9 kB 1.6 MB/s eta 0:00:00
Installing collected packages: django-sortedm2m-filter-horizontal-widget
Successfully installed django-sortedm2m-filter-horizontal-widget-2.0.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

[notice] A new release of pip is available: 23.2.1 -> 23.3.1
[notice] To update, run: pip install --upgrade pip

Cannot start service website: OCI runtime create failed

If you receive an error like this

jonfroehlich@jonfhome:~/git/makeabilitylabwebsite$ docker-compose up
makeabilitylabwebsite_db_1 is up-to-date
Starting makeabilitylabwebsite_website_1 ... error

ERROR: for makeabilitylabwebsite_website_1  Cannot start service website: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./docker-entrypoint.sh": permission denied: unknown

ERROR: for website  Cannot start service website: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "./docker-entrypoint.sh": permission denied: unknown
ERROR: Encountered errors while bringing up the project.

Then you need to update some permissions on your configuration files. Try:

chmod 755 docker-entrypoint.sh

standard_init_linux.go:228: exec user process caused: no such file or directory

If you receive an error like:

website_1  | standard_init_linux.go:228: exec user process caused: no such file or directory

Then the line endings in the shell script are set to CRLF rather than LF (see StackOverflow post).

To fix this, open docker-entrypoint.sh in VSCode and set the line endings to LF.

PermissionError: [Errno 13] Permission denied: '/code/media/debug.log' error on localhost

If you receive a PermissionError: [Errno 13] Permission denied: '/code/media/debug.log' error on localhost, see this StackOverflow post.

makeabilitylabwebsite-website-1  | ****************** STEP 1/5: docker-entrypoint.sh ************************
makeabilitylabwebsite-website-1  | 1. Collecting static files
makeabilitylabwebsite-website-1  | ******************************************
makeabilitylabwebsite-website-1  | Traceback (most recent call last):
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/config.py", line 563, in configure
makeabilitylabwebsite-website-1  |     handler = self.configure_handler(handlers[name])
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/config.py", line 744, in configure_handler
makeabilitylabwebsite-website-1  |     result = factory(**kwargs)
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/handlers.py", line 148, in __init__
makeabilitylabwebsite-website-1  |     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/handlers.py", line 55, in __init__
makeabilitylabwebsite-website-1  |     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1147, in __init__
makeabilitylabwebsite-website-1  |     StreamHandler.__init__(self, self._open())
makeabilitylabwebsite-website-1  |   File "/usr/local/lib/python3.8/logging/__init__.py", line 1176, in _open
makeabilitylabwebsite-website-1  |     return open(self.baseFilename, self.mode, encoding=self.encoding)
makeabilitylabwebsite-website-1  | PermissionError: [Errno 13] Permission denied: '/code/media/debug.log'

Interestingly, after fixing this by doing the following (per the instructions above)

chmod 777 media
mkdir static
chmod -R 777 static/
mkdir website/migrations
chmod -R 777 website/

I had to rerun docker build . -t make_lab and that broke things again.

So, I ended up opening up a new terminal inside the Docker container as root and resetting the directory permissions (from Stackoverflow):

You need the Docker container id to create a sudo shell:

> docker ps
jonf@jonfhome:~$ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS              PORTS                      NAMES
eee162a179fd   makeabilitylabwebsite-website   "./docker-entrypoint…"   19 minutes ago   Up About a minute   127.0.0.1:8571->8000/tcp   makeabilitylabwebsite-website-1
4eddc3e71cd8   postgres                        "docker-entrypoint.s…"   3 days ago       Up About a minute   0.0.0.0:51733->5432/tcp    makeabilitylabwebsite-db-1

Then use that container ID (in this case eee162a179fd) below:

> docker exec -it -u root eee162a179fd /bin/bash
root@c6ee57d8d824:/code# chmod -R 777 media

Then, from Linux (Ubuntu using WSL2), I had to copy over the media directory:

> docker cp media c6ee57d8d824:/code

And that changed media from drwxr-xr-x 2 root root 4096 Sep 15 23:36 media to drwxrwxrwx 2 root root 4096 Sep 15 23:36 media