In this part we are going to create our docker image with our application in it.
Lets create our own image with dotnet runtime in there. And then put in a dotnet console app in the image. Here is my .net6.0 console program that I will use.
var randomGen = new Random();
var guessThis = randomGen.NextInt64(9).ToString();var input = string.Empty;do
System.Console.WriteLine("Enter your guess");
input = Console.ReadLine();
if(input!=null && input.Equals(guessThis))
System.Console.WriteLine("Right answer ! 🎊 Go again 👍");
guessThis = randomGen.NextInt64(9).ToString();
Lets create a folder called dotnet-docker.
Do “dotnet publish”, then copy the contents of the publish folder into another folder called app in the dotnet-docker folder.
Inside the dotnet-docker folder lets create a “Dockerfile”. Create a file with name Dockerfile with no extension. Here is the first line in that file.
Docker images are built with layers. That is the base layer of the image. Every line I add will be added over the layer that gets created by lines before it. We use the FROM instruction, which is usually the first line of any docker file.
Now lets create a folder in the image and navigate into it. The way we do that in the docker files is by adding another layer. We will use the WORKDIR instruction. It sets the working directory for the layers below it; which is what we want. So lets add another line to it. Our Dockerfile now looks like this:
The next thing we want layer in our image is having all our app files in the image. We will use the COPY instruction to copy files from our folder /app to the folder in the image /container-app.
Cool, those are the layers defined for our image. We have got our application in there too. Well, not yet. This is a docker file only. We will build our image from this Docker file. So, next step, build the image.
docker build -t dotnet-docker/tester:latest .
Ok, lets break it down. We use docker build command.
How does it know where is the docker file? We run this command in the same directory as the docker file. Hence we did not have to specify. If we had to we would use -f flag.
To build an image, docker build command will need a docker file and a context. we already talked about docker file that it can pick from the directory the command is running. The context is the set of file the command may need to use. Since we will have to copy files from our file system to the image, we tell which files using the context. We put a dot “.” for the context, that means those are the set of files that docker build should be ready to use if mentioned in the docker file. The COPY instruction in the docker file works because we specify the context.
The -t flag is used to specify a repository and a tag. In this case we said dotnet-docker as the repository and latest as the tag. Note that this is quite similar in format to the image we are inheriting from in the FROM instruction.
Once the image is built. You will find the image by running the following command:
Now, lets run the image as a container. You already know this command from part 1.
docker run -it --rm dotnet-docker/tester:latest
Once we are in the container. We will invoke our application.
There we are in our application. “exit” to stop the program. Another “exit” to stop the container.
Containers are most used for server applications. Now that we have got our feet wet, in the next part we will run a web application in a container.
Until next time. Cheers.