Nitin Ashutosh
Nitin Ashutosh

Reputation: 131

Getting container-ips inside docker container

how can we run docker commands inside container with docker-compose? Simply I want to get IP of some other network container.

I am running three container va-server, db and api-server. All the containers are in same docker-network

Here I am providing docker-compose file below:

version: "2.3"

services:
  va-server:
    container_name: va_server
    image: nitinroxx/facesense:amd64_2022.11.28 #facesense:alpha
    runtime: nvidia
    restart: always
    mem_limit: 4G
    networks:
    - perimeter-network
  db:
    container_name: mongodb
    image: mongo:latest
    ports:
    - "27017:27017"
    restart: always
    volumes:
    - ./facesense_db:/data/db
    command: [--auth]
    networks:
    - perimeter-network
  api-server:
    container_name: api_server
    image: nitinroxx/facesense:api_amd64_2022.11.28
    ports:
    - "80:80"
    - "465:465"
    restart: always
    networks:
    - perimeter-network
  
 
networks:
  perimeter-network:
    driver: bridge
    ipam:
      config:
      - gateway: 10.16.239.1
        subnet: 10.16.239.0/24

I have install docker inside the container which giving me below permission error:

docker.errors.dockerexception: error while fetching server api version: ('connection aborted.', permissionerror(13, 'permission denied')

Upvotes: 0

Views: 357

Answers (1)

David Maze
David Maze

Reputation: 159781

...inside [a] container [...] I want to get IP of some other network container....

Docker provides an internal DNS service that can resolve container names to their Docker-internal IP addresses. From one of the containers you show, you could look up a host name like db to get the container's IP address; but in practice, this is a totally normal DNS name and all but the lowest-level networking interfaces can use those directly.

This does require that all of the containers involved be on the same Docker network. Normally Compose sets this up automatically for you; in the file you show I might delete the networks: blocks and container_name: overrides in the name of simplicity. Also see Networking in Compose in the Docker documentation.

In short:

  1. You can probably use the Compose service names va-server, db, and api-server as host names without specifically knowing their IP addresses.
  2. This probably means you never need to know the container IP addresses at all (they're usually unusable from outside Docker).
  3. If you do need an IP address from inside a container, a DNS lookup can find it.

You can't usually run docker commands from inside containers. You can't do this safely without making it possible for the container to take over the whole host. There are usually better patterns that don't tie you to the Docker stack specifically.

Upvotes: 2

Related Questions