3 Linking to Docker Network

In order for a set of software to be able to work together, we need to link the different Docker containers to the same 'Docker netowork'.

1) First, lets create a Docker network and then check that it has been created: docker netowrk create my-network docker network ps


2) Now we can link our MongoDB container to the network as we run the container:

docker run -d -p 27017:27017 --name my-mongo --network my-net mongo -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=pass

where:
-d means detached, ie. running in the background,
-p denotes the (host port):(container port)
--name denotes the alias name of the container,
--net denotes the docker network to link.
-e denotes the environment variable,

3) Lets run another container for mongo-express, a service which allows you to inspect the contents of the mongo database on a browser.

docker run -d -p 8081:8081 --name my-mongo-express --net my-network --name my-mongo-express -e ME_CONFIG_OPTIONS_EDITORTHEME="ambiance" -e ME_CONFIG_MONGODB_SERVER="my-mongo" -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=pass mongo-express

Be sure to use the flags for the admin account rather than the normal account.

On a browser, enter the address: localhost:8081. You should see that mongo-express is running:




On a Windows or Mac machine, you should also be to see that the containers are running: