Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to isolate docker container from the host network (docker-compose)

I have a container that running a simple socket python script that listen on his ip address. I made a network for that container with the bridge as the driver. But when I am running the container I can access it from outside of his network using the localhost:port address. I want to isolate the container from anyone that is outside of his network. Can someone help me with that?.

This is my docker-compose file:*

version: '3.5'

services:
  relayG1_1:
    container_name: relayG1_1
    image: image
    command: python3 server.py 10.1.0.5
    ports:
      - 4000:4000/tcp
    networks:
      first_network:
        ipv4_address: 10.1.0.5

networks:
  first_network:
    name: first_network
    driver: bridge
    ipam:
     config:
       - subnet: 10.1.0.0/24
         gateway: 10.1.0.1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you want to expose port on network, without being accessible from localhost you should use property expose instead of ports.

An example :


version: '3.5'

services:
  relayG1_1:
    container_name: relayG1_1
    image: image
    command: python3 server.py 10.1.0.5
    expose:
      - "4000"
    networks:
      first_network:
        ipv4_address: 10.1.0.5

networks:
  first_network:
    name: first_network
    driver: bridge
    ipam:
     config:
       - subnet: 10.1.0.0/24
         gateway: 10.1.0.1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading