Day 21:docker Getting Started tutorial

Source: Internet
Author: User
Tags docker fedora install node virtual environment docker run docker registry
This is a creation in Article, where the information may have evolved or changed.

Editor's note: We found an interesting series of articles, "30 days to learn 30 new technologies", is translating, one-day update, year-end package. The following is the 21st day of the content.


A few months ago, Red Hat announced a partnership with DotCloud on Docker technology. I didn't have time to learn about Docker, so today, taking advantage of this 30-day challenge, I decided to learn what Docker really is. This blog post is not about how to use Docker on OpenShift. Read the "technical thinking about OpenShift and Docker", written by Mike McGrath. You can also take a look at this stackoverflow problem and learn about the differences between Docker and OpenShift.

What is Docker?

Docker provides an envelope (envelope), or container, that can run your application. It was originally an amateur project launched by DotCloud, and was open source in the early days. It attracts a lot of attention and discussion, causing DotCloud to rename it to Docker Inc. It was originally written in the Go language, which is equivalent to a pipeline added to the LXC (LinuX containers,linux container), allowing developers to work at a higher level of concept.

Docker extends the Linux container (Linux Containers), or LXC, with a high-level API that provides a lightweight virtual environment for the process alone. Docker leverages LXC, Cgroups, and Linux's own kernel. Unlike traditional virtual machines, a Docker container does not contain a single operating system, but is run based on the functionality provided by the operating system in an existing infrastructure. Here is a StackOverflow answer, which describes in great detail all the features of Docker different from pure LXC

Docker works like a portable container engine. It packs the application and all of its dependencies into a virtual container that can run on any Linux server. This greatly improves the flexibility and portability of program operation, whether it's a need for licensing, a public cloud or a private cloud, a bare metal environment, and so on.

Docker consists of the following:
1. Docker Server Daemon (server daemon), used to manage all containers.
2. Docker command-line client for controlling the server daemon.
3. Docker Image: Find and browse Docker container images. It is also visited here to get: https://index.docker.io/

Why should I care about this?

Docker is useful because it is often difficult to move code from one machine to another. It tries to make the process of software migration more credible and automated. Docker containers can be ported to all operating systems that support running Docker.

Read this article to learn more: How the Fedora Project is embracing Docker

But I'm already using virtual machines (VMS).

Until now, the only option to reliably transplant a program is virtual machine (MACHINES,VMS). Virtual machines are now common, but virtual machines are very low-level, providing a complete operating system environment. The problem with virtual machines is that the migration is too big. They contain a lot of information that is not needed such as hardware drivers, virtual processors, network interfaces, and so on. Virtual machines also take a long time to start, and also consume a lot of memory and CPU resources.

Docker is very lightweight compared to the other. Run like a regular program. This container not only runs fast, but also creates a snapshot of the image and makes the file system fast. It can run in a virtual environment like EC2, RackSpace VMs. In fact, a better way to use Docker on MAC and Windows systems is to use Vagrant. Docker's original intention was to play a similar role as a VM, but it started faster and needed less resources.

Is it like Vagrant?

One of the questions I came across was, should I use Vagrant or Docker to create a sandbox environment for my next project? Once again, the answer is the same.

Docker is more resource-saving than Vagrant. The Vagrant provides an environment that is based on virtual Box-provided VMS. Read StackOverflow's answer to learn more.

Oh, no! Another application packaging system

When I first read the Docker packaging application, I was confused. Why do we need one more application packaging system (packaging systems)? I already packaged my Java program as a JAR or WAR. After spending some time reading about Docker, I understood the meaning of the Docker application package (application). Docker is a bridge between a virtual machine and your application package like a WAR or JAR. On the one hand, virtual machines are very heavyweight (resource-intensive), because there is something that needs to be included with the porting. On the other hand, the application code package (the application code packages) is very lightweight and does not come with information that is reliable enough to run up. Docker has a good balance between these two aspects.

In Docker, application packages (application package) imply a package that contains the application code and the required deployment environment. For example, in Java we typically package our Web application in a WAR file. This WAR file is a very simple package that contains only the code of the application. But applications require a specific deployment environment to run efficiently. Sometimes the environment for deployment and development time is different. For example, developers use the Java 7 Development program, but the deployment environment is in OpenJDK Java 6, or is developed on a MAC, but deployed on RHEL. It is also possible that there are system libraries (systems libraries) that have different effects on different applications in the development environment and the simulation Environment (staging environment). Docker solves this problem by not only packaging the application, but also packaging the application's dependent environment.

Getting Started with Docker

Use the instructions in this post to install Docker on the Fedora machine

$ vagrant up$ vagrant ssh

Then install the Docker Fedora Image:

$ sudo docker pull mattdm/fedora

The above command will download the Docker Fedora image from the https://index.docker.io/.
After installing the Docker Fedora image, we can list all the images using the following command:

$ sudo docker imagesREPOSITORY                     TAG                 IMAGE ID            CREATED             SIZEshekhargulati/node_image_007   latest              e12b3054d981        50 minutes ago      470.3 MB (virtual 601.8 MB)mattdm/fedora                         12.04               8dbd9e392a96        7 months ago        131.5 MB (virtual 131.5 MB)

The first image in the list above is the one I created earlier. It's packed with NodeJS and Express fremework. The second image is the stored Docker Fedora image.

Now, let's run a script inside the Docker container:

$ sudo docker run -t -i -p 3000 mattdm/fedora /bin/bash

After running the above command, we are in the container of Docker. We can ls list all the commands by command.

Now we create the following directory structure /home/shekhar/dev :

$ mkdir -p home/shekhar/dev$ cd home/shekhar/dev

Now, I'll install NodeJS. Run the following command to install Node on the Fedora Docker image:

$ sudo yum install npm

Next, we install the Express framework:

$ npm install express -g

After the Express framework is installed, we create a new Express program and then run it:

$ express myapp$ cd myapp$ npm install$ node app.js

The above will 3000 start the NodeJS Express program on the port.

Now open another Command Line tab, listing all the Docker processes:

$ sudo docker psCONTAINER ID        IMAGE                                 COMMAND             CREATED             STATUS              PORTS                     NAMES4a5715a915e5        mattdm/fedora   /bin/bash           5 minutes ago       Up 5 minutes        0.0.0.0:49157->3000/tcp   red_duck

You will notice that the 3000 port and the bindings on this computer 49157 . You can test the Express application by following the commands shown below curl :

$ curl 0.0.0.0:49157<!DOCTYPE html>

Now commit the mirror and push to the Docker image Registry (registry). Before you do this, you must register a docker registry through https://index.docker.io/account/signup/.

$ sudo docker commit 4a5715a915e5 shekhargulati/node_image_007$ sudo docker push shekhargulati/node_image_007

Please use your own user name and image name.

So, my first image has been uploaded to the Docker registry: https://index.docker.io/u/shekhargulati/node_image_007/

You can pull download this image using the command:

$ docker pull shekhargulati/node_image_007

That's what this is about today. Keep Feedback!

Original: Day 21:docker--the Missing Tutorial
Translation finishing: Segmentfault

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.