Monday, August 8, 2016

Getting Started with Docker for the Node.js Developer_part2 (end)

Docker Run: Running our Ubuntu image and accessing the container
We've got our Ubuntu image (our blueprint ☺). Now let's start a new container based on our image and pass a command to it:
  1. $ docker run ubuntu /bin/echo ‘Hello world’
That should output in your terminal the message "Hello World". Well, it's pretty neat that we just started a container running a completely isolated instance of Ubuntu and executed a command, but that's not really useful.

So now, let's run a new container with Ubuntu and connect to it:
  1. $ docker run -i -t ubuntu
Note: The run command is huge (check $ docker help run) and we'll go more in-depth in the next blog post
The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container. If it worked correctly, you should be connected to a terminal inside the container showing something like this:
  1. $ root@c9989236296d:/#
Run ls -ls and see that your running commands in the root of a Ubuntu system. ☺


I think it's nice to stop for a minute and think about what we just did. This is just one of the awesome parts of containers. We just downloaded and started a container running Ubuntu. That happened (depending on your internet connection) in 5 minutes? Compare that to downloading a VM Ubuntu image and spinning up a new VM. That would probably take you around 15–30min? And then creating new VMs, stopping, rebooting, how long that would take? When you add all of those up, the time you can save using containers is enormous!

Docker Commit: Installing node, npm, express and committing the changes

Okay, now that we are inside a running Ubuntu container, let's install the tools we need to run a node application (remember that you only need to execute the part after $ root: ):
  1. $ root: apt-get update  
  2. $ root: apt-get install nodejs  
  3. $ root: apt-get install nodejs-legacy
  4. $ root: apt-get install npm
Note: We need to install nodejs-legacy to run the express-generator module
Running node -v should give you an output:
  1. $ root: node -v  
  2. v0.10.25
With node installed, we can go ahead and install the express generator module from npm:
  1. $ root: npm install -g express-generator
Now we have our container with everything we're gonna need installed in it. Let's go ahead and exit from our container:
  1. $ root: exit
When we exit our container, Docker will stop running it. We can use the $ docker ps command to list containers, so let's do:
  1. $ docker ps -a

The $ docker ps command by default only displays running containers, so we pass the -a flag so we can see our Ubuntu container we just exited.
Now we can use that container to create a new image that other people can use. We do that by using the commit command:
  1. $ docker commit -a "Your Name <youremail@email.com>" -m "node and express" CONTAINER_ID node-express:0.1
Note: Change the contents from the -a flag, and the CONTAINER_ID with the ID from your container shown in the $ docker ps -a output. You can use just the first 3/4 characters from the ID. ☺
The commit command takes a few parameters. The -a flag sets the author, you can set a message using the -m flag, and finally we reference our container ID and the name of the image we're creating, in this case node-express. We also set a tag for our image by adding the :0.1 after the image name. If we run:
  1. $ docker images
We should see:

Awesome, you just created your first Docker image!

Now let's add another tag to our newly created image. Run:
  1. $ docker tag node-express:0.1 node-express:latest
It's good practice to tag images with a specific version so people can know exactly which image they're running. Adding the latest tag helps so that other people can simply refer to your image when downloading it by its name (node-express in our case), and Docker will automatically download the latest tag version. If you run $ docker images again, you can see that there's two rows with our image, but they both have the same ID, which means they're not ocuppying any extra space in our HD. ☺

Now we can start as many containers as we want ready to go with our image! Let's remove our old container:
  1. $ docker ps -a   
  2. $ docker rm YOUR_CONTAINER_ID
Note: Remember that you can just use the ID first 3–4 characters.
And let's run a container based on our new image, connect to it using the -i -t flags, and expose port 8080 of the host (VirtualBox) as the port 3000 of the container (VM):
  1. $ docker run -i -t -p 8080:3000 node-express
Let's use the express-generator we installed to create a new Node.js app:
  1. $ root: express mynodeapp
Following the instructions in the terminal, move to the app folder, install the dependencies and start the application:
  1. $ root: cd mynodeapp  
  2. $ root: npm install  
  3. $ root: npm start
Now we have a Node.js application running inside a container, and exposing port 3000. To see our application we need to find the Boot2Docker VM IP, so open another terminal and run:
  1. $ boot2docker ip  
  2. 192.168.59.103
And remember that we actually exposed port 8080 of our container to access port 3000. So go to your browser and open:
  1. 192.168.59.103:8080
Ta-ra!



Now, you might start wondering: this is a lot of work just to have a running application! I already have my development environment, I could have done all of that in 30 seconds! Well, that's true, but in this tutorial we're running a super simple application that doesn't have many dependencies. When you are running a real project that has much more dependencies, you may require a development environment with different packages, Python, Redis, MongoDB, Postgres, Node.js or io.js, etc. There're so many things involved that can make an application running in your computer not run correctly in another machine (or in QA/Test/Production), that is the main reason why Docker is so popular. Going back to the tutorial introduction, by providing a fundamental unit (our container/lego brick) that can be executed independent of hardware, and also easily run, moved, shared, Docker absolutely changes the way we can develop, test and share applications.

Docker Push: Pushing our container image so other people can use it

Okay, now let's share our "great" Ubuntu image with node, npm, and express-generator installed so other people can also use it. Exit our running Node application and the container:
  1. # Ctrl+C to stop our node app  
  2. $ root: exit
Head over to Docker Hub and create a free account: http://hub.docker.com
After that, go back to your terminal and run:
  1. $ docker login
Now that we're logged in in the cli we can push our image to the Docker Hub. Let's first rename it and add our username to it, so just like adding a tag:
  1. $ docker tag node-express your_docker_hub_username/node-express  
  2. $ docker rmi node-express  
  3. $ docker push your_docker_hub_username/node-express
Done! Now anyone with Docker can execute:
  1. $ docker pull your_docker_hub_username/node-express
And have the exact same environment with Ubuntu, Node.js, npm and the express-generator package as the one we previously created.
Written by Heitor Tashiro Sergent

If you found this post interesting, follow and support us.
Suggest for you:

Complete Node JS Developer Course Building 5 Real World Apps

Node.js Tutorials: The Web Developer Bootcamp

Learn and Understand NodeJS

Learn Nodejs by Building 12 Projects

No comments:

Post a Comment