building-image-from-the-docker-command-line

Building  image from the command line

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

docker build command

docker build [options] PATH | URL | -

Refer to the options section for an overview of available OPTIONS for this command.

Description

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

For our productmicroservice  I do the following:

Open cmd command line form  the following folder:

C:\Utvecklingprogram\Microservices\MicroServices_DotNET_Core-Master

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"]

And run the following command:

docker build -t mehzan07/productmicroservice:v1 . -f ./ProductMicroservice/ProductMicroservice/Dockerfile

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.

2022/07/docker-commands-on-cli-1.png
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

docker-commands-on-cli-2.png
Adding DNS address [“10.1.2.3”, “8.8.8.8”] for Container
Restart Docker Desktop.

Now with running again the docker build command:

docker-commands-on-cli-3.png
docker build succeed

And we can check the image by docker images command:

docker-commands-on-cli-4.png

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-commands-on-cli-11.png
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)

Now we run the following command:

docker build -t mehzan07/productmicroservice:v1 . -f ./ProductMicroservice/ProductMicroservice/Dockerfile

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            SIZE
mehzan07/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:

docker login -u username
Password:xxxx
Login Succeeded

The result shall be success and seen in the following figure:

docker-commands-on-cli-7-2.png
docker push to docker hub

If we check the Docker hub we see the that image is pushed as seen in the following figure:

docker-commands-on-cli-8.png
mehzan07/productmicroservice is pushed to the docker hub

docker run command to start container

Now we run this image to start container with docker run command as following:

docker run -d -p 32792:80 -p 32793:443 mehzan07/productmicroservice:v1

The -d -p is ports which you can start container from the browser

The result is shown in the following figure:

docker-commands-on-cli-9.png
docker run creates a container with container ID = 21b395b0578e

As we see in the above figure, a container is created and we can check this both in command: docker ps and Docker desktop: Container.

docker ps command shows the running container

where the Container ID = 21b395b0578e

you can open browser with the following url

http://localhost:32792/
or:
http://localhost:327923/

building-image-from-the-docker-command-line-17.png.png

You can even start Swagger UI in browser, in the Docker desktop: Container port :32792:

This was process of docker build, push, and run command.

after that you can pull from docker hub to the production server and run (start) the container from the image and run the application.

These are the steps to go from develop to production:

  1. Create & Build the Container
  2. Store the Image in an Accessible Registry
  3. Build a Deployment Configuration
  4. Make the Deployment

And here are som usefull links:

How to Go From Development to Deployment with Docker

How to customize Docker containers in Visual Studio

How do I move a docker image from QA to production

Step by step: Building custom ASP.NET Core containers with Docker

Deploy to Docker Hub

 

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)

building-image-from-the-docker-command-line-14.png
docker pull mehzan07/productmicrservice

As we see above the image with tag name: docker.io/mehzan07/productmicrservice:latest has been pulled.

Docker Exec 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.

 

building-image-from-the-docker-command-line-15.png
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.

Options

Name, shorthand Default Description
--attach , -a Attach STDOUT/STDERR and forward signals
--checkpoint experimental (daemon)
Restore from this checkpoint
--checkpoint-dir experimental (daemon)
Use a custom checkpoint storage directory
--detach-keys 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 

building-image-from-the-docker-command-line-16-1.png
docker start [i] containder id
AS 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.

For more about docker commands look to the Docker cli base commands

 

you can find the code in my Github.

Conclusion

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.

In my next post  I will explain How to Connect from Docker Container to local or remote SQL Server

This post is part of  Docker step by step

And also part of Microservices-Step by step”.

Back to home page