Docker Compose Networking Essentials Quiz Quiz

Explore key aspects of networking with Docker Compose, testing your understanding of service connectivity, network configuration, name resolution, and best practices for managing multi-container setups. Enhance your knowledge of docker-compose network features and common scenarios with this focused quiz.

  1. Default Network Behavior

    When you launch a multi-service application using docker-compose without specifying the 'networks' key, what is the default network behavior for the services?

    1. A bridge network is automatically created and all the services are attached to it.
    2. Each service is isolated on its own private network by default.
    3. Services connect using the network mode 'host' unless specified otherwise.
    4. All services are connected to the external network 'default_network' by default.

    Explanation: By default, docker-compose creates a bridge network with a unique name and attaches all declared services to it, allowing internal communication. The statement about services being isolated by default is incorrect because they share the same bridge network unless configured otherwise. The 'host' mode is not the default for Compose; it must be explicitly set. There is no external network named 'default_network' automatically used, making that option inaccurate.

  2. Accessing a Service by Name

    How can one containerized service in a Compose file connect to another service using its service name?

    1. By resolving the service name through internal DNS provided by the Compose network.
    2. By manually adding an entry for the service name in the /etc/hosts file inside the container.
    3. By assigning a static IP and using it directly in application code.
    4. By exposing the port and using the host machine's IP address to access the service.

    Explanation: Docker Compose configures an internal DNS within the user-defined network, enabling services to resolve each other's names automatically. Manually editing /etc/hosts is unnecessary and not scalable in Compose-managed networks. While static IPs can be assigned, using service names streamlines connectivity and is recommended. Accessing services via the host IP and exposed ports is only necessary for connections external to the Compose network.

  3. Specifying Multiple Networks

    What happens if a docker-compose service is connected to more than one network as defined in the 'networks' key?

    1. The service can communicate with containers on all those networks.
    2. The service can only communicate with containers on its default network.
    3. The service's connections are limited to the first network listed in the Compose file.
    4. The service will fail to start due to network conflicts.

    Explanation: When a service is connected to multiple networks via the 'networks' key, it can interact with containers available on each network, increasing flexibility. Restricting traffic to only the default or first network does not align with Compose's functioning. Services will not fail to start due to having multiple networks, making the last option wrong.

  4. Impact of 'external' Networks

    What is the effect of declaring a network as 'external: true' in the docker-compose networks section?

    1. Compose connects to a pre-existing network but does not create it.
    2. A new default bridge network will be created automatically.
    3. Compose creates the named network during the deployment.
    4. All services are restricted from accessing the external network.

    Explanation: Setting 'external: true' instructs Compose to use an already existing network rather than creating it, facilitating integration with other resources. The default bridge will not be created or used in this scenario unless specified. Compose does not form new networks when 'external: true' is set, and it also does not restrict service access to external networks; rather, it connects to them.

  5. Valid Network Scopes

    Which of the following is a valid scope for a network defined in a docker-compose file?

    1. The network is limited to the containers within the same Compose project.
    2. The network can only be used by a single container.
    3. The network automatically spans all nodes in a multi-host cluster by default.
    4. The network is accessible only to the host operating system.

    Explanation: By default, docker-compose networks are scoped to the project, connecting only the containers defined under that Compose setup. A single container-only scope doesn't take advantage of Compose networks, while spanning a multi-host cluster is only possible with advanced networking configurations not enabled by default. Network access is for containers, not typically for the host operating system directly.