Introduction Of Docker File

We are going to discuss the Docker step-by-step

Agenda

  • Docker overview
  • Docker file overview
  • Sample docker file
  • Commands to create a docker image

Docker overview

  • The primary focus of docker is to automate the software deployment using lightweight containers.
  • Docker helps to run applications in different environments efficiently.
  • It uses a container as a package that contains all the dependencies to run our software application
  • Docker provides many features like Isolation, Security, High Scalability, and faster configuration.
  • If you want to learn more about docker then check my blogs related to that. (docker-introduction)

Docker file overview

  • The main thing about docker is the docker file which contains a set of instructions and commands using which we can create our application docker images as per need and configuration.
  • It’s the text document containing docker commands to assemble application images as per dependencies and needs.
  • Docker instructions are mostly in upper case for better readability and understanding and it will run all instructions in order one by one.
  • In the docker file, the # symbol is used for the comment

Sample .NET Core Web Application Docker File

#Comment
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

EXPOSE 443
EXPOSE 80 

# copy project csproj file and restore it in docker directory
COPY ./*.csproj ./
RUN dotnet restore

# Copy everything into the docker directory and build
COPY . .
RUN dotnet publish -c Release -o out

# Build runtime final image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "ProductMicroservice.dll"]
  • As you can see above Docker file in which we put many instructions one by one which is going to create a docker image for the .NET Core Web Application.
  • The Docker file starts with FROM instruction which specifies the parent image which we are going to use while creating the application image and it may be after comments and globally scoped argument inside the docker file. As you can see above, we get a .NET Core SDK image using FROM instruction.
  • Also, the # symbol is treated as a comment inside the docker file and that will not be treated as instruction while creating an image.
  • Next, we used WORKDIR instruction to set the working directory as we set the app in the above docker image.
  • After that, we used EXPOSE instruction to inform the docker that our container listens on the ports which we specified. There we specified ports that either listen on TCP or UDP and if we don’t supply any port then it will take TCP as the default protocol.
  • Next, we used COPY instruction to copy our source files inside the docker container image directory’s location which we specified earlier. COPY is just copied files from source to destination. As you can see in the above docker image we first copy the project file inside the docker directory and then restore the required things using RUN instruction.
  • RUN instruction is installed application with their require packages after creating a new layer on the top of the current docker image. We often find multiple RUN instructions inside the docker file as per need.
  • After that, we copied everything inside the docker directory using COPY instruction and create a new build using the RUN instruction.
  • Finally, we create a docker runtime build using an asp net image which we take and use using FROM instruction as I showed in the above Docker file. Also, set the working directory and copied the build, and set the entry point of the application using the ENTRYPOINT instruction which should start when we start the container using the docker image which we create using the docker file.

Commands to create a docker image

  • Use the following commands to create a docker image
  • First, build an image using this command
    docker build -t docker-image-name .
  • Later on, run the docker image using the following command as I have shown
    docker run -t -d -p 8085:80--name container-name docker-image-name

Conclusion

Here we have discussed the basics of docker and details about the docker file the and set of instructions we mostly use inside that while creating a docker image.

Happy Learning!


Similar Articles