300-910 · Question #87
Drag and drop the code snippets from the bottom onto the boxes in the code to complete the docker-compose yml and the mdex.js files to enable network communication between the containers of a Redis in
This question tests knowledge of Docker Compose networking, requiring you to wire a Redis container and a Node.js app container so they can communicate internally while exposing the Node.js app on host port 4001.
Question
Explanation
This question tests knowledge of Docker Compose networking, requiring you to wire a Redis container and a Node.js app container so they can communicate internally while exposing the Node.js app on host port 4001.
Approach. In docker-compose.yml, both services (e.g., 'web' and 'redis') must be placed on the same named network (e.g., 'app-network') defined under the top-level 'networks' key, and the Node.js service needs a 'ports' mapping of '4001:3000' (host:container) to expose it externally. In index.js, the Redis client must connect using the Docker Compose service name ('redis') as the hostname-not 'localhost'-because Docker's internal DNS resolves service names within the shared network; e.g., redis.createClient({ host: 'redis', port: 6379 }). The Redis service does not need a ports mapping since it only needs to be reachable inside the Docker network, not from the host.
Concept tested. Docker Compose inter-container networking: service name DNS resolution, shared network definitions, and host-to-container port mapping syntax
Reference. Docker Compose documentation - Networking in Compose (docs.docker.com/compose/networking/)
Topics
Community Discussion
No community discussion yet for this question.