Docker Debian nc command not found
Asked Answered
N

1

26

When I build my Debian image from docker-compose, with the command $ docker-compose -f docker-compose-dev.yml build web, like so:

docker-compose-fev.yml

services:

  web:
    build:
      context: ./services/web
      dockerfile: Dockerfile-dev
    volumes:
      - './services/web:/usr/src/app'  
    ports:
      - 5001:5000
    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:postgres@web-db:5432/web_dev 
      - DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/web_test
      - SECRET_KEY=my_precious
    depends_on:  
      - web-db
      - redis

As though it appears to build all packages successfully, I'm getting:

web_1| /usr/src/app/entrypoint.sh: 5: /usr/src/app/entrypoint.sh: nc: not found

If I change #!/bin/sh to #!/bin/bash, error log changes:

web_1| /usr/src/app/entrypoint.sh: line 5: nc: command not found

Dockerfile:

FROM python:3.7-slim-buster

RUN apt-get update && apt-get -y dist-upgrade
RUN apt-get -y install build-essential libssl-dev libffi-dev libblas3 libc6 liblapack3 gcc python3-dev python3-pip cython3
RUN apt-get -y install python3-numpy python3-scipy 

# set working directory
WORKDIR /usr/src/app

COPY ./requirements.txt /usr/src/app/requirements.txt 
RUN pip3 install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["/usr/src/app/entrypoint.sh"]

entrypoint.sh

#!/bin/sh

echo "Waiting for postgres..."

while ! nc -z web-db 5432; do
  sleep 0.1
done

rm -rf celery_logs/*

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0

Note: this entrypoint configuration used to work with Alpine, and now has changed to Debian.

what am I missing?

Nickola answered 14/1, 2020 at 2:29 Comment(8)
nc: command not found is the actual errorDissension
yes. I am trying to install apt-get install -y netcat. still building.Nickola
By the way, this image works great. github.com/tiangolo/meinheld-gunicorn-flask-dockerDissension
numpy and scipy should be installed with pip, and python3 has pip already, so don't need python3-pipDissension
you mean in requirements.txt as well, or in Dockerfile?Nickola
feel free to update Dockerfile code in order to improve it, like you said. either on an answer os in the question itself. I'd approveNickola
@cricket_007 I have some other scientific packages in requirements.txt, like pandas, matplotlib, scikitlearn. do you recommend me to move numpy ans scipy there too?Nickola
Move whatever you need to run the app within requirements file, yesDissension
W
45

Update the Dockerfile and append,

RUN apt install -y netcat

It should be like,

FROM python:3.7-slim-buster


RUN apt-get update && apt-get -y dist-upgrade
RUN apt-get -y install build-essential libssl-dev libffi-dev libblas3 libc6 liblapack3 gcc python3-dev python3-pip cython3
RUN apt-get -y install python3-numpy python3-scipy 
RUN apt install -y netcat
Windrow answered 14/1, 2020 at 3:2 Comment(10)
I had done RUN apt-get update && apt-get -y dist-upgrade && apt-get install -y netcat. any different?Nickola
No difference, it would install nc! and are you still experiencing the same issue?Windrow
Its not really best practice to have multiple RUN lines like thatDissension
@cricket_007 you mean that could add additional layers to the image?Windrow
It will, yes. Also, rm the apt cacheDissension
If the apt-get update gets cached but it's a week old, the apt-get install lines can fail (both Debian and Ubuntu fairly aggressively purge non-current packages from their repositories). It's important to apt-get update && apt-get install in the same RUN command to avoid this. ("Additional layers" isn't really a significant concern on its own.)Ceramic
@Data docs.docker.com/develop/develop-images/…Dissension
@cricket_007 rm -rf /var/lib/apt/lists/* after the last RUN apt-get?Nickola
@DataGarden I think apt-get clean will do the same, you can run apt-get -s clean to do a simulation and see what are the locations will be cleared.Windrow
@AnuradhaFernando" Official Debian and Ubuntu images automatically run apt-get clean, so explicit invocation is not required.". From the link above.Nickola

© 2022 - 2024 — McMap. All rights reserved.