Dieses Blog durchsuchen

Dienstag, 6. September 2016

ubuntu: make service rebootsave

sysv-rc-conf is an alternate option for Ubuntu. The usage is almost the same.

To install:

sudo apt-get install sysv-rc-conf

To configure apache2 to start on boot

sysv-rc-conf apache2 on
equivalent chkconfig command
chkconfig apache2 enable

To check runlevels apache2 is configured to start on

sysv-rc-conf --list apache2
equivalent chkconfig command
chkconfig --list apache2

Montag, 5. September 2016

docker: make container accessable via ssh with username / password

Normaly the containers are not accessable via ssh with username and password.

This hack will aktivate ssh for root via the container ip with username and password

Login via docker inspect <container> bash
export TERM=xterm 
nano /etc/ssh/sshd_config

PermitRootLogin without-password
change to
PermitRootLogin yes

sudo apt-get --reinstall install openssh-server openssh-client
service ssh restart
 
passwd root 
 
Type in your password.

After that you can access the container via IP and username / password








Sonntag, 4. September 2016

docker: create a docker-compose(v2) node.js-mongodb-docker-network with a litte sampleapp


In our last tutorial we have created a network with 2 containers in it,
One node.js app container with a pet app and one with our mongo db.

That worked very well, but it was a hard fight on the console. We dont want to do that every time.  Now we want to automate that with a docker-compose.yml file.

Let us start.

Prerequisits

We need some things installed on the local machine.
  • docker installed
  • git installed 
  • docker compose installed in min version 1.8.x  

If you are on ubuntu16.04 , you will need to install docker compose manually, because the official version 1.5.x is too old for us.

If you are on windows and mac, just install docker toolbox
 

Install docker-compose 1.8.0 on ubuntu

Open a terminal
$ curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Now you should be able to run:
$ docker-compose -v

Checkout our sample app

We want to use our little pet app with node.js from the last tutorials too. 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/docker-compose

 

Inspect the docker-compose.yml

In the projectroot you find a "docker-compose.yml". This is our central file to create our container enviroment. I hope that xml or json will come on stage it the future again.

This is the content
version: '2'

services:
mongodb:
image: mongo
ports:
- "mongodb:27017:27017"
networks:
- nodeapp
node:
build:
context: .
dockerfile: node.dockerfile
ports:
- "8080:3000"
networks:
- nodeapp


networks:
nodeapp:
driver: bridge
To explain that
  • Line 1 defines the version of docker-compose to use. At the moment v2 is brand new and you need docker-compose 1.6.0 minimum to read it
  • Line2 contains the services object. Here are all servicenodes attached. In our case "node" and mongodb. These are the names of the images
  • As you can see mongo is build from an "image" named "mongo"
  • The mongo-portforwarding is more or less self explaining.
  • As you can see, the "node" container is build by a a dockerfile. The dockerfile resides in context "." which means, that the buildfunction looks in the current directory for it   
  • Interresting is the network definition. Both, mongodb and node are added to a network "nodeapp" which is defined at the end of the file.
  • The network "nodeapp" itself  is defined as a bridged network.
  • So both machines are encapsolated in its own network

    That is great!


If you whish, you can have a look in the node.dockerfile, to see how the nodeapp is build from.

Compose your containernetwork


Now we want to start the rocket.

Open a console and type
docker-compose build

After some seconds you should see a success message

Up your container network
docker-compose up -d

This will start your enviroment directly.

Inspect you container
docker ps


Shows your running container prefixed with "dockercompose"

docker network ls 

shows your network "dockercompose_nodeapp"

At last we want to populate some data again.

Populate data
docker exec dockercompose_nodeapp node dbSeeder.js


Now you can reach your app under http://localhost:8080







docker: link a node.js container with a mongodb container in a isolated network

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 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
    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 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 hiu

    Pull the image and create a named container
    docker 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 network
    docker 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 network
    docker 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 network
    docker 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 network
    docker 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






    docker: link a node.js container with a mongodb container with legacylinking

    To use docker in an enviroment where you have to seperate database from webserver and webserver from contentserver you can use 2 methods in docker.
    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.

     

    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 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
    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 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 hiu

    Pull the image and create a named container
    docker 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 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




    Samstag, 3. September 2016

    docker: create a dockerimage from a dockerfile with a node.js app in it

    Let's create a node.js dockerkerimage with a dockerfile and let us start a node.js app on this container

    Prerequisits

    We need some things installed on the local machine.
    • node.js installed
    • npm installed
    • docker installed

    Install node.js sampleapp

    If you already have a node.js to pack you can leave this step

    We want to use a simple express app with a hello world text, to demonstrate that the webapp is running on our container

    Install an express and express-generator
    $ npm install -g express express-generator
    $ npm install

    Create an express project
    $ express sampleApp
    This will generate our skeleton app in the current folder.

    Create our DockerFile

    With a DockerFile we are able to describe what we want to install on the container in a easy readable configfile. If you want to see the documentation, what commands the DockerFile can execute, here is the documentation

    Now we want to to following things automated by the dockerfile. This can be managed by the following dockerfile

    Save following content in a file "Dockerfile" in your project-skeletons root
    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 Dockerfile -t <yourdockerhub-username>/node-express
    This will create your image

    You can see you new image with
    docker images


    Create a new container from your image

    Now, that we have created our image from the dockerfile we can create a new container and run our sample app

    Open a terminal in the folder where your Dockerfile lives
    docker run -d -p 8081:3000 <yourdockerhub-username>/node-express

    Now you can reach your app under http://localhost:8081

    docker: dockerize your node.js application and your dev-enviroment and deploy it to the docker cloud

    Today we want to setup a simple node.js app with express and expressgenerator and deploy it to a docker container. The goal is:
    - to have a linux container with our app up and running.
    - The sourcecode lives in mounted volume on the host, so, that we can directly

    Prerequisits

    At first, you need to have some tools installed locally on yout machine. Here they are.
    • node.js installed
    • npm installed
    • docker installed
    On ubuntu you can install it like that
    $ apt-get install nodejs
    $ apt-get install npm
    We only need node and npm installed, to create a boilerplate app. In your daily works you will only checkout your project.

    On windows and mac you can do it like that
    https://nodejs.org/en/download/

     

    Install docker

    Windows and Mac, follow the instruction under:
    https://www.docker.com/products/docker#/linux

    If you are on ubuntu you can install docker like that:
    http://magento2-tuts.blogspot.de/2016/08/install-docker-on-ubuntu-1604.html

    Install node sample app 

    We want to have our node.js sourcecode on the local machine, not in the docker container directly. So we are mounting the workingdirectory into the docker container later

    Install an express and express-generator
    $ npm install -g express express-generator

    Create an express project
    $ express sampleApp
    $ npm install
    This will generate our skeleton app. in the current folder

    Download your docker baseimage 

    To make things simple, we want to use a predefined "node.js ready" ubuntu image from dockerhub. If you dont know docker hub, take your time to create a an acocunt. you will need it later. https://hub.docker.com/

    pull your node container
    $ docker pull node 
    This will download our working image

     

    Create your appcontainer

    Now let's create our contrainer in which our node.js express app will live. We want, that we can browse the app on port 8080 and we want our sourcecode is mounted as a volume to the the docker container in dir "/var/www"

    This can be done by follwing command
    $ docker run -p 8080:3000 -v $(pwd):var/www -w "/var/www" node npm start 
    To expain that 
    • -p 8080:3000 -> the host port 8080 will be forwarded to port 3000 on the docker container, so we can reach the container under http://localhost:8080
    • -v -> we want to mount a folder on the host as a volume to the container 
    • $(pwd):var/www -> That means to mount the current dir to "var/www" in the container
    • -w "var/www" -> That means to change to "/var/www" on the container. 
    • npm start -> starts the app in "/var/www"

     

    Test your container

    Now you can surf "http://localhost:8080" and you should see your express site
    WOW. thats awesome.

    But wait. We want to test, how easy it is to edit your sourcecode.

    Go to your app directory where your code files lives and edit the index.hbm in the views folder.

    Type "Hi there" somewhere in the html code.

    Reload your browser. 

    You will see your changes directly