0

I have a java application, that connects through external database through custom docker network and I want to connect a Redis container.

docker-redis github topic

I tried the following on the application config:

1 localhost:6379
2 app_redis://app_redis:6379 
3 redis://app_redis:6379 

nothing works on my setup

docker network setup:

    docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 mynet

Connect to a Database Running on Your Docker Host

PS: this might be off-topic, how I can add the network on docker-compose instead of external

docker-compose:

    services:
        app-kotin:
            build: ./app
            container_name: app_server
            restart: always
            working_dir: /app
            command: java -jar app-server.jar
            ports:
                - 3001:3001
            links:
              - app-redis
            networks: 
                - front
        app-redis:
            image: redis:5.0.9-alpine
            container_name: app-redis
            expose:
                - 6379 

    networks:    
        front:        
            external:
                name: mynet

with the setup above how can I connect through a Redis container?

1 Answer 1

1

Both containers need to be on the same Docker network to communicate with each other. The app-kotin container is on the front network, but the app-redis container doesn't have a networks: block and so goes onto an automatically-created default network.

The simplest fix from what you have is to also put the app-redis container on to the same network:

    app-redis:
        image: redis:5.0.9-alpine
        networks:
            - front

The Compose service name app-redis will then be usable as a host name, from other containers on the same network.

You can simplify this setup considerably. You don't generally need to manually specify IP configuration for the Docker-private networks. Compose can create the network for you, and in fact it will create a network named default for you. (Networking in Compose discusses this further.) links: and expose: aren't used in modern Docker networking; Compose can provide a default container_name: for you; and you don't need to repeat the working_dir: or command: from the image. Removing all of that would leave you with:

version: '3'
services:
  app-kotin:
    build: ./app
    restart: always
    ports:
      - '3001:3001'
  app-redis:
    image: redis:5.0.9-alpine

The server container will be able to use the other container's Compose service name app-redis as a host name, even with this minimal configuration.

Sign up to request clarification or add additional context in comments.

2 Comments

adding redis service to the same network. it works now. and using the service name as the host-name in the app configuration but using the default network created by docker-compose, connecting to host-container db isnt working. can you show the right way to configure a network, instead of using externally created docker network, I configure it using docker-compose? thanks, greatly appreciated.
If you have a database running on the non-Compose mynet network then you do need a top-level networks: block like you originally had pointing at it, but you can use that block to configure the default network instead of creating a new network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.