0

I am new in Docker and Mongodb. I have a mongodb container with a database and collection. I created another container with php and apache using following command:

docker run -d -p 8020:80 --name my-php-apache php:7-apache

In order to be able to connect to mongodb container (my-mongo), i need to link this container to mongodb container. So i used following command to link both containers:

docker run -d -p 8020:80 --link my-mongo --name php-mongo-link php:7-apache

But it shows following error:

b36e400bb5c0d229f952a7b365d5a8bfed402410bdb5a802f29fd2fdc1ef28f9

docker: Error response from daemon: driver failed programming external connectivity on endpoint php-mongo-link (ec4eb66d0a314201c99f79eb4a09cf82ffb2fc399647020adaf34932878857b7): Bind for 0.0.0.0:8020 failed: port is already allocated.

To link the containers, should the ports of php-apache (8020:80) and php-mongo-link be same ? Or should they be different ? Is there any better way to link 2 containers (php-apache and mongodb) ? I just need to connect from the php container to the mongodb container to insert some data in the mongodb database.

2
  • What's the difference between the my-php-apache and the php-mongo-link container; they both seem to be running the same image? Use bridge networks in the Docker documentation is probably what you're looking for, though it's not the most approachable explanation. Commented Nov 4, 2019 at 23:10
  • @DavidMaze my-php-apache is the name of php:7-apache container and php-mongo-link is the link connecting mongodb container with php-apache container. let me know if you have any better idea to connect the two containers Commented Nov 5, 2019 at 7:00

3 Answers 3

1

The issue is that you expose both container on the same port 8020 , so one must listen on 8021 .

extract from https://docs.docker.com/engine/reference/commandline/run/

Publish or expose port (-p, --expose)

$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. You can also specify udp and sctp ports. The Docker User Guide explains in detail how to manipulate ports in Docker.

Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't be using docker run --link at all on modern Docker. You need to create a Docker network (default settings are just fine) and then run both containers attached to that network. The containers will be able to reach each other using the --name of the container as a host name.

docker network create myapp
docker run \
  -d \                 # in the background
  --net myapp \        # attached to the network
  -v ... \             # with persistent storage
  --name my-mongodb \
  mongo:4.2
docker run \
  -d \                 # in the background
  --net myapp \        # attached to the network
  -p 8020:80 \         # accessible from outside
  --name my-php-apache \
  -e MONGO_URL=mongodb://my-mongodb:27017 \
  php:7-apache

(If you use Docker Compose, it does this setup automatically.)

Every time you docker run something it creates a new container. A link is a property of a container that allows it to connect to a specific other container, in the networking model that existed before Docker 1.0; you need to specify it when you start the first container. In your example you're running a container, then running a second copy of it with the link setting; you're getting a conflict because both containers publish the same host port (-p 8020:...). If you were to use --link it would be on the first docker run command, and you wouldn't need a second one.

4 Comments

i used bridge network, but still unable to connect with mongodb container from php container
from my php container, i am running a php script to connect with mondb container like : $mongoClient = new MongoDB\Driver\Manager('mongodb://my-mongo:27017'); But, var_dump($mongoClient) shows following- object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(24) "mongodb://my-mongo:27017" ["cluster"]=> array(0) { } }
does it mean it's connected with mongodb ?
can you have a look at this question please ? stackoverflow.com/questions/58685240/…
0

You hand bind both containers to the same port on your host maschine. Both are bind to port 80. You should use another port for the second container like 81.

docker run -d -p 8020:81 --link my-mongo --name php-mongo-link php:7-apache

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.