Table of contents
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.
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:
Image: A docker image is an immutable file which contains source code, libraries, dependencies, tools, and other files needed for an application to run.
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.
Container: It is the instance of an image. This is what we run in a docker server (basically can be called a virtual machine).
Docker daemon: A daemon process that performs the main tasks, such as creating, running, and monitoring containers, along with building and storing images.
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
Installation
Basic commands
Creating docker image
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
Pull: Pulls the image from the repository
docker pull hello-world
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)Ps: Lists all the containers that are running
docker ps
Images: Lists all the images currently on the server
docker images
Logs: Retrieves logs of a container
docker logs <container-id>
Inspect: Gives detailed information about a container
docker inspect <container-id>
7: Stop: Stops a running containerdocker stop <container-id>
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 imagedocker build -f <DockerFile> -t <ImageName> .
Let's create a docker image and run it
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
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")
Create the image using the command below
docker build -f Dockerfile -t python_server .
Verify the image is created using the command
docker images
Run the image
docker run -d -p 8080:8080 python_server
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