• author: Nick Chapsas

How to Run Applications Using Docker Compose and Docker file

In today's world of web development, one of the most valuable skills anyone can possess is the ability to deploy applications using Docker. With containerization, we can easily bundle our application along with any dependencies needed, regardless of the machine they are running on.

This article will be a tutorial on how to run web applications on Docker using a Docker compose file and Docker file.

Docker File

The Dockerfile is a collection of instructions that essentially builds our website in Docker and runs it. Here are the steps to create a Dockerfile:

  1. Open the root of your project in the terminal
  2. Create a new Dockerfile in this directory with the following commands:

    touchDockerfile
    
  3. Open the Dockerfile and copy/paste the following boilerplate code:

    # Use the .NET Core SDKFROMmicrosoft/dotnet:2.2-sdkASbuild-env# Set the container working directory to /appWORKDIR/app# Copy the project directory in to the containerCOPY../
    
    # Restore packages and build our projectRUNdotnetrestore
    RUNdotnetpublish-cRelease-oout
    
    # Use the .NET Core runtime to launch our appFROMmicrosoft/dotnet:2.2-aspnetcore-runtimeWORKDIR/appCOPY--from=build-env/app/out.
    ENTRYPOINT["dotnet","Twitter.dll"]
  4. Save and close Dockerfile

Docker Compose

While the Dockerfile can be used to build our web application, it does not handle dependencies. For that reason, the recommended approach is to use a Docker Compose file, which contains everything we need to spin up our application and its dependencies in isolation.

With Docker Compose, we can define all the services, networks, and volumes needed by our application in a single file. Here are the steps to create a Docker Compose file:

  1. Create a new file named docker-compose.yml in your solution level.

    touchdocker-compose.yml
    
  2. Open the docker-compose.yml file and paste in the following boilerplate syntax:

    version: '3.5'
    
    networks:
      localdev:
        driver: bridge
    
    services:
      mainAPI:
        build: 
          context: ./YourAppName
          dockerfile: Dockerfile 
        restart: always
        ports: 
          - "7000:80"
        depends_on:
          - dbServer
        networks: 
          - localdev
    
      dbServer:
        image: microsoft/mssql-server-linux:2017-latest
        container_name: dbServer
        environment:
          ACCEPT_EULA: Y
          SA_PASSWORD: "YourStrong!Passw0rd"
          MSSQL_TCP_PORT: "1433"
        ports:
          - "1433:1433"
        networks: 
          - localdev
    

    *Note: Replace 'YourAppName' with the actual name of your application. Also, ‘YourStrong!Passw0rd’ is just an example of a strong password. Use whatever password you prefer for your installation.

  3. Save the docker-compose.yml file.

Running the Application on Docker

With the Dockerfile and the docker-compose.yml file created, it's time to build and run the application on Docker. Here are the steps:

  1. Open the root of your project in the terminal
  2. Build the Docker image by running the following command:

    docker-composebuild
    

    This command will build the images for both the API and the database specified in your docker-compose file.

  3. Once image building is finished, we can run the Docker container by executing this command:

    docker-composeup
    

    This command will run the API at http://localhost:7000

  4. Stop the Docker container by pressing Control+C
  5. To delete our Docker containers and images from our system, run the following command:
    docker-composedown
    

Conclusion

In conclusion, with Docker, it's easy to almost effortlessly deploy and run our web applications quickly and securely. The Docker Compose file takes the complexity out of spinning up our application's services and dependencies, allowing us to streamline our development process. Now that you understand how Docker Compose and Dockerfiles work, you can easily integrate Docker into your current or future projects.

Previous Post

Three Formerly Used .NET Best Practices and Why I Don't Use Them Anymore

Next Post

Building Your Business Presence Online with Analytics

About The auther

New Posts

Popular Post