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:
- $ docker run ubuntu /bin/echo ‘Hello world’
So now, let's run a new container with Ubuntu and connect to it:
- $ 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 postThe -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:
- $ root@c9989236296d:/#
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: ):
- $ root: apt-get update
- $ root: apt-get install nodejs
- $ root: apt-get install nodejs-legacy
- $ root: apt-get install npm
Note: We need to install nodejs-legacy to run the express-generator moduleRunning node -v should give you an output:
- $ root: node -v
- v0.10.25
- $ root: npm install -g express-generator
- $ root: exit
- $ 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:
- $ 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:
- $ 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:
- $ 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:
- $ docker ps -a
- $ 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):
- $ docker run -i -t -p 8080:3000 node-express
Let's use the express-generator we installed to create a new Node.js app:
- $ root: express mynodeapp
Following the instructions in the terminal, move to the app folder, install the dependencies and start the application:
- $ root: cd mynodeapp
- $ root: npm install
- $ 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:
- $ boot2docker ip
- 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:
- 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:
- # Ctrl+C to stop our node app
- $ root: exit
After that, go back to your terminal and run:
- $ docker login
- $ docker tag node-express your_docker_hub_username/node-express
- $ docker rmi node-express
- $ docker push your_docker_hub_username/node-express
- $ docker pull your_docker_hub_username/node-express
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