In our
last tutorial we have seen, how a
Legacy Linking of a mongodb and a webserver can be created
Now we want to create linked containers in an isolated network. That is good if you want to keep your containers strictly seperate from other container constructs. So you can be sure that everything in your dev enviroment is on its own area.
Prerequisits
We need some things installed on the local machine.If you have our testapp and database image already installed, you can skip the next 2 sections and start with
"Create your isolated network"
Install node.js pet-app
We want to use a little pet app with node.js. It starts an express server and connects to a mongodb server "my-mongodb". The app simply lists some docs pics and some describing texts
Clone the app repository in your projectroot $ git clone https://github.com/pboethig/dockerlessons.git
$ cd dockerlessons/legacyLinking
Inspect the dockerfile
In the projectroot you find a
"node.dockerfile" this is the dockerfile for our node webserver with the node.js app in it
This is the contentFROM node:latest
MAINTAINER Peter Böthig
ENV NODE_ENV=production
ENV PORT=3000
COPY . /var/www
WORKDIR /var/www
RUN npm install
EXPOSE $PORT
ENTRYPOINT ["npm", "start"]
To explain that- Line 1 load a node baseimage
- Line 2 define a maintainer (your username in thinat case)
- Line 3 & 4 define enviromentvariables, to switch from dev to prod
- Line 5 copy our sourcecode to the container in "var/www"
- LINE 6 define a workingdirectory
- Line 7 run npm install
- Line 8 Expose the default webserverport (prod/live)
- Line 9 start the express webserver
In the follwing we use <your_docker_username>. Please make sure you have an account
Create our image from the Dockerfile
Now, that we have created our Dockerfile we can create our first image from it.
Open a terminal in the folder where your Dockerfile livesdocker build -f node.dockerfile -t <your_docker_username>/node-pet-app .
Don't forget the last point at the end of the line, otherwhis nothing will be found to image. The first load could take some seconds.
You can see you new image withdocker images
Create your mongo-database-container
Our pet app needs a mongodatabase where the pet data live. We simply use an image from the docker hiu
Pull the image and create a named containerdocker run -d --name my-mongodb mongo
This will download the mongo-image and create a container "my-mongodb". The name "my-mongodb" is important, because it's mapped to the configured databasename "mongodb" in our config/config.production.js of our pet app.
Create your isolated network
This task is pretty simple.
Create custom bridged networkdocker network create --driver bridge first_isoltated_network
Now you can check, if everything went fine:
docker network ls
This will list all networks. You should see your
"first_isolated_network"Inspect your networkdocker network inspect first_isolated_network
This will show your networkconfiguration. At the moment the "containers" object is empty.
Create our mongodb container and link it to our isolated_network
Now that we have created our
"first_isolated_network" lets add the mongodb container to it
Add mongodb to the networkdocker run -d --net=first_isolated_network --name mongodb mongo
As you can see, there is a "--net=first_private_network" attribute, wich adds the created xontainer to our network
Inspect your network again docker network inspect first_isolated_network
This will now show you your mongo db container in the containers section.
Create our node-js container and link it to our isolated_network
Now we do the same with our node-js container. The one specific this is, that we directly link the app container to the database too.
Add node app to the networkdocker run -d --net=first_isolated_network --name nodeapp
As you can see, there is a "--net=first_private_network" attribute, wich adds the created xontainer to our network
Inspect your network again docker network inspect first_isolated_network
This will now show you your
"nodeapp" and your
"mongodb" containers in the containers section of your network.
That's totaly creazy. I love that for the moment. At last we want to populate some data again.
Populate data docker exec nodeapp node dbSeeder.js
Now you can reach your app under
http://localhost:8080