Essential DevOps

Essential DevOps

Docker for developer: Part 1 (Basic)

·

3 min read

Table of contents

No heading

No headings in the article.

Containerization has been very popular in modern software development, where the applications can be managed easily using containers along with their dependencies and resources.

image.png

There are many tools for containerisation. One of the popular tools used for containerisation is "Docker" which is actively used by multiple companies.

What is Docker?
Docker is an open-source platform that allows your application to run as a container, in simple terms it allows your application to run along with its dependencies packaged in a restricted environment. There are a few concepts that are used by docker which are explained as follows:

  1. Image: A docker image is an immutable file which contains source code, libraries, dependencies, tools, and other files needed for an application to run.

  2. Registry: This is the place where the docker images can be stored and pulled whenever required to be run. Docker Hub is the default Docker registry that everyone uses.

  3. Container: It is the instance of an image. This is what we run in a docker server (basically can be called a virtual machine).

  4. Docker daemon: A daemon process that performs the main tasks, such as creating, running, and monitoring containers, along with building and storing images.

  5. Docker Client: A client communicates to the Docker daemon with the help of HTTP. It might be a UI tool for your computer for monitoring the docker.

Following steps

  1. Installation

  2. Basic commands

  3. Creating docker image

  4. Monitoring

Note: All the actions for the above-mentioned steps are performed on Mac.

Installation

The installation of docker is pretty easy just download the installer and install it in your system. Click here for the download page

Verify installation using the terminal: docker version

Basic commands

  1. Pull: Pulls the image from the repository docker pull hello-world

  2. Run: Runs an image as a container docker run hello-world (Note: docker run automatically pulls the image if the image is not available on the instance)

  3. Ps: Lists all the containers that are running docker ps

  4. Images: Lists all the images currently on the server docker images

  5. Logs: Retrieves logs of a container docker logs <container-id>

  6. Inspect: Gives detailed information about a container docker inspect <container-id> 7: Stop: Stops a running container docker stop <container-id>

  7. Rm: Deletes a container docker rm <container-id>

Creating Docker Image

As we know the basic of docker let's start creating our docker image. A Docker image is created using the docker build command and with the help of the "Dockerfile" file, which contains instructions/commands on how the image should be built.

Note: The file name used to create a docker image can be any name, but the good practice is to name it "Dockerfile"

Command to build a docker image
docker build -f <DockerFile> -t <ImageName> .

Let's create a docker image and run it

  1. Create a docker file with the name Dockerfile and paste the below contents to the file

     # Dockerfile
    
     FROM ubuntu:latest
     COPY server.py .
     RUN apt-get update
     RUN apt-get install -y python3
     CMD python3 server.py
    
  2. Create a file name server.py and add the code below to the file

     from http.server import BaseHTTPRequestHandler, HTTPServer
    
     hostName = "0.0.0.0"
     serverPort = 8080
    
     class MyServer(BaseHTTPRequestHandler):
         def _set_headers(self):
             self.send_response(200)
             self.send_header('Content-type', 'text/html')
             self.end_headers()
    
         def do_GET(self):
             self._set_headers()
             self.wfile.write(bytes("Docker Part 1 server.", "utf-8"))
    
     if __name__ == "__main__":
         webServer = HTTPServer((hostName, serverPort), MyServer)
         try:
             print("Server Started")
             webServer.serve_forever()
         except KeyboardInterrupt:
             pass
    
         webServer.server_close()
         print("Server Stopped")
    
  3. Create the image using the command below

    docker build -f Dockerfile -t python_server .

  4. Verify the image is created using the command

    docker images

  5. Run the image

    docker run -d -p 8080:8080 python_server

  6. Verify the server running

    curl --location 'http://localhost:8080/'

Monitoring

Command to get run container usage stats docker stats <ContainerName>

Note: This article just gives a basic understanding of docker but there are advanced concepts of docker which will be published in the upcoming article