Post

Docker 2 - Dockerfile Introduction

Introduction

  • A Dockerfile is a text file that contains the instructions we use to build and run a Docker image.
  • The following aspects of the image are defined:
    • The base or parent image we use to create the new image
    • Commands to update the base OS and install additional software
    • Build artifacts to include, such as a developed application
    • Services to expose, such as storage and network configuration
    • Command to run when the container is launched
  • Suppose we’re creating a Docker image for our ASP.NET Core website. The Dockerfile might look like the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Step 1: Specify the parent image for the new image
FROM ubuntu:18.04

# Step 2: Update OS packages and install additional software
RUN apt -y update &&  apt install -y wget nginx software-properties-common apt-transport-https \
	&& wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb \
	&& dpkg -i packages-microsoft-prod.deb \
	&& add-apt-repository universe \
	&& apt -y update \
	&& apt install -y dotnet-sdk-3.0

# Step 3: Configure Nginx environment
CMD service nginx start

# Step 4: Configure Nginx environment
COPY ./default /etc/nginx/sites-available/default

# STEP 5: Configure work directory
WORKDIR /app

# STEP 6: Copy website code to container
COPY ./website/. .

# STEP 7: Configure network requirements
EXPOSE 80:8080

# STEP 8: Define the entry point of the process that runs in the container
ENTRYPOINT ["dotnet", "website.dll"]
  • Key Points:

    • The Dockerfile is a series of instructions that define how to build a Docker image.
    • Each instruction creates a new layer in the image, allowing for incremental changes.
    • The FROM instruction specifies the base image for the new image.
    • The RUN instruction executes commands in the image.
    • The COPY instruction copies files from the local system into the image.
    • The WORKDIR instruction sets the working directory for subsequent commands.
    • The EXPOSE instruction exposes ports for communication.
    • The ENTRYPOINT instruction specifies the command to run when the container starts.
  • Creating a Docker Image:

    • Start by identifying a suitable base image, often found on Docker Hub.
    • Base images typically include an application framework and a Linux distribution (e.g., Ubuntu or Alpine).
  • Example of a Base Image:

    • For an ASP.NET application, you might use Microsoft’s base image: mcr.microsoft.com/dotnet/aspnet, which includes the ASP.NET runtime.
  • Customizing a Docker Image:

    • Start a container from the base image and make changes as needed.
    • Common changes include copying files from the local system into the container and running tools to compile code.
This post is licensed under CC BY 4.0 by the author.