Using Docker Containers

CS296N Web Development 2: ASP.NET

Weekly Topics
1. Intro to course and Input Validation
6. Load Testing and Performance
2. Repositories and Unit Testing
7. Creating a Web Service
3. Claims and Third Party Authentication
8. Consuming a Web Service
4. Web Security
9. Docker Containers
5. Publishing to a Production Server 10.  Term Project



Announcements

  • Term Project

Solving the "but it worked on my machine" problem.

Set up Docker

You will need to install the Docker environment on your development machine and on the server. There are several versions of the development environment. You need to choose the one that matches your operating system:

Deploy a Web App in a Container

On your development machine:

We will deploy a framework-dependent ASP.NET Core web app to Linux in a Docker container.

  1. Publish your web app to a folder. Use your Azure database connection string. Verify that the web app runs from the folder you published it to by executing this command:
    dotnet yourwebappname.dll

  2. Create a file named Dockerfile in your web app solution folder (the same folder as your .sln file). Example:
    # download the ASP.NET Core runtime
    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

    • FROM—downloads an image from the Microsoft Docker image repository, initializes a new build stage, and sets the Base Image for the remaining instructions.

  3. Build the image and give it a name, following the -t (The tag is optional and could be given in the ‘name:tag’ format.) The dot means that the Dockerfile is in the current folder. Execute this command in the folder containing the Dockerfile:

    docker build -t yourwebappname . 
  4. To see the images currently installed, type:
    docker images

  5. Add commands to the Dockerfile to copy the web app into the container and set an entry-point for the web app. This will create new image layers.# Copy files from the dev machine to the container.
    COPY GoodBookNook/bin/Release/netcoreapp3.1/publish/ app/

    # Change directories, we need to run the app from the app directory.
    WORKDIR app/

    # Start the web app when the container runs.
    ENTRYPOINT ["dotnet", "GoodBookNook.dll", "--environment=Production"]

    1. COPY—copies new files or directories from the source path and adds them to the destination container filesystem.
    1. ENTRYPOINT—specifies the command to run when the container starts, dotnet, and any arguments.

  6. Build your images again as you did in step 3 and view them again too.
  7. Create a container using the image you just created:
    docker create yourwebappname
    The output of the command shows the ID of your new container. To see a list of all containers, type:
    docker ps -a
    You'll notice that each container has a name that was assigned automatically.

  8. Start the container using it's name:
    docker start some_name
    FYI, you can use the docker stop command to stop the container.

  9. Run the app
    docker run --rm -p 5001:8080 yourwebappname
    Notes:
    • I set the port option to map port 5001 inside the container to 8080 outside the container since my web app is configured for HTTPS on port 5001.
    • If you are specifying a port in appsettings.json, be sure you don't specify localhost. Use + to represent any domain, like this:  "http://+:5001"
  10. Upload the image to Docker Hub or any other container registry.

On the server:

  1. Blah blah

Troubleshooting

You can connect a terminal to your container in order to see what's going on inside.

  1. Start the container using it's name:
    docker start container_name
    Notes:
    • You can stop your container using the command: docker stop
    • You can get the names of your containers using the command: docker ps -a
  2. Use the docker attach command.
    docker attach --sig-proxy=false container_name
    Notes:
    The CTRL + C keystroke is used to detach from the running container. This keystroke may actually end the process in the container, which will stop the container. The --sig-proxy=false parameter ensures that CTRL + C won't stop the process in the container.

Notes


Docker Toolbox for Windows


Example

  • BookInfo—Docker branch
    Web app with:
    •  Dockerfile
    • Startup code for an optional in-memory DB selected by environment variable
    • Connection string for MariaDB
    • Other modifications to make the app containter friendly

References