sooobus
sooobus

Reputation: 889

Install Python libraries in Docker

I need to implement a simple python application inside Docker. I followed the instruction: https://docs.docker.com/get-started/part2/#dockerfile

I run a build command like that:

sudo docker build -t sender .

My requirements.txt looks the following way:

 pika==0.11.2

And Dockerfile contains following (code from the tutorial above)

# Use an official Python runtime as a parent image
FROM python:3

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

When I run it, pip can not install pika:

 sudo docker build -t sender .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM python:3
 ---> 336d482502ab
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> 9b0ffaad3d8c
Step 3/7 : ADD . /app
 ---> Using cache
 ---> 42aa7eb4ab74
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in 24a3943a217b
Collecting pika==0.11.2 (from -r requirements.txt (line 1))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f9911830668>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pika/

I tried to install numpy instead of pika, it had the same problem. Then I googled and understood that the problem is with firewall.

Firstly I tried running like that (https://github.com/docker/compose/issues/2111):

 sudo docker build --build-arg HTTP_PROXY=$HTTP_PROXY -t sender .

Then I tried to turn off proxy:

sudo ufw disable

Also I tried to throw off requirements.txt from Dockerfile and substitute it with pip install pika.

Nothing helped.

Upvotes: 0

Views: 5210

Answers (2)

yamenk
yamenk

Reputation: 51738

A direct way to solve this issue is to build using the host network mode. This will make the container use the host newtorking stack when building:

docker build --network=host ...

Upvotes: 1

jcpaiva
jcpaiva

Reputation: 422

Are you sure docker is using the proper DNS server? Try to run docker with the following parameter: --dns 8.8.8.8.

For docker build, add a file resolv.conf to the directory of your Dockerfile with the following

nameserver 8.8.8.8
nameserver 8.8.4.4

Then change your Dockerfile to

# Use an official Python runtime as a parent image
FROM python:3

# Set the working directory to /app
WORKDIR /app

# Copy resolv.conf
ADD resolv.conf /etc/resolv.conf

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Upvotes: 0

Related Questions