Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a VIsual Studio Docker container from the command line? Works in VS, but not on command line

Using the VIsual Studio 2017 Out-of-the-Box (OOB) docker support, I am able to build and run my container in my Visual Studio solution by using the F5 button - this fires up my website container successfully, and I see my website! I notice that it is using docker-compose to do this (SUCCESS).

BUT

I want to understand how to run up the container from the command line, without Visual Studio.

First, I build my images in Release mode (Visual Studio). This produces TWO new images (docker images shows one with no name, and one with the name I defined in docker-compose.yml). [Question: why is the extra image created?)

docker-compose.yml:

services:
  mssql:
    image: microsoft/mssql-server-linux:2017-latest
    container_name: sql1
    ports:
      - 1433:1433
    environment:
      SA_PASSWORD: "password"
      ACCEPT_EULA: "Y"
      MSSQL_PID: Developer
  st.web:
    image: myrepo/stweb:1.0.0.1
    build:
      context: .
      dockerfile: ST.Web/Dockerfile

Dockerfile

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY SupportTicket.sln ./
COPY ST.Web/ST.Web.csproj ST.Web/
COPY ST.AppServicesLib/ST.AppServicesLib.csproj ST.AppServicesLib/
COPY ST.SharedInterfacesLib/ST.SharedInterfacesLib.csproj ST.SharedInterfacesLib/
COPY ST.SharedEntitiesLib/ST.SharedEntitiesLib.csproj ST.SharedEntitiesLib/
COPY ST.SQlServerRepoLib/ST.SQLServerRepoLib.csproj ST.SQlServerRepoLib/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/ST.Web
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ST.Web.dll"]

Now I want to run it up from the command line.

  1. docker-compose up (I run this from the command line, from the Solution Directory). This fires up the container, and says

    Now listening on: http://[::]:80

But I am unable to browse to the website. Where is the URL (it says [::]:80 --> looks cryptic)? I try http://localhost:80 but I get This site can’t be reached localhost refused to connect.

  1. I try docker run -it myrepo/mycontainer:1.0.0.0 --publish 80:80 and I get the same as above This site can't be reached etc.

  2. I try docker run -it myrepo/stweb:1.0.0.1 -p 80:80 and get The short switch '-p' is not defined in the switch mappings.

  3. I try docker build from the Solution Directory. My solution contains the Dockerfile within my Web App project. The solution builds fine and is able to copy files. But docker build is unhappy and can't find the solution file. I use, from the Solution Directory:

    docker build MyWebAppDirectory

I get COPY failed: stat /var/lib/docker/tmp/docker-builder753352966/MyApp.sln: no such file or directory

But if I copy the Dockerfile (which sits in the Web App project to the Solution Dir, it works!). The OOB Dockerfile is situated in the Web App directory. But this doesn't work with the command line in any of the permutations I tried. I needed to move the DockerFile out of the Web App Project. But this breaks the Visual Studio build!

QUESTIONS:

  1. How do I browse to a website using docker-compose up with the Visual Studio 2017 OOB docker support, from the command line? (What is/are the correct procedure/steps and URL?)

  2. Any ideas on how to run up my container/s (specifically, the website container) form the command shell? (I am particularly interested in docker run, as well as docker-compose)

  3. Any ideas on "The short switch '-p' is not defined in the switch mappings."?

  4. What is the correct location of the Dockerfile for a particular project? SolutionDir or project dir?

  5. Why is an extra unnamed image created when I build in Release mode? I expect to see only one output.

Many Thanks!

like image 488
Banoona Avatar asked May 29 '18 19:05

Banoona


1 Answers

Running your application from the command-line

# Build
docker-compose build
# Run
docker-compose up

Now browse to localhost:{port} in your browser. (You should be able to find the {port} configured in a docker-compose.override.yml file.)

Running the web container

  1. Build the container:

    docker build -f path/to/Dockerfile . -t virasana/stweb
    

    The syntax for docker build is docker build <context>. Since the context is the solution directory, the docker build command must be run from there. The -f flag is used to specify the Dockerfile to use.

    I prefer to keep the Dockerfile in the project it logically belongs to. This is also your only choice if you need to have multiple containers in the same solution.

  2. You can now run the container using:

    docker run -it --rm -p "5500:80" virasana/stweb
    

    Browse to localhost:5500 to see the site.


Why is an extra unnamed image created when I build in Release mode? I expect to see only one output.

You have two images: mssql & st.web.
st.web is missing a container_name.

like image 111
galdin Avatar answered Oct 22 '22 01:10

galdin