Skip to content

Internal Ports & Network

Iva edited this page Aug 23, 2024 · 3 revisions
  • Configuration Refinement (Internal Ports and Network)

    • Add the definition of internal port exposure, exposing port 3306 of the mariadb container for access by the WordPress container, and exposing port 9000 of the wordpress container for access by the NGINX container, as well as a specific bridge type network definition, which is a common, secure, and easy-to-configure network type in the docker-compose.yml file:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          ports:
            - "443:443"
          volumes:
            - .web:/var/www/html
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          expose:
              - "9000"
          volumes:
             - .web:/var/www/html
        mariadb:
          build: requirements/mariadb/.
          container_name: mariadb
          expose:
              - "3306"
    • Add the network definition to the docker-compose.yml file, which if not defined, will be automatically created by the compose, and by default is configured with a bridge type driver, a common, secure, and easy-to-configure network type, and include all services/containers in this network as shown below:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          networks:
            - inception
          ports:
            - "443:443"
          volumes:
            - .web:/var/www/html
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          networks:
            - inception
          expose:
            - "9000"
          volumes:
            - .web:/var/www/html
        mariadb:
          build: requirements/mariadb/.
          container_name: mariadb
          networks:
            - inception
          expose:
            - "3306"
      networks:
        inception:
          driver: bridge

      👉🏼 The internal port exposure done through the docker-compose.yml file as above is sufficient for internal functionality. However, it is a good practice to add the port exposure to each container's Dockerfile with the EXPOSE directive followed by the port number to be exposed.

      👉🏼 At this stage, you can test the host connection functionality on defined ports by rebuilding the images and starting all the containers. Then, run the port tests provided in the repository readme.

    • Add the definition to automatically restart the container in case of execution failure, and a name followed by the version to be given to each server's docker image, in the docker-compose.yml file, as shown below:

      version: "3.8"
      
      services:
        nginx:
          image: nginx:v1.0
          build: requirements/nginx/.
          container_name: nginx
          restart: on-failure
          networks:
            - inception
          ports:
            - "443:443"
          volumes:
            - .web:/var/www/html
        wordpress:
          image: wordpress:v1.0
          build: requirements/wordpress/.
          container_name: wordpress
          restart: on-failure
          networks:
            - inception
          expose:
            - "9000"
          volumes:
            - .web:/var/www/html
        mariadb:
          image: mariadb:v1.0
          build: requirements/mariadb/.
          container_name: mariadb
          restart: on-failure
          networks:
            - inception
          expose:
            - "3306"
      networks:
        inception:
          driver: bridge

    ⏮️ Previous
    Next ⏭️

Clone this wiki locally