Create a Console app and run in a Container with Visual Studio 2022

I couldn’t find an article on this topic . so i thought i will put one .

Now there are many ways to interact with the container .You can do it with visual studio , Docker Desktop /terminal window
The following shows how to interact with the console app running in container using visual studio .
click the button shown below to open the terminal window .

It opens the power shell in the /app directory
cd in to the the directory that has the dll file .Fo e.g in this case it will be
cd /app/bin/Debug/net9.0/
dotnet <.dllfile>
Now you will be able to interact with it


Another way to do it is to build the image from the docker file generated by visual studio, as shown below

so try to open a separate terminal window to interact with the container .

here i built the image from the docker file visual studio generated and then started a container from it.using the following command
D:\LocalProjects\2024\DotNetConsoleApps\JuiceShop.Solution>docker buildx build -t juiceshop:v2 -f ./JuiceShop/Dockerfile .
The following is the output of theabove command

Once the above command is successful , it created a container image .

if you have installed docker desktop you could see this local image .

Now, to spin a container from the image , you can execute the following command in the terminal
docker run -it –name “zzzz” juiceshop:v2

if you don’t specify –name followed by container name (in this example zzzz) then docker gives an arbitrary name to the container.

Run the container
docker run -it –name “zzzz” juiceshop:v2
Hello please enter your name ?
Ethan Hunt
hello Ethan Hunt


Running a local SQL Server Express in Docker Container

The following describes how to run SQL Server Express instance in a Docker Container .The quick way to ‘install’ a SQL Express, is to run a Docker container from the mssql-server-linuximage. You can start one by running the following command from cmd or powershell.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Pass@word" -e "MSSQL_PID=Express" -p 1433:1433 -d --name=mssqlexpress  mcr.microsoft.com/mssql/server:latest

The above command will start a background Docker container named ‘sqlsrvr’. The SA password is Pass@word

From any application , you may connect using this connection string

Server=localhost,1433;Initial Catalog=Testdb;Integrated Security=True;User Id=sa;Password=Pass@word

Also , the container exposes the container port 1433 to the localhost on 1433, which is used for communication if this conflicts with your setup / any other SQL instance you have running on your local machine , you may either stop the running SQL instance using services.msc or try to run the container on another host port.

For e.g the below code we are mapping the SQL server port 1433 to 1439 on the host machine

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyPass@word" -e "MSSQL_PID=Express" -p 1439:1433 -d --name=mssqlexpress mcr.microsoft.com/mssql/server:latest

Connecting via SSMS should be fairly straightforward

connected

Starting and Stopping the sql container

docker start mssqlexpress
docker stop mssqlexpress

The running sql container is volatile i.e if you remove the container, the data is gone with it.

To persist your data, you need to mount it to a Docker volume / docker mount

A Docker volume maps a directory within a container file system to a path on the host file system . In the sample below, we’ll map the d:\LocalDB\SQLExpress\Datafiles on a Windows host to the datafolder from the mssql-server-linux container, on path/var/opt/mssql/data

for e.g below is the command and followed by it’s explanation

  • docker run: This is the command to create and start a new Docker container.
  • -e "ACCEPT_EULA=Y": This sets an environment variable ACCEPT_EULA to Y, which indicates that you accept the End User License Agreement.
  • -e "SA_PASSWORD=pass@word1": This sets another environment variable SA_PASSWORD with the value pass@word1. This password will be used for the SQL Server sa (system administrator) account.
  • -e "MSSQL_PID=Express": This sets the MSSQL_PID environment variable to Express, specifying that you want to use the SQL Server Express edition.
  • -p 1433:1433: This maps port 1433 of your host machine to port 1433 of the Docker container. SQL Server listens on port 1433 by default.
  • -d: This runs the container in detached mode, which means it will run in the background.
  • --name=mssqlexpress: This assigns a name to the container, which in this case is mssqlexpress.
  • --mount type=bind,source=d:\\LocalDB\\SQLExpress\\Datafiles,target=/var/opt/mssql/data: This binds a directory from your host machine (d:\\LocalDB\\SQLExpress\\Datafiles) to a directory inside the container (/var/opt/mssql/data). This allows the container to use the data files from your host machine.
  • mcr.microsoft.com/mssql/server:latest: This specifies the Docker image to use, which in this case is the latest version of the SQL Server image from the Microsoft Container Registry.

You can now back up database files and use it to restore the back up files created at your mounted path d:\\LocalDB\\SQLExpress\\Datafiles

Mount Local file on windows to a Docker container

Imagine you have a web application container that needs to access configuration settings stored in a file on your host system. This file might contain sensitive information like database credentials or API keys. Storing such sensitive data directly within the container image can lead to security risks, particularly when sharing the image. To mitigate this issue, Docker provides storage options that help bridge the gap between container isolation and your host machine’s data.

Docker offers two primary storage options for persisting data and sharing files between the host machine and containers: volumes and bind mounts.

Here we will use the bind mount

docker run -it --mount type=bind,source=d:\\MyFolder\\temp,target=/app/data wordcounterapp:latest -s /app/data/config

e.g The above is the example of a console app running in a container . It counts the words in a sentence . I ve put a sentence in a file on my d drive in windows and mounted it to container and passed the argument 

I have a windows 11 host machine so my example is the use case where i have file in my local machine in a directory called "temp" and i want to mount it to a container .so i will mount the content of the directory on my source system to the container file system.
so in the above command , the source is d:\\MyFolder\\temp
target is /app/data/config

Note: Please make sure that file path is correct and there is no spaces for source and target values. for e.g
the space shown below between cmdline argument and value will also give error

create a dev container using vscode and push it docker hub

The following article describes how to create dev containers using vscode and publish it to docker hub .

1. Install Prerequisites

  • Docker: Install Docker Desktop from here.
  • VS Code: Install Visual Studio Code from here.
  • Remote – Containers Extension: Install the Remote – Containers extension in VS Code. You can find it in the Extensions view in VS Code .

2. Create a Dev Container Configuration

  1. Open your project in VS Code.
  2. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the Command Palette.
  3. Type and select Remote-Containers: Add Development Container Configuration Files.
  4. Choose a predefined container configuration that suits your project’s needs or customize your own.

This will create a .devcontainer folder with a devcontainer.json file in your project.

3. Customize devcontainer.json

Edit the devcontainer.json file to include any specific tools, extensions, or settings your project requires. Here’s an example:

4. Build and Open the Dev Container

  1. Reopen the Command Palette and select Remote-Containers: Reopen in Container.
  2. VS Code will rebuild the container and reopen your project inside it.

we could see the vs code opening the code in dev container

you can see that that the container has loaded and i am able to run the code from within the container

5. Push the Container to Docker Hub

we need to first make sure that we are authenticated successfully in the docker hub .so
First, log in to Docker Hub from your terminal using:

Then here i will use dev container CLI to build and publish images .

  1. Install DevContainer CLI using Node JS package manager npm as follows :

2. Build and Push the Container

devcontainer build --workspace-folder <path_to_your_workspace> --push true --image-name <your_dockerhub_username>/<your_image_name>:<version>

Replace <path_to_your_workspace>, <your_dockerhub_username>, <your_image_name>, and <version> with your actual values.
Note : This command need to be execute in the host file system not inside container

Here’s a breakdown of that command:

  • devcontainer build: This is the main command for building a development container. It’s part of the Dev Containers specification, which allows you to define your development environment as code.
  • --workspace-folder <my_repo>: This flag specifies the root folder of your workspace. Replace <my_repo> with the path to your repository. For example, if your repository is in a folder named project, you would use --workspace-folder project.
  • --push true: This flag indicates whether the built image should be pushed to a container registry. By setting it to true, the image will be pushed after the build.
  • --image-name <your_dockerhub_username>/<my_image_name>:<optional_image_version>: This flag specifies the name (and optionally the version) of the image. Replace <your_dockerhub_username> with the docker user name which you can find it once you login to docker.com under the profile .
    Replace <my_image_name> with the desired name for your image. You can also include an optional version tag by replacing <optional_image_version>. For example, myapp:latest.

for example , refer the above command
i am executing the devcontainer command from the root directory of my repo that contains .devcontainer folder which makes it easy . Hence you will see that i ve indicated a “.” (dot) after the –workspace-folder parameter switch .

Once i execute command , since you have already authenticated docker earlier it will push your image to the docker hub and if it is successful , you will get the following :