In my previous two articles about Docker overview, we have seen how docker containers can help in efficient use of hardware and what are technical components involved in docker.
In this article, we will try to
- Dockerfile to create a new image
- Publish Image to Docker Hub
- Run the application in docker
Let’s get started.
Dockerize the Blazor Application
Docker image is a template file which has all the instructions for containers. In most of the cases, you do not create image from the scratch. Generally you base your image on already existing image.
The docker image can be created using Dockerfile.
Step 1: Create a Visual Studio 2019 Blazor App
Create New Project and Select Blazor App as the project template. Name the project and solution both as “FirstBlazorApp”.
Step 2: Create a new Dockerfile
Create the Dockerfile under the project folder. Please note that Dockerfile is a filename and there is no extension required in filename.
The solution structure should like like the one shown below:
Now, put below contents in the Dockerfile.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["FirstBlazorApp/FirstBlazorApp.csproj", "FirstBlazorApp/"]
RUN dotnet restore "FirstBlazorApp/FirstBlazorApp.csproj"
COPY . .
WORKDIR "/src/FirstBlazorApp"
RUN dotnet build "FirstBlazorApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "FirstBlazorApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FirstBlazorApp.dll"]
Below are the explanation of the instructions we have used in the dockerfile.
In short, it creates a base image and exposes it over port 8080.
Then it pulls another image from docker hub and copies the application there. Then it builds it and pushes the output again to the Base Image and sets Entry point of the container.
FROM <image> [AS <name>]
A valid Dockerfile always starts from the instruction FROM. This sets the base image for further instructions.
Optionally, you can specify name to the image and you can use that in subsequent FROM clauses or COPY –from parameter.
WORKDIR /path
This instruction sets the working directory for any instructions that follow. The output of the any of the following command is stored in the WORKDIR path.
COPY <src> <dest>
The instruction copies the contents of source directory in destination directory. Optionally COPY
accepts a flag --from=<name|index>
. This flag can be used to set the source location to a previous build stage (created with FROM .. AS <name>
).
RUN <command> [<param1> <param2>]
The instruction executes the command or executable with argument. The results are applied to current image and the result image is used for subsequent instructions.
ENTRYPOINT
This instruction allows you to configure the executable that will run as an executable when you run the container. Only the last ENTRYPOINT instruction will have an effect.
The command line arguments specified after docker run <image> [<args>] are passed to the ENTRYPOINT
Option Step 2.1: Add .Dockerignore file
Optionally, you can also create .dockerignore file to ignore some files from getting into docker image. Below is sample .dockerignore file
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
Step 3: Build the image
Please make sure you run the command from the directory Below command builds the image from the docker file. The image name is myfirstblazorapp. If you run docker images command, you should be able to see the images available locally. You can run this on command prompt if you have installed docker on your machine.
Open command prompt and navigate to path of folder in which your solution file resides.
Then if you run below command, you will not get any output as you do not have any images in local.
docker image list
Run below command to create a new image from the docker file. Do not ignore the last dot from the command, it tells to look file in either current directory (including sub directories).
docker build -t myfirstblazorapp -f FirstBlazorApp/Dockerfile .
This will pull the aspnetcore images from docker hub and extract them on your machine. Then it will use Dockerfile to publish your application to create a new image. Now if you run docker image list command you should be able to see your image as well as other images you downloaded from Docker Hub.
Step 4: Run the container
If you run docker ps command, it shows running containers. Currently, your machine may not have any running containers and hence the list may be empty.
This is time to run the container by executing the below command. This command binds 8080 port from your local machine to 80 port of container and starts the container. If you access http://localhost:8080 in your browser.
docker run -p 8080:80 --name myapp myfirstblazorapp
Now, if you run docker ps command, it will show one container. This container name is myapp while the image name is myfirstblazorapp.
Step 5: Push to Docker Hub
You need to login to the Docker hub registry from the command prompt by using docker login command. This command will ask for username and password.
After docker login, you can execute below command to tag your image. Replace yourhubhusername with your actual username you used to login.
docker tag myfirstblazorapp yourhubusername/myfirstblazorapp
Once this is done, you can run below command to push the image to public repository. Please replace yourhubusername with the actual username you use to login to Docker Hub.
docker push yourhubusername/myfirstblazorapp
And that’s it, you have published your image to docker hub. You can go to browser, login to docker hub to see your published image.
Visual Studio Support
Visual studio supports Docker. You can enable docker support while creating the project. If not done while creating the project, you can add a Dockerfile to the project by right clicking on the project file and selecting option Add Docker Support.
In fact, the Dockerfile and .dockerignore files shown in this blog are the files generated by Visual Studio 2019, when I added docker support. The Visual Studio can also run the application in Docker and it is as simple as running it on IIS Express.
Let me know how it goes when you start playing with Docker.