DEV Community

Cover image for Docker With
Hasan Elsherbiny
Hasan Elsherbiny

Posted on

Docker With

in this article we are going step by step to create docker image from project


  • Docker Installed
  • ASP.NET Core SDK
  • Visual Studio (or VS Code)

1. Create an ASP.NET Core Project

create a new ASP.NET Core project (If you haven't already)

dotnet new mvc -n MyProject
cd MyProject
Enter fullscreen mode Exit fullscreen mode

2. Dockerize the ASP.NET Core Application

To containerize the ASP.NET Core application, you need to create a Dockerfile in the project directory.
The Docker file contains instructions for building the Docker image.
Create a new file named 'Dockerfile' in the root directory of your ASP.NET Core project and add the following content:

# Use the official ASP.NET Core runtime image as a base
FROM AS base

# Copy the published output of the ASP.NET Core application into the container
FROM AS build
COPY ["MyProject.csproj", "."]
RUN dotnet restore "./MyProject.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyProject.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile defines a multi-stage build process:

-The first stage (base) sets up the base image, exposes port 80, and sets the working directory.
-The second stage (build) restores dependencies, builds the application, and publishes it.
-The third stage (publish) copies the published output into a separate stage.
-The final stage (final) sets up the runtime environment and specifies the entry point for the application.

3. Build the Docker Image

Once you have created the Dockerfile, you can build the Docker image using the docker build command

docker build -t myproject .
Enter fullscreen mode Exit fullscreen mode

This command builds a Docker image tagged as myproject using the Dockerfile in the current directory (.).

now our image is ready to be hosted on docker host and downloaded to any server to be deployed and install it's dependences
but we will test it on our local host first.

4. Run the Docker Container

After successfully building the Docker image, you can run a Docker container using command

docker run -d -p 8080:80 --name myproject-container myproject
Enter fullscreen mode Exit fullscreen mode

This command starts a Docker container named myproject-container based on the myproject image, mapping port 8080 on the host to port 80 inside the container (-p 8080:80).

The -d flag runs the container in detached mode, allowing it to run in the background.

5. Access the ASP.NET Core Application

Once the Docker container is running, you can access the ASP.NET Core application by navigating to http://localhost:8080 in your web browser. If you're running Docker on a remote server, replace localhost with the server's IP address.

Top comments (0)