New .NET Core is the biggest change since the invention of .NET platform. It is fully open-source, components and is supported by Windows, Linux and Mac OSX. In this post I am going to give it a test ride by creating a containerised C# application with the latest .NET CORE.
Docker containers allow teams to build, test, replicate and run software, regardless of where the software is deployed.Docker containers assure teams that software will always act the same no matter where it is – there’s no inconsistency in behavior, which allows for more accurate and reliable testing.
The main advantage to using Docker containers is that they mimic running an application in a virtual machine, minus the hassle and resource neediness of a virtual machine. Available for Windows and Linux systems, Docker containers are lightweight and simple, taking up only a small amount of operating system capacity. They start-up in seconds and require little disk space or memory.
docker installation is available for Mac and windows and can be downloaded from office channel. clickhere
- Install Visual studio for mac
- Install Docker for mac
here we will try to build asp.net core webapi and host/run on container with the help of docker.
1. Create New Project: choose asp.net core webapi template from visual studio 2017
Now provide project name,solution name and other details like where to save the project.
Now our newly created project structure looks as per below
Here we have very simple case where service only returns some information about employees as our main objective to host this tiny application on container.
2.Add Docker Support To application
Now add docker file in project and write instructions that how the docker image build from base image of asp.net core image.
Below are the instructions issue to daemon to create docker image.
3. Open Terminal on mac :
search for “Terminal” on mac machine and open new window.
Once we click on new window option then command window will appear and now all set to issue/write docker command to create docker image.
4. Navigate to application folder by issuing change directory command “CD” and make sure we are inside the application folder .We can verify that all items listed by issuing “LS” command that means we are in right place.
5. Create Docker Image :
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context
Command: Docker build -t .
here our image name is “firstapiwithdocker”,so command should be
docker build -t firstapiwithdocker .
at the moment we can see daemon accept the command start creating the docker image from the docker file instructions.
at last we can see image has been successfully created and tagged with “Latest” keyword.if we don’t provided any tag than daemon tagged the image with “Latest” keyword.
5. List all Docker Images:
Now we have to verify that require image has been created or not,so below command have to issue list down all the images.we can see all the important information about images like image name,tag,imageid,created date and size. in below image we can see that out newly created image listed with other base images.
Command : Docker Images
6.Run image with in container:
Till now we have successfully created docker image for our webapi solution and contain all the necessary files and now we have to create container to run this image.
below command will create container.
Syntax: docker run -d -p : –name
example: docker run -d -p 9000:80 –name FirstContainer firstapiwithdocker
once we execute above command,a new container has been created and get random number that means container has been created successfully.
Now List down all containers and we can see all the important metadata about containers like containerId,ImageName, Command,Created date,container status,Ports and container name.
here our newly created container is running and exposing 9000 from the host to 80 on the container.
Let’s hit the url “http://localhost:9000/api/values” on browser or postman to verify that our application is running on container or not.
Below are the result of the webapi which is running on container instead of local machine.
7. Push docker image on docker hub:
Docker Cloud uses Docker Hub as its native registry for storing both public and private repositories. Once you push your images to Docker Hub, they are available in Docker Cloud.
we need to create docker hub account to push the image on public/private repositories.
Docker image should be tagged with well qualified name before issuing the push command. so below command will tagged the image with name “RakeshMahur/FirstApiWithContainer”.
Syntax: docker tag <ImageName> <TagName>
Example: docker tag firstapiwithdocker rakeshmahur/webapicore-sample
Now login on docker hub from the terminal window by issuing the “docker login” command and provide the docker hub account details (username/password).
once docker hub credentials has been validated successfully then a message comes on window and now we will able to push image to docker hub.
Issue docker push command to push docker image to docker hub.
docker push rakeshmahur/webapicore-sample
once we execute above command then we can see our local docker image push to docker cloud repository and listed done over there and every one can pull this image start working on it.
Below are some important and comman used commands , refer to the docker documentation for more details and a more exhaustive list of flags.
- docker build -t .
- Builds an image from a given dockerfile. While still useful when handling individual images, ultimately docker-compose will build your project’s images.
- docker exec -it
- Runs a command in a running container. More than anything else, I’ve used exec to run a bash session (docker exec -it /bin/bash).
- docker image ls
- Lists images on your machine.
- docker image prune
- Removes unused images from your machine. Especially when building new images, I’ve found myself constantly wanting a clean slate. Combining prune with other commands helps clear up the clutter.
- docker inspect
- Outputs JSON formatted details about a given container. More than anything else I look for IP address via (docker inspect | grep IPAddress)
- docker pull
- Downloads a given image from a remote repository. For development purposes, docker compose will abstract this away, but if you want to run an external tool or run project on a new machine you’ll use pull.
- docker ps
- Without any flags, this lists all running containers on your machine. I’m constantly tossing on the ‘-a’ flag to see what containers I have across the board. While you are building a new image you inevitably have containers spawned from it exit prematurely due to some runtime error. You’ll need to do ‘docker ps -a’ to look up the container.
- docker push
- Once you have an image ready to be distributed/deployed you’ll use push to release it to either docker hub or a private repository.
- docker rm
- Removes an unstarted container from your system. Need to run docker stop first if it is running.
- docker rmi
- Removes an image. May need to add on the ‘–force’ flag to force removal if it is in use (provided you know what you are doing).
- docker run
- Runs a command in a new container. Learning the various flags for the run command will be extremely useful. The flags I’ve been using heavily are as follows:
- –rm – Removes the container after you end the process
- -it – Run the container interactively
- –entrypoint – Override the default command the image specifies
- -v – Maps a host volume into the container. For development, this allows us to use the image’s full environment and tools, but provide it our source code instead of production build files.
- -p – Maps a custom port (ie. 8080:80)
- –name – Gives the container a human readable name which eases troubleshooting
- –no-cache – Forces docker to reevaluate each step when it runs the container, as opposed to using caching.
- docker version
- Outputs both the client vs. server versions of docker being run. This isn’t the same as ‘-v’.
- docker volume ls
- While there are variants on volumes, so far I mostly use the ‘ls’ command to list current volumes for troubleshooting. I’m sure there will more to come with using volumes.