0

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 :

  1. 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 .

  1. 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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
sarra
  • 1
  • 1

1 Answers1

0

Welcome to SO ;) First of all, it must be considered you can't call CMD several times in one dockerfile due to which has been mentioned here! But as I found out you are going to run an API and then test it through a custom scenario in script.py, I recommend writing the RUN.sh file for running your Service and test script before anything else and call it at the end of dockerfile with only one CMD directive. In addition, it's best practice to use COPY instead of ADD when you're copying from the local file system to the image.

meti
  • 1,921
  • 1
  • 8
  • 15
  • Hello , thank you for the answer , i have updated the Dockerfile as below , but unfortunately i still have the same issue : always "r" is empty : `FROM datascientest/fastapi:1.0.0 ARG address ENV address = '0.0.0.0' RUN echo $address ARG port ENV port = 8000 RUN echo $port COPY server.py /home/ubuntu/my_docker_image_test1/server.py COPY server.py /home/ubuntu/server.py WORKDIR /home/ubuntu CMD python3 /home/ubuntu/server.py RUN python3 /home/ubuntu/server.py &` – sarra Sep 06 '21 at 22:15