i am working on Docker project : we have api ( fast api ) which is working fine after pulling the image then running the the container:
docker image pull toto/fastapi:1.0.0
docker container run -p 8000:8000 toto/fastapi:1.0.0
the API is available on port 8000 of the host machine --> I would like to run the tests on that API in a container , using Dockerfile :
- I created a folder : that contains the Dockerfile and my test script :
toto_image_test1: ____
|______Dockerfile
|______script.py
Remark: when I run my script of test locally with going through the container : it is working correctly and generating the output that I am looking for .
- I have built the image related to the api test :
docker image build -f Dockerfile . -t toto_image_test1:latest
then when i run the container I face an issue: here is the container that I run :
docker container run -it -d toto_image_test1:latest
Here is the content of my Dockerfile :
FROM toto/fastapi:1.0.0
ARG address
ENV address = '0.0.0.0'
RUN echo $address
CMD ["Bash"]
ARG port
ENV port = 8000
RUN echo $port
CMD ["Bash"]
ADD script.py /home/ubuntu/toto_image_test1/script.py
COPY script.py /home/ubuntu/script.py
WORKDIR /home/ubuntu
CMD python3 /home/ubuntu/script.py
RUN python3 /home/ubuntu/script.py &
Here is the first part of my script:
import os
import requests
import json
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from time import sleep
api_port = os.environ.get('port')
print("api_port",api_port)
api_address = os.environ.get('address')
print("api_address",api_address)
pwd = os.environ.get('PWD')
print ( 'le path actuel ssssssssss',pwd)
# requête authentication
r = ''
while r == '':
try:
r = requests.get(url='http://0.0.0.0:8000/permissions'.format(address=api_address,` `port=api_port),params= {'username': 'XX','password': '****'})
print("request rrrrrrrr",r)
break
except:
print("Connection refused by the server..")
print("Let me sleep for 5 seconds")
print("ZZzzzz...")
#print("URL is ",url)
print("r is rrrrrrrrr ",r)
time.sleep(5)
print("Was a nice sleep, now let me continue...")
continue
The issue is that I always find that 'r' is always empty: the query never gives a result inside the container, however it is working fine locally.