Followers

Docker Compose with Custom Network Example

  Docker Compose allows you to define multi-container applications and specify how they communicate with each other using Docker networks. B...

 Docker Compose allows you to define multi-container applications and specify how they communicate with each other using Docker networks. Below is an example of a Docker Compose file that defines two services (containers) and sets up a custom network for them to communicate:

version: '3'

services:
  webapp:
    image: nginx:latest
    container_name: webapp-container
    ports:
      - "80:80"
    networks:
      - my-network

  database:
    image: mysql:latest
    container_name: db-container
    environment:
      MYSQL_ROOT_PASSWORD: root_password
    networks:
      - my-network

networks:
  my-network:

In this Docker Compose file:

  1. We define two services: webapp and database.

  2. The webapp service uses the official Nginx image and maps port 80 from the container to port 80 on the host machine.

  3. The database service uses the official MySQL image and sets the root password as an environment variable.

  4. We define a custom network named my-network under the networks section.

  5. Both services (webapp and database) are connected to the my-network network using the networks directive within their respective service definitions.

By placing both containers on the same custom network (my-network), they can communicate with each other using their service names as hostnames. For example, the webapp service can connect to the MySQL database using the hostname database.

To start these containers using Docker Compose, navigate to the directory containing the docker-compose.yml file and run:

docker-compose up -d

This will create and start the webapp-container and db-container containers on the my-network network, allowing them to communicate.

Inside the webapp container, you can use the hostname database to connect to the MySQL database. For example, in a configuration file or code that requires the MySQL host:

# MySQL connection settings
host: database
port: 3306
user: root
password: root_password

This setup simplifies container communication and isolates the services within the same network while keeping them separate from other containers running on the host machine.

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Docker Compose with Custom Network Example
Docker Compose with Custom Network Example
DevOpsWorld
https://www.devopsworld.co.in/2023/10/docker-compose-with-custom-network.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2023/10/docker-compose-with-custom-network.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content