serving the solutions day and night


Sunday, February 2, 2020

ASP.NET CORE and Docker

Command to see the installed docker version

docker --version

Commands to list docker images  - display the repository name, a tag that usually specifies the repository version, image ID, creation date and size

docker image ls

docker images

Download the latest version of the image

docker pull microsoft/dotnet-samples 

docker pull microsoft/dotnet-samples:aspnetapp 
     - a specific image version, for example, the version that contains only the ASP.NET CORE sample

Create and run a container from the downloaded image - ASP.NET CORE Console App
docker run 70e25069fca7

docker run 70e2 - you can also use the first digits of the ID
  • If it all works fine, you see the message Hello from .NET Core! in the terminal.
  • A Docker container has been created from the image with the ID 70e2 and it has been run.

Container States : A container can be in one of the following states:
  • created: It’s been created, but not yet started.
  • restarting: It’s in the process of being restarted.
  • running: It is executing.
  • paused: Its execution is suspended.
  • exited: It’s been run and has completed its job.
  • dead: The daemon tried and failed to stop.
Commands to list docker containers

docker ps       => To show all containers in all status
docker ps -a   => To show all containers in a running state

  • The status of the container (70e25069fca7) is exited. The reason is, a simple console .net app that terminates its execution at the end of the main method. That means, at the end of process execution, the container was terminated.
  • In VM, remains in an execution state if your program has been executed. The OS running in the VM doesn't stop its execution. But a container is not a VM, it doesn't contain the OS, just your code, and if your code has completed its job, the container terminates.
Create and run a container from the downloaded image - ASP.NET CORE  web application

docker run 575d
  • This container starts an ASP.NET CORE web application, so the requested listener remains in waiting for the HTTP request.
  • docker ps -a (from another terminal window, you can see the container that’s just launched.)
  • Try http://localhost, you will receive a connection refused error, because the ASP.NET Core listener waits for requests on port 80 in the container. But, by default, your container doesn't redirect the external request to its localhost. You have to specify this redirection when starting the container, using the -p options.
  • Redirect a request from the external port 8082 to the internal port 80 with the execution of the following command. 

docker run -p 8082:80 575d
  • Try http://localhost:8082, you can see the default template of an ASP.NET CORE application.

Docker Commands
  • By default, the command docker run creates and executes a new container every time. To start already created the container with a previous run,  docker start <container-id>
  • Run a container, you can also specify a name that is assigned by default randomly (priceless_kepler).

docker run -p 8080:80 --name anc_mvc_app 575d

  • Start and stop the container using the anc_mvc_app name instead of the container ID.

docker start anc_mvc_app
docker stop anc_mvc_app

  • Create a container without running it, you can use the command docker create instead of docker run.

docker create -p 8080:80 --name mvcapp2 d203

  • Remove the container, you can use the docker rm command with the container name or ID.
docker rm anc_mvc_app
docker rmi anc_mvc_app
docker rm --force priceless_kepler

  • Run a container that doesn't terminate, is -d.  (detached mode ). The container is created and is running in the background, you can run other command in the same window. 

docker run -d -p 8080:80 --name anc_mvc_app 575d

  • Run a container in the Interactive mode

docker start -i anc_mvc_app

No comments: