Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose error -- panic: runtime error: index out of range [1] with length 1


I've been trying to get Docker working with Postgres and Flask and I was having issues with Postgres password and docker not being able to find my entry.sh file. This seemed to be an issue with docker not updating properly, but now after updating, I am getting "go" errors when I run docker-compose up, and I don't know what they mean.

Here's the error log:

panic: runtime error: index out of range [1] with length 1

goroutine 37 [running]:
github.com/docker/compose-cli/pkg/compose.(*convergence).ensureService(0xc00027ff20, 0x2120ba8, 0xc000454450, 0xc000240f00, 0xc0001ce7c8, 0x3, 0x0, 0x0, 0x0, 0xc000240a00, ...)
        github.com/docker/compose-cli/pkg/compose/convergence.go:222 +0x11f1
github.com/docker/compose-cli/pkg/compose.(*convergence).apply.func1(0x2120ba8, 0xc000454450, 0xc0001ce7c8, 0x3, 0x0, 0x0)
        github.com/docker/compose-cli/pkg/compose/convergence.go:99 +0x21f
github.com/docker/compose-cli/pkg/compose.run.func1(0x0, 0x0)
        github.com/docker/compose-cli/pkg/compose/dependencies.go:102 +0xa3
golang.org/x/sync/errgroup.(*Group).Go.func1(0xc000590540, 0xc000626180)
        golang.org/x/[email protected]/errgroup/errgroup.go:57 +0x59
created by golang.org/x/sync/errgroup.(*Group).Go
        golang.org/x/[email protected]/errgroup/errgroup.go:54 +0x66

Heres my docker-compose.yml:

services:
    api:
      container_name: api
      build:
        context: .
        dockerfile: api/Dockerfile.api
      image: mutcompute-api
      ports:
        - "5000:5000"
      volumes:
        - ./api:/app/
      env_file: 
        - ./api/.env
      command: gunicorn -b 0.0.0.0:5000 api:app && flask db upgrade
      # entrypoint: ["./api/entry.sh"]
      depends_on:
        - db
    client:
      container_name: client
      build:
        context: .
        dockerfile: client/Dockerfile.client
      image: mutcompute-client
      ports:
        - "3000:3000"
      volumes:
        - ./client:/app/
    email:
      container_name: email
      build:
        context: .
        dockerfile: api/Dockerfile.api
      image: mutcompute-api
      ports:
        - "8025:8025"
      command: python -m smtpd -n -c DebuggingServer 0.0.0.0:8025 
    db:
      image: postgres:13-alpine
      volumes:
        - postgres_data:/var/lib/postgresql/data
      environment:
        POSTGRES_USER: mutcompute
        POSTGRES_PASSWORD: mutcompute
        POSTGRES_DB: mutcompute_dev
      networks:
        - default
      ports:
        - 5405:5432
      restart: always
      volumes:
        - ./postgres-data:/var/lib/postgresql/data

My Dockerfile:

FROM python:slim

LABEL maintainer="Brad Alexander"

RUN useradd mutcompute

WORKDIR /app

COPY . /app/
COPY entry.sh /app/

ENV FLASK_APP api.py

RUN chown -R mutcompute:mutcompute /app && \
    # chmod +x ./entry.sh && \
    pip install -r requirements.txt

USER mutcompute

EXPOSE 5000

And a rough file tree

Project
   |-api
      |-app/
      |-Dockerfile-api
      |-entry.sh
   |-client
      |-src/
      |-Dockerfile-client
   |-Docker-compose.yml

The client is React, and was working fine before the update.

Sometimes when I edit the compose file, it starts to recreate the api container then fails like this:

⠙ Container api               Recreate                                                                                                                                                       0.2s
 ⠙ Container bb72362b9e19_api  Recreate                                                                                                                                                       0.2s
panic: runtime error: index out of range [1] with length 1
...

Hopefully this is enough information. I really appreciate any help!

like image 530
Brad Alexander Avatar asked Aug 24 '21 01:08

Brad Alexander


1 Answers

TLDR: I guess I had containers in the background that were binding ports.

Simple fix:

docker-compose up --remove-orphans // not sure if this step is necessary
docker-compose down
docker-compose up

How I actually fixed it:

I switched to a previous version without db service and was prompted to run docker-compose --remove -orphans, which didn't really do much. After searching through GitHub issues, and seeing most of the issues were related to replicas, I decided what the heck and added a replica section to the api service. While this error had nothing to do with replicas, I was able to get a different error:

WARNING: The "api" service is using the custom container name "api". Docker requires each container to have a unique name. Remove the custom name to scale the service.

for this error, I just commented out all the container_name sections in each service, and got a new error:

Error response from daemon: driver failed programming external connectivity on endpoint <...> Bind for 0.0.0.0:5000 failed: port is already allocated

Boom! this was the real issue. Searched SO for this, and found out I just needed to run docker-compose down

When I switched back to the branch with the db service, all was working!

like image 178
Brad Alexander Avatar answered Oct 21 '22 04:10

Brad Alexander