In this article we are going through to build an image and the push it to the Docker hub and then run the image to create a container and test the application via docker online command (without Visual Studio).’
In Visual Studio you can create a docker-compose project and set as startup project, start this project to create images and run your application via docker-compose. How to create this project you can look to the my post: Docker Containers and Docker installation
In this post we are going to describe docker commands and using my ProductMicroservice project which is in my Github
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. For example, your build can use a COPY instruction to reference a file in the context.
Build the Dockerfile without Visual Studio
You don’t need Visual Studio to create a Docker image. This is useful when you want to create the image and then push it to a container registry like Dockerhub. You should always do this in a build pipeline but it’s useful to know how to do it by hand and sometimes you need it to quickly test something.
Open command prompt, or Powershell or bash and navigate to the folder that containing the Solution.sln file. To build an image, you can use docker build [build context] [location of Dockerfile]. Optionally, you can add a tag by using -t tagname. Use
The following is our Dockerfile in the current directory:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 1433
# <add your commands here>
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj", "ProductMicroservice/"]
RUN dotnet restore "ProductMicroservice/ProductMicroservice.csproj"
COPY . .
WORKDIR "/src/ProductMicroservice"
RUN dotnet build "ProductMicroservice.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProductMicroservice.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProductMicroservice.dll"]
Here -t is tag and tag name is: v1, and mehzan07 is repository (organization name) on docker hub and productmicroservice is product name , (.) is current dir and -f is file path which is: path to Dockerfile
(./ProductMicroservice/ProductMicroservice/Dockerfile
This command goes through the docker commands in the Dockerfile
The Dockerfile is in your current folder with the tag name productmicrservice. This will download the needed images (or use them from the cache) and start to build your image.
Build docker image failed
Have got following erro:
Determining projects to restore...C:\src\ProductMicroservice\ProductMicroservice\ProductMicroservice.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json..
I have also found that in Visual Studio with Release Configuration has the same error.
With investigation I have found the solution to fix in Docker Desktop as following:
The Resolution
The DNS server is wrong in the container. To fix, hardcode the DNS into Docker i.e. put this JSON
“dns”: [“10.1.2.3”, “8.8.8.8”]
into the Docker daemon settings. In Docker Desktop it’s here
Adding DNS address [“10.1.2.3”, “8.8.8.8”] for ContainerRestart Docker Desktop.
Now with running again the docker build command:
docker build succeed
And we can check the image by docker images command:
we see that the image with tag productmicroservice is created. We can check this even in Docker Desktop under images.
Push image to Docker hub register:
First we should have a account in Docker hub for me docker hub username is mehzan07.
We have to create a repository in Docker hub. Docker Hub organizes repositories by user name. Any repository created under my account includes my username in the Docker image name. That is because we should build image with a tag name as username/repository name (e.g. : mehzan07/productmicroservice).
we use the docker push command as following:
docker push mehzan07/productmicroservice:v1
In case you got Error:
denied: requested access to the resource is denied
you need to do the followings:
1- login to docker:
>docker login:
usename: give your username
Psw: give your password:
2. tag your image as follow:
> docker tag imagename YOUR_DOCKERHUB_NAME/iamagename
in may canse:
> docker tag productmicroservice mydockerhubusername/productmicroservice
then check it by
> docker images:
you can see:
mydockerhubusername/productmicroservice under repository
3. Now you should be able to push it. >docker push YOUR_DOCKERHUB_NAME/firstimage in my case: >docker push mehzan07/productmicroservice
It takes a little time and image is pushed to the docker hub you can see their and in the docker desktop Remote directory.
Here is our example:
docker push failed, there is no repository to match on the docker hub
As we see pushing image with tag (-t) productmicroservice failed because the tag not matches with repository name in the docker hub, that is because we have to build a new image with tag (-t) mehzan07/productmicroservice)
In above command -t is for tag: mehzan07/productmicrservice and we run this command after success we can check the created image with command docker images:
REPOSITORY TAG IMAGE ID CREATED SIZEmehzan07/productmicrservice latest a6409fb6abec About a minute ago 374MB
Now we have a image with its Repository is exact the same we have created in the docker hub. and we can push this image with following docker push command: docker push mehzan07/productmicroservice
Note: sometimes we need to login to docker hub before push command as following:
Delete images from Docker hub
Log in to Docker Hub.
Click Repositories from the main menu and select a repository from the list. …
From Settings select Delete and press to delete
Check your image:
docker images: shows you images information
docker pull command:
This command pulls image with image tag name (Repository name) form Docker hub to the local machine.
docker pull mehzan07/productmicrservice
If you got error:
Error response from daemon: manifest for mehzan07/productmicrservice:latest not found: manifest unknown: manifest unknown
1- Image Exist in the docker hub
2- Correct Tag or Repository: Ensure the repository and tag name are correct.
3- Permissions or Authentication Issue: it should be public
4- Double-check the repository and tag names
In docker hub: press to the repository and then General
– you see Public View, press this button
– you see the Docker Pull Command ( docker pull mehzan07/productmicroservice)
– copy this command and run on the command prompt
>docker pull mehzan07/productmicroservice then it pulls from docker hub and can be seen on the docker desktop ( if it was not seen before)
docker pull mehzan07/productmicrservice
As we see above the image with tag name: docker.io/mehzan07/productmicrservice:latest has been pulled.
Docker Exec Command
Docker Exec Syntax
In order to execute commands on running containers, you have to execute “docker exec” and specify the container name (or ID) as well as the command to be executed on this container.
$ docker exec <options> <container> <command>
As an example, let’s say that you want to execute the “ls” command on one of your containers.
The first thing that you need to do is to identify the container name (if you gave your container one) or the container ID.
In order to determine the container name or ID, you can simply execute the “docker ps” command.
docker ps command
C:\Users\default1>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21b395b0578e mehzan07/productmicrservice:latest "dotnet ProductMicro…" 25 hours ago Up 32 minutes 1433/tcp, 0.0.0.0:32792->80/tcp, 0.0.0.0:32793->443/tcp naughty_agnesi
The running Container ID = 21b395b0578e for image mehzan07/productmicroservice.
Note : the “docker ps” is also used in order to determine whether a container is running or not.
As you can see, the container ID is the first column of the ‘docker ps’ output.
Now, to execute the “ls” command on this container, simply append the ‘ls’ command to the ID of your container (this is in linux not windows)
$ docker exec 21b395b0578e ls
bin
boot
dev
etc
home
Docker Start command
docker start [OPTIONS] CONTAINER [CONTAINER...]
For example uses of this command, refer to the examples section below.
Override the key sequence for detaching a container
--interactive , -i
Attach container’s STDIN
Examples:
docker start with container id (21b395b0578e)
docker start 21b395b0578e
and docker start -i 21b395b0578e
docker start [i] containder idAS we see the first command only start container , but the second works interactive (-i). wen we start browser: http://localhost:32792/ then in the terminal shows Error and give a hint to correct the error.
As we have seen we can build an image via command line (without visual studio) and push the image to docker hub after that we can run and even pull the image to another machine an deploy and run the image and create container.