Docker Image Archives - TEKSpace Blog https://blog.tekspace.io/tag/docker-image/ Tech tutorials for Linux, Kubernetes, PowerShell, and Azure Wed, 30 Aug 2023 15:20:21 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://blog.tekspace.io/wp-content/uploads/2023/09/cropped-Tekspace-logo-icon-32x32.png Docker Image Archives - TEKSpace Blog https://blog.tekspace.io/tag/docker-image/ 32 32 Manage Docker Images locally and in remote Container Registry https://blog.tekspace.io/manage-docker-images-locally-and-in-remote-container-registry/ https://blog.tekspace.io/manage-docker-images-locally-and-in-remote-container-registry/#respond Sun, 31 Mar 2019 21:02:32 +0000 https://blog.tekspace.io/index.php/2019/03/31/manage-docker-images-locally-and-in-remote-container-registry/ Managing Docker images is very important. Just as similar to managing application source code in a version controlled repository such as GIT. Docker also provides similar capabilities. Docker images can be managed locally on your development machine and also on remote container registry also known as Docker hub. In this tutorial, I will demonstrate a

The post Manage Docker Images locally and in remote Container Registry appeared first on TEKSpace Blog.

]]>
Managing Docker images is very important. Just as similar to managing application source code in a version controlled repository such as GIT. Docker also provides similar capabilities. Docker images can be managed locally on your development machine and also on remote container registry also known as Docker hub.

In this tutorial, I will demonstrate a set of commands on how to manage Docker images both locally and remotely.

Prerequisite

Managing images locally

1. List Docker Images

To view Docker images locally, you can type Docker images, and it will list all the images in the console. Execute the below command from an elevated PowerShell or command line tool to see the output.

docker images

Output

REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
demo/webappcore                        2.2.0               9729270fe1ac        38 hours ago        401MB
<none>                                 <none>              2822fcdec81d        38 hours ago        403MB
<none>                                 <none>              807f7b4b42c1        38 hours ago        398MB
<none>                                 <none>              659fbabfde96        38 hours ago        398MB
<none>                                 <none>              ad0df2c81cf1        38 hours ago        397MB
<none>                                 <none>              97a33d1a133d        38 hours ago        395MB
mcr.microsoft.com/dotnet/core/aspnet   2.2                 36e5a01ef28f        3 days ago          395MB
hello-world                            nanoserver          7dddd19ddc59        2 months ago        333MB
nanoserver/iis                         latest              7eac2eab1a5c        9 months ago        1.29GB

NOTE: I am using Windows 10 to demonstrate Docker image management.

Next, we will limit the list and only print images by repository name. The command is Docker images [repository name].

docker images hello-world

Output

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         nanoserver          7dddd19ddc59        2 months ago        333MB

You can also add an addition filter to the above command by providing tag. The command is docker images [repository name]:[tag].

docker images hello-world:nanaserver

2. Deleting Docker images

Sometime you may want to just delete an image that you no longer need. By deleting the image, it will permanently delete an image from your local. The command is docker rmi [image name]:[tag].

NOTE: There are many ways to delete an image. You can also reference Docker official website.

docker rmi hello-world:nanoserver

NOTE: If you see an error like this “Error response from daemon: conflict: unable to remove repository reference "hello-world:nanoserver" (must force) - container 3db17e64fadb is using its referenced image 7dddd19ddc59“. It means there is a container running locally on your machine, and it has been referenced.

To remove it forcefully, you can apply -f at the end of the above command. It will stop the running container and delete the image.

docker rmi hello-world:nanoserver -f

You should see almost similar output shown below.

PS C:\> docker rmi hello-world:nanoserver -f
Untagged: hello-world:nanoserver
Untagged: hello-world@sha256:ea56d430e69850b80cd4969b2cbb891db83890c7bb79f29ae81f3d0b47a58dd9
Deleted: sha256:7dddd19ddc595d0cbdfb0ae0a61e1a4dcf8f35eb4801957a116ff460378850da

Now, let’s go ahead and execute docker images. You will see your image was successfully deleted.

PS C:\> docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
demo/webappcore                        2.2.0               9729270fe1ac        38 hours ago        401MB
<none>                                 <none>              2822fcdec81d        38 hours ago        403MB
<none>                                 <none>              807f7b4b42c1        39 hours ago        398MB
<none>                                 <none>              659fbabfde96        39 hours ago        398MB
<none>                                 <none>              ad0df2c81cf1        39 hours ago        397MB
<none>                                 <none>              97a33d1a133d        39 hours ago        395MB
mcr.microsoft.com/dotnet/core/aspnet   2.2                 36e5a01ef28f        3 days ago          395MB
nanoserver/iis                         latest              7eac2eab1a5c        9 months ago        1.29GB

Managing Docker images on remote container registry

In this tutorial, we will use Docker hub, which gives you one free private repository and unlimited public repositories. In this tutorial, I will demonstrate how to use a private container registry that is only accessible to you or your organization.

Step 1: First, we need to register to get access to Docker hub. Once you have successfully registered and signed in. We are now ready to create our first private repository for Docker images.

Step 2: In the below example, I will show you how to push demo/webappcore image to remote repository. Execute the below command to push Docker image to Docker hub.

Login to Docker hub.

docker login --username tekspacedemo

Then it will prompt you for password. Go ahead and type the password.

NOTE: Replace tekspacedemo with your registered docker id.

Before we move on to push the local image to remote registory. We need to tag it to remote path so that we can push that to docker hub.

docker tag demo/webappcore:2.2.0 tekspacedemo/demo:2.2.0

The above command will tag local Docker image path demo/webappcore:2.2.0 to the path that matches for remote repository path, which in my case is tekspacedemo/demo:2.2.0.

Now we will push a local image from tekspacedemo repository.

docker push tekspacedemo/demo:2.2.0

NOTE: for the demo purpose, I created a remote repo name called demo. You can use any name to define repository name.

After your image is pushed to remote container registry. Your output should look like below

PS C:\> docker push tekspacedemo/demo:2.2.0
The push refers to repository [docker.io/tekspacedemo/demo]
a034775f3ab9: Pushed
7e886042ad70: Pushed
6c8276f92903: Pushed
596811bf044f: Pushed
84ff941997ac: Pushed
673fa658bebd: Pushed
75932c99c074: Pushed
3d57d631c3a7: Pushed
63077ec902e9: Pushed
f762a63f047a: Skipped foreign layer
2.2.0: digest: sha256:161bdf178437534bda10551406944e1292b71fa40075f00a29851f6fd7d7d020 size: 2505

That’s it! You have successfully pushed your first local repo to a remote container repository. Thank you for following this tutorial. Please comment below and feel free to share your feedback.

The post Manage Docker Images locally and in remote Container Registry appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/manage-docker-images-locally-and-in-remote-container-registry/feed/ 0
Building your first docker image for Windows https://blog.tekspace.io/how-to-create-docker-image-for-windows-containers/ https://blog.tekspace.io/how-to-create-docker-image-for-windows-containers/#respond Sat, 30 Mar 2019 01:37:45 +0000 https://blog.tekspace.io/index.php/2019/03/30/how-to-create-docker-image-for-windows-containers/ In this tutorial, I will demonstrate how to host an ASP.NET Core 2.2 application on Windows Containers by using a Docker image. A Docker image will be packaged with an ASP.NET Core application that will be run when a container is spun up.Before we get started with creating a Docker image. Let’s make sure we

The post Building your first docker image for Windows appeared first on TEKSpace Blog.

]]>
In this tutorial, I will demonstrate how to host an ASP.NET Core 2.2 application on Windows Containers by using a Docker image. A Docker image will be packaged with an ASP.NET Core application that will be run when a container is spun up.
Before we get started with creating a Docker image. Let’s make sure we have prerequisites done.

Prerequisites

Once you have the prerequisites, we will use a publicly available ASP.NET Core base image from Microsoft. Microsoft maintains their Docker images on Docker hub. Docker hub is a container registry to manage your Docker images either by exposing the image publicly or maintaining it privately. Private image responsibilities cost money. Visit Docker Hub website to learn more about image repository management.

Building your first Docker Image

Step 1: Open the PowerShell console as an administrator

Step 2: Let’s get started by pulling ASP.NET Core 2.2 Docker image from Docker hub by executing the below command.

docker pull mcr.microsoft.com/dotnet/core/aspnet:2.2

Your output should look similar to what is shown below:

Step 3: Create a folder with your preference name whatever you prefer. I will use c:\docker\ for demonstration purposes.

mkdir c:\docker

Step 4: Download ASP.NET Core application package from this URL.

Invoke-WebRequest -UseBasicParsing -OutFile c:\docker\WebAppCore2.2.zip https://github.com/rahilmaknojia/WebAppCore2.2/archive/master.zip

What we are doing in the above command is downloading packaged code that is already built to save time on building a package.

Step 5: Extract WebAppCore2.2.zip by using the PowerShell 5.0 native command. If you do not have PowerShell 5.0 and above, you will have to manually extract the package.

Expand-Archive c:\docker\WebAppCore2.2.zip -DestinationPath c:\docker\ -Force 

Step 6: Now let’s create a Docker file in c:\docker folder.

New-Item -Path C:\docker\Dockerfile -ItemType File

Step 7: Go ahead and open C:\docker folder path in Visual Studio Code.

Step 8: Now we will open Dockerfile by double-clicking on the file in Visual Studio Code to start writing the required steps to build an image.

Copy and paste the code below into Dockerfile.

# Pull base image from Docker hub 
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2

# Create working directory
RUN mkdir C:\\app

# Set a working directory
WORKDIR c:\\app

# Copy package from your machine to the image. Also known as staging a package
COPY WebAppCore2.2-master/Package/* c:/app/

# Run the application
ENTRYPOINT ["dotnet", "WebAppCore2.2.dll"]

What we told the Dockerfile is to pull an asp.net core base image from Docker hub. Then we ran a command to create a directory called app in c:\app path. We also told the container to set c:\app as a working directory. That way we can access binary directly when the container is spun up. We also added a step to copy all the binaries from c:\docker\WebAppCore2.2-master\Package\ to destination path in container c:\app. Once we had the package staged in the container, we told it to run the application by executing dotnet WebAppCore2.2.dll so that the app would be accessible from outside the container. To learn more about Dockerfile for Windows, check out this Microsoft documentation.

Now that you have the required steps to build an image, let’s go ahead with the below steps.

Step 9: Navigate to Dockerfile working directory from PowerShell console. If you are already in that path, you can ignore it.

cd c:\docker

Step 10: Execute the below command to build a container image.

docker build -t demo/webappcore:2.2.0

The above command will create a Docker image under demo path. With the image name called as webappcore and version 2.2.0.

Your output should look like below once it is successful:

PS C:\docker> docker build -t demo/webappcore:2.2.0 .
Sending build context to Docker daemon  9.853MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
 ---> 36e5a01ef28f
Step 2/5 : RUN mkdir C:\\app
 ---> Using cache
 ---> 8f88e30dcdd0
Step 3/5 : WORKDIR c:\\app
 ---> Using cache
 ---> 829e48e68bda
Step 4/5 : COPY WebAppCore2.2-master/Package/* c:/app/
 ---> Using cache
 ---> 6bfd9ae4b731
Step 5/5 : ENTRYPOINT ["dotnet", "WebAppCore2.2.dll"]
 ---> Running in 4b5488d5ea5f
Removing intermediate container 4b5488d5ea5f
 ---> 9729270fe1ac
Successfully built 9729270fe1ac
Successfully tagged demo/webappcore:2.2.0

Step 11: Once the image has been built, you are now ready to run the container. Execute the below command.

docker run --name webappcore --rm -it -p 8000:80 demo/webappcore:2.2.0

The above command will create a new container called webappcore with parameters.

  • --rm is used to automatically remove the container after it is shutdown.
  • -it will open a session into your container and output all the logs.
  • -p is used for creating an external port and assigning it to the internal port of a container. Port 8000 is exposed to outside containers, and port 80 is used to access the app within the container.
  • demo/webappcore:2.2.0 is the path to the Docker image to run as a container.

Output of a running container

Step 12: Browsing your application from your local machine localhost:8000.

This is it! You ran your first Docker container in your local environment. Thank you for following the tutorial. Please comment below for any issue or feedback you would like to share.

The post Building your first docker image for Windows appeared first on TEKSpace Blog.

]]>
https://blog.tekspace.io/how-to-create-docker-image-for-windows-containers/feed/ 0