Learning About Docker Basics in Minutes - Docker File

Docker File

It’s used to build a Docker Image. Which will be used to create a container based on instructions. It has some basic commands to build a Docker Image, they are.

  • FROM
  • CMD
  • EXPOSE
  • ENV
  • WORKDIR
  • COPY
  • RUN
  • USER

Let’s see in detail.

FROM

It denotes the base image for the build. We may say it will load dependencies. We don’t worry about anything for installation or other items.

  • Base image: It's a pre-built image containing an operating system, programming languages, libraries, and other tools. It serves as the foundation for your Docker image.
  • Dependency handling: Many base images come pre-installed with common dependencies, saving you the hassle of installing them individually.

CMD

A default command of a container using this image will run.

EXPOSE

Port to expose for Image, Ex: EXPOSE 8081. After Building the Image, the container will be available (deployed) in the port like 127.0.0.1:8081

WORKDIR

It helps us to specify the files to copy & commands execute for Image.

ENV

Used for setting or updating an environment variable. It will be used while running or creating a container i.e. while the deployment of the application.

COPY

This command will be used to copy file(s) from hosting and maintain the inside of the image.

RUN

It will execute a command. i.e. It tells the builder to run the command that is specified.

USER

This instruction sets the default user for all subsequent instructions.

Live Example For DOT NET 8 API.

API

Part 1

  1. download the Docker image of dot net asp net 8.0 FROM Microsoft Container Register and set it as the base.
  2. USER set to app
  3. Setting app folder as WORKDIR.
  4. Finally, the runnable container will be accessed through the 8080 port.

Part 2

  1. download the Docker image of dot net asp net 8.0 FROM Microsoft Container Register and set it as build.
  2. Setting the Build Configuration as Release using ARG.
  3. Setting src folder as WORKDIR.
  4. Copying the project files to the working directory (/src).
  5. Using RUN Command execute DOTNET RESTORE Command and Build Command.

Part 2 is used to restore and build the project before publishing. If any issues, It will terminate here.

Part 3

  1. From the last checkpoint i.e. FROM build of previous Part 2, the current task will continue.
  2. Again, it set the Build Configuration as Release.
  3. Using the RUN Command, Execute the publish command and store the published files to /app/publish

Part 4

  1. From the base checkpoint (Part 1) and set as a final checkpoint.
  2. Change the working directory as /app.
  3. Using COPY command to copy all published files to the current working directory.
  4. Then Set the Executable or API Startup.

Commands for Build, Tag, and Run Container

  • Go to the project where you place your Docker file, generally Docker file is placed in the root of the project where the .csproj file.
  • Open the terminal or command prompt to execute the following command.
    docker build -t {ACCOUNT NAME/REPOSITORY NAME}:{TAG NAME} .
    #Example Command
    docker build -t vinothCSharpCorner/Dummy:dev_001 .
  • Push to Hub
    doker push {ACCOUNT NAME/REPOSITORY NAME}:{TAG NAME}
  • Run in Different Machines i.e, deployment
    docker run –d {ACCOUNT NAME/REPOSITORY NAME}:{TAG NAME}
    docker run
    –p {Local Port to Run}:{Exposed_PORT}
    --name="{Name of the Container}"
    {ACCOUNT NAME/REPOSITORY NAME}:{TAG NAME}

Thanks, guys! We will in more detail in the next article, for Angular and other templates. Will post what are the issues we faced and solutions found and applied to the production environments.

Previous Article: Learning About Docker Basics in Minutes (c-sharpcorner.com)


Similar Articles