Containerization is the packaging together of software code with all itβs necessary components like libraries, frameworks, and other dependencies so that they are isolated in their own "container."
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
VM | Container |
---|---|
Use Hypervisors to virtualy emulate physical hardware. | Use the OS Kernal to access underlying host hardware. |
Slow start up, as the VM OS needs to be launched first | Quick start up, as containers only need to access the OS Kernal. |
Difficult depenceny managed for applications to be deployed. | Easy dependeny management, as containers natively isolate and load all the dependencies into the internal File system. |
Require high effort to backup and restore applications, as all the dependencies go on single File system. | Applications can be easily migrated between machines and environments, as each container can be represented as 1 Docker file. |
-
Dockerfile: This is a text document, that contains all the commands required to assemble the Image of the application. Docker can automatically build images by reading instructions within the Dockerfile document.
-
Image: A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template.
-
Container: A container is an instance of an image, with all the dependencies and runtime managed required to run the application. This also has access to Host OS Kernal to access the underlying physical hardware.
-
Registry: A registry is a storage and content delivery system, holding named Docker images, available in different tagged versions. Images are also refered as Repositories.
- Create a new docker definition file in the root directory of your application and name the file as ##### Dockerfile
- Add following commands to the Dockerfile:
For JavaScript/Node Application
FROM node:alpine
COPY . /app
WORKDIR /app
CMD node <JS_FILE_NAME>
For ASP.Net Core Application
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["<PROJECT_NAME>.csproj", ""]
RUN dotnet restore "./<PROJECT_NAME>.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "<PROJECT_NAME>.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "<PROJECT_NAME>.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "<PROJECT_NAME>.dll"]
- Open a Command Prompt/PowerShell/Terminal at the root directory of the application to be Containerized.
- Build the Docker Image for the application using Docker Build Command, as below:
docker build -t <IMAGE_NAME> .
Wait for build to finish. 5. Run the Docker image, using the Docker Run command.
docker run <IMAGE_NAME>
- To push Docker image to Docker Hub, use Docker Login command to login to the Registry.
docker login
- Assign a Tag to Docker Image before pushing, using the Docker Tag command as below:
docker tag <IMAGE_NAME> <DOCKER_HUB_USERNAME:IMAGE_NAME>
- Push Docker Image, to Docker Hub using the Push command as follows:
docker push <DOCKER_HUB_USERNAME:IMAGE_NAME>
Wait for push activity to be successful.
NOTE: The pushed Docker Image, can now be pulled on any host machine running Docker Daemon using the Docker Pull Command and then can be ran easily as below:
docker pull <DOCKER_HUB_USERNAME:IMAGE_NAME>
docker run -d -p 8080:80 <DOCKER_HUB_USERNAME:IMAGE_NAME>