skip to Main Content

I’m a bit new to docker and I’m messing around with it. I currently have a server being ran on port 5000 in another container. This server is being ran with express and uses JavaScript. I’m trying to send requests to that server with python. I tried using both localhost:5000 and 127.0.0.1:5000 but neither of these seems to work. What can I do? I noticed if I run the python code without docker it works perfectly fine.

Python Docker File:

FROM ubuntu:latest

RUN apt update
RUN apt install python3 -y 
RUN apt-get install -y python3-pip

WORKDIR /usr/app/src
COPY . .

RUN pip install discum
RUN pip install python-dotenv

EXPOSE 5000
CMD ["python3", "./src/index.py"]

JavaScript Docker File:

FROM node:latest

WORKDIR /usr/src/app

COPY package.json ./
RUN npm install

COPY . .
CMD ["npm", "start"]

2

Answers


  1. You could create a network between the to containers using –net look at this answer How to get Docker containers to talk to each other while running on my local host?

    Another way, and my preferred way, is to use docker-compose and create networks between your containers.

    EDIT ——-

    Sorry, I didn’t see the notification of your question. This is not a video but to get started with the basics of docker-compose I would try something like https://docs.docker.com/compose/gettingstarted/ . For networking multiple containers together you can create simple networks like this https://accesto.com/blog/docker-networks-explained-part-2/.

    Here is an example of one of the docker-compose.yml files we use.

          version: '3'
      
      # create private networks between containers
      networks:
        data:   # simple networks only need to be named
        management:
      
      volumes:
        postgres-data:
        redis-data:
      
      services:
        nginx:
          image: nginx
          ports:
            - "7001:80"
          volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
            - ../project/static:/static
          depends_on:
            - web
          command: [nginx-debug, '-g', 'daemon off;']
          networks:
            - management  # connect this container to the management network
          links:
            - "web"
      
        db:
          container_name: postgres
          image: postgres:9.6.5
          restart: always
          volumes:
            - postgres-data:/var/lib/postgresql/data/
            - ../data:/docker-entrypoint-initdb.d
          environment:
            - POSTGRES_USER=userid
            - POSTGRES_PASSWORD=mypassword
          networks:
            - data  # connect this container to the data network
      
        web:
          container_name: web1
          image: web_image
          build:
            context: .
            dockerfile: Dockerfile
          environment:
            - DEBUG=1
            - DOCKER_CONTAINER="1"
            - DJANGO_LOG_LEVEL=ERROR
          volumes:
            - ../project:/code
            - ./configuration.py:/code/project/configuration.py
          working_dir: /code
          ports:
            - "7000:7000"
          command: bash -c "python manage.py migrate --noinput && python manage.py collectstatic --no-input && python manage.py runserver 0.0.0.0:7000"
      
          networks:  # connect this container to both networks
            - data
            - management
          depends_on:
            - db
            - redis
      
        redis:
          image: redis
          volumes:
            - redis-data:/data
          networks:
            - data  # connect this container to the data network
    
    Login or Signup to reply.
  2. Use Service Name and Port is always the best.
    So if you have a docker file like the below you could use the URL http://client:5000

    version: 3.8
      services:
         client:
            image: blah
            ports:
              - 5000:5000
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search