Hi! I’m incredibly new to docker, but I genuinely have no idea where to even get started. This is my dilemma:
I have containers A, B, C, and D. A and B are both servers while C and D are clients. I need to be able to connect and communicate Server A with Clent C, or Client D when C is turned off.
Additionally, I need to be able to connect Server B with Client C and D.
A and B use phpmyadmin with MariaDB, while C and D have apache and php to run websites. I tried linking two containers using this tutorial and it works, but now I need to be able to have the above scenario.
You need to use user-defined networks for connecting and communicating between containers.
To create a user-defined network and add the containers to it, you can use the docker network create command to create the network, and the docker network connect command to connect a container to the network.
Here is an example of how you can create a network named “my-network” and add Server A, Server B, Client C and D to it:
docker network create my-network
docker run --name mariadb --network my-network -e MYSQL_ROOT_PASSWORD=password -d mariadb
docker run --name server-a --network my-network -p 8080:80 -d phpmyadmin/phpmyadmin
docker run --name server-b --network my-network -p 8081:80 -d phpmyadmin/phpmyadmin
docker run --name client-c --network my-network -d httpd
docker run --name client-d --network my-network -d httpd
This command will create a network called “my-network”, and then runs the container for each service and connect them to the “my-network”.
With the above command, all containers that are part of the “my-network” will be able to communicate with each other using the container’s name as hostname.
You can also use docker network connect command to connect a running container to a network.
Hi! Thank you so much for the explanation, it really helped my understanding! I do have another question though, If I want to only use Server A and Client C(not D), is there a method in which to do so, with Client C still being in the network.
Eg. I’d like to display a running website in localhost port 80 on client C, with Server A as the database(like mariadb.) And when Client C is shut off, It will display Client D instead while still being connected to Server A. I hope this query was understandable…? I’m still not entirely sure of the terminology myself;; Or is this query actually the answer in the last paragraph you provided?
Sorry, I have another question! Is this entire network situation possible to be in vscode to run in a dockerfiler or docker-compose.yml? How should I go about it if that’s possible? This is mainly because there are php files I’d like to run in localhost with docker…
I’m genuinely so sorry for the noob questions, I’m utterly clueless when it comes to docker
I tried doing the instructions you mentioned above, but localhost refused to connect…?
Yes, you can accomplish that by using the same user-defined network and connecting only the necessary containers to it. For example, you can create a network named “my-network” as before, and then run and connect only Server A and Client C to it, like this:
With this setup, Server A and Client C will be able to communicate with each other using the container name as hostname.
If you want to display a running website in localhost port 80 on client C, you will need to configure the web server running on the Client C container to serve the website. Also, you need to configure the web server running on the client C container to connect to the Server A container for the database.
When you want to use Client D instead, you would stop the Client C container and start the Client D container and connect it to the same network. With this approach, the Client D container will be able to communicate with the Server A container and use it as a database.
To connect and communicate Server A with Client C or Client D when C is turned off, you can use Docker Compose to define and run multiple containers as a single service.
First, you’ll need to create a docker-compose.yml file in the root directory of your project. In this file, you’ll define the services for each container (A, B, C, and D), along with their respective configurations and dependencies.
To connect Server A with Client C, you would define the service for Server A and specify that it depends on the service for Client C. Then, you would set the environment variables for Server A to connect to the service for Client C (i.e. the IP address and port of Client C’s service).
To connect Server B with Client C and D, you would similarly define the service for Server B, specify that it depends on the services for both Client C and D, and set the environment variables for Server B to connect to both of those services.
You can use the same method to link with other containers as well.
Here is an example of how you might define the services in your docker-compose.yml file:
In this example, server_a is dependent on client_c and server_b is dependent on both client_c and client_d. Then you can use docker-compose up command to start all the containers defined in the compose file and it will automatically link the dependent services.
I tried out the code and phpmyadmin/mariadb didnt work, it kept giving me the error “Error response from daemon: pull access denied for phpmyadmin/mariadb, repository does not exist or may require ‘docker login’: denied: requested access to the resource is denied” this is my current docker compose now…
I already updated the compose file with just “mariadb” as Docker image. Can you try replacing it with it?
Also, please consider the compose file as an example - server-a, server-b, client-c etc. You might need to modify it to make it work.