Today we want to have a look on Legacy Linking. Like the name describes is this the simplest method to link containers, wich can be marked as deprecated in the future.
This methods simply runs the dependend containers with the --link attribute
Prerequisits
We need some things installed on the local machine.- docker installed
- git installed
- a dockerhub account (visit http://hub.docker.com)
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 textsClone 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 itThis is the content
FROM 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
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 lives
docker 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 with
docker 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 hiuPull the image and create a named container
docker run -d --name my-mongodb mongoThis 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 our node-app container and link it to the databasecontainer
Now that we have created our node.js app image and have our database up and running, we want to create our app server and link it to our pet database.Run and link the containers
docker run -d -p 8080:3000 --link mymongodb:mongodb --name nodeapp <your_docker_username>/node-pet-app
To explain that:
- -d -> starts container detached, so the console can be used
- -p 8080:3000 -> maps the hostport 8080 on the containerport 3000
- --link my-mongodb:mongodb -> creates a link "mongodb" from to the container "my-mongodb". So "mongodb" can be found in the network
- --name nodeapp <your_docker_username>/node-pet-app -> creates the container "nodeapp" from your app image
Now you can reach your app under http://localhost:8080
This is really great. But you don't see any pets. Thats because you dont have any data in it
Populate database with testdatas
docker exec nodeapp node dbSeeder.js
This will execute "node dbSeeder.js" inside your new container "nodeapp"
That's it. Really awesome stuff. Now your can surf to your new app and see your pets
http://localhost:8080
Keine Kommentare:
Kommentar veröffentlichen