|
| 1 | +## Contents |
| 2 | + |
| 3 | +**Docker** |
| 4 | + - [What is docker?](#what-is-docker) |
| 5 | + - [Creating a web server](#creating-a-web-server) |
| 6 | + - [Building docker image](#building-docker-image) |
| 7 | + - [Getting docker images](#getting-docker-images) |
| 8 | + - [Running the container image](#running-the-container-image) |
| 9 | + - [Accessing your application](#accessing-your-application) |
| 10 | + - [Listing all your running containers](#listing-all-your-running-containers) |
| 11 | + - [Running a shell inside an existing container](#running-a-shell-inside-an-existing-container) |
| 12 | + - [Exploring container from within](#exploring-container-from-within) |
| 13 | + - [Stopping and removing a container](#stopping-and-removing-a-container) |
| 14 | + - [Pushing the image to an image registry](#pushing-the-image-to-an-image-registry) |
| 15 | + - [Pushing image to docker hub](#pushing-image-to-docker-hub) |
| 16 | + |
| 17 | +#### What is docker |
| 18 | + |
| 19 | +Docker is a platform for packaging, distribution and running applications. It allows you to package your application together with its whole environment. This can be either a few libraries that the app requires or even all the files that are usually available on the filesystem of an installed operating system. Docker makes it possible to transfer this package to a central repository from which it can then be transferred to any computer running Docker and executed there |
| 20 | + |
| 21 | +Three main concepts in Docker comprise this scenario: |
| 22 | +- **Images** :— A Docker based container image is something you package your application and its environment. It contains the filesystem that will be available to the application and other metadata, such as the path to the executable that should be executed when the image is run. |
| 23 | +- **Registries** :- A Docker Registry is a repository that stores your Docker images and facilitates easy sharing of those images between different people and computers. When you build your image, you can either run it on the computer you’ve built it on, or you can push (upload) the image to a registry and then pull (download) it on another computer and run it there. Certain registries are public, allowing anyone to pull images from it, while others are private, only accessible to certain people or machines. |
| 24 | +- **Containers** :- A Docker-based container is a regular Linux container created from a Docker-based container image. A running container is a process running on the host running Docker, but it’s completely isolated from both the host and all other processes running on it. The process is also resource-constrained, meaning it can only access and use the number of resources (CPU, RAM, and so on) that are allocated to it. |
| 25 | + |
| 26 | +## Learning while working |
| 27 | + |
| 28 | +#### Creating a web server |
| 29 | + |
| 30 | +You first need to create a container image. We will use docker for that. We are creating a simple web server to see how kubernetes works. |
| 31 | + |
| 32 | +- create a file `app.js` and copy this code into it |
| 33 | + |
| 34 | +```js |
| 35 | +const http = require('http'); |
| 36 | +const os = require('os'); |
| 37 | +console.log("Kubia server starting..."); |
| 38 | +var handler = function (request, response) { |
| 39 | + console.log("Received request from " + request.connection.remoteAddress); |
| 40 | + response.writeHead(200); |
| 41 | + response.end("You've hit " + os.hostname() + "\n"); |
| 42 | +}; |
| 43 | +var www = http.createServer(handler); |
| 44 | +www.listen(8080); |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +Now we will create a docker file that will run on a cluster when we create a docker image. |
| 49 | + |
| 50 | +- create a file named `Dockerfile` and copy this code into it. |
| 51 | + |
| 52 | +```Dockerfile |
| 53 | +FROM node:8 |
| 54 | + |
| 55 | +RUN npm i |
| 56 | + |
| 57 | +ADD app.js /app.js |
| 58 | + |
| 59 | +ENTRYPOINT [ "node", "app.js" ] |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +#### Building docker image |
| 64 | + |
| 65 | +Make sure your **docker server is up and running**. Now we will create a docker image in our local machine. Open your terminal in the current project's folder and run |
| 66 | + |
| 67 | +`docker build -t kubia .` |
| 68 | + |
| 69 | +You’re telling Docker to build an image called **kubia** based on the contents of the current directory (note the dot at the end of the build command). Docker will look for the Dockerfile in the directory and build the image based on the instructions in the file. |
| 70 | + |
| 71 | +Now check your docker image created by running |
| 72 | + |
| 73 | +#### Getting docker images |
| 74 | + |
| 75 | +`docker images` |
| 76 | + |
| 77 | +This command lists all the images. |
| 78 | + |
| 79 | +#### Running the container image |
| 80 | + |
| 81 | +`docker run --name kubia-container -p 8080:8080 -d kubia` |
| 82 | + |
| 83 | +This tells Docker to run a new container called **kubia-container** from the kubia image. The container will be detached from the console (-d flag), which means it will run in the background. Port 8080 on the local machine will be mapped to port 8080 inside the container (-p 8080:8080 option), so you can access the app through [localhost](http://localhost:8080). |
| 84 | + |
| 85 | +#### Accessing your application |
| 86 | + |
| 87 | +Run in your terminal |
| 88 | + |
| 89 | +`curl localhost:8080` |
| 90 | +> You’ve hit 44d76963e8e1 |
| 91 | +
|
| 92 | +#### Listing all your running containers |
| 93 | + |
| 94 | +You can list all your running containers by this command. |
| 95 | + |
| 96 | +`docker ps` |
| 97 | + |
| 98 | +The `docker ps` command only shows the most basic information about the containers. |
| 99 | + |
| 100 | +Also to get additional information about a container run this command |
| 101 | + |
| 102 | +`docker inspect kubia-container` |
| 103 | + |
| 104 | +You can see all the container by |
| 105 | + |
| 106 | +`docker ps -a` |
| 107 | + |
| 108 | +### Running a shell inside an existing container |
| 109 | + |
| 110 | +The Node.js image on which you’ve based your image contains the bash shell, so you can run the shell inside the container like this: |
| 111 | + |
| 112 | +`docker exec -it kubia-container bash` |
| 113 | + |
| 114 | +This will run bash inside the existing **kubia-container** container. The **bash** process will have the same Linux namespaces as the main container process. This allows you to explore the container from within and see how Node.js and your app see the system when running inside the container. The **-it** option is shorthand for two options: |
| 115 | + |
| 116 | +- -i, which makes sure STDIN is kept open. You need this for entering commands into the shell. |
| 117 | +- -t, which allocates a pseudo terminal (TTY). |
| 118 | + |
| 119 | +#### Exploring container from within |
| 120 | + |
| 121 | +Let’s see how to use the shell in the following listing to see the processes running in the container. |
| 122 | + |
| 123 | +```bash |
| 124 | +root@c61b9b509f9a:/# ps aux |
| 125 | +USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND |
| 126 | +root 1 0.4 1.3 872872 27832 ? Ssl 06:01 0:00 node app.js |
| 127 | +root 11 0.1 0.1 20244 3016 pts/0 Ss 06:02 0:00 bash |
| 128 | +root 16 0.0 0.0 17504 2036 pts/0 R+ 06:02 0:00 ps aux |
| 129 | +``` |
| 130 | + |
| 131 | +You see only three processes. You don’t see any other processes from the host OS. |
| 132 | + |
| 133 | + |
| 134 | +Like having an isolated process tree, each container also has an isolated filesystem. Listing the contents of the root directory inside the container will only show the files in the container and will include all the files that are in the image plus any files that are created while the container is running (log files and similar), as shown in the following listing. |
| 135 | + |
| 136 | +```bash |
| 137 | +root@c61b9b509f9a:/# ls |
| 138 | +app.js bin boot dev etc home lib lib64 media mnt opt package-lock.json proc root run sbin srv sys tmp usr var |
| 139 | +``` |
| 140 | + |
| 141 | +It contains the **app.js** file and other system directories that are part of the node:8 base image you’re using. To exit the container, you exit the shell by running the **exit** command and you’ll be returned to your host machine (like logging out of an ssh session, for example). |
| 142 | + |
| 143 | +#### Stopping and removing a container |
| 144 | + |
| 145 | +`docker stop kubia-container` |
| 146 | + |
| 147 | +This will stop the main process running in the container and consequently stop the container because no other processes are running inside the container. The container itself still exists and you can see it with **docker ps -a**. The -a option prints out all the containers, those running and those that have been stopped. To truly remove a container, you need to remove it with the **docker rm** command: |
| 148 | + |
| 149 | +`docker rm kubia-container` |
| 150 | + |
| 151 | +This deletes the container. All its contents are removed and it can’t be started again. |
| 152 | + |
| 153 | +### Pushing the image to an image registry |
| 154 | + |
| 155 | +The image you’ve built has so far only been available on your local machine. To allow you to run it on any other machine, you need to push the image to an external image registry. For the sake of simplicity, you won’t set up a private image registry and will instead push the image to [Docker Hub](http://hub.docker.com) |
| 156 | + |
| 157 | +Before you do that, you need to re-tag your image according to Docker Hub’s rules. Docker Hub will allow you to push an image if the image’s repository name starts with your Docker Hub ID. You create your Docker Hub ID by registering at [hub-docker](http://hub.docker.com). I’ll use my own ID (knrt10) in the following examples. Please change every occurrence with your own ID. |
| 158 | + |
| 159 | +Once you know your ID, you’re ready to rename your image, currently tagged as kubia, to knrt10/kubia (replace knrt10 with your own Docker Hub ID): |
| 160 | + |
| 161 | +`docker tag kubia knrt10/kubia` |
| 162 | + |
| 163 | +This doesn’t rename the tag; it creates an additional tag for the same image. You can confirm this by listing the images stored on your system with the docker images command, as shown in the following listing. |
| 164 | + |
| 165 | +`docker images | head` |
| 166 | + |
| 167 | +As you can see, both kubia and knrt10/kubia point to the same image ID, so they’re in fact one single image with two tags. |
| 168 | + |
| 169 | +#### Pushing image to docker hub |
| 170 | + |
| 171 | +Before you can push the image to Docker Hub, you need to log in under your user ID with the **docker login** command. Once you’re logged in, you can finally push the yourid/kubia image to Docker Hub like this: |
| 172 | + |
| 173 | +`docker push knrt10/kubia` |
0 commit comments