DEV Community

Cover image for Intro to Docker - Step by Step
Mohamad Lawand
Mohamad Lawand

Posted on

Intro to Docker - Step by Step

In this article I will be giving you an introduction about Docker.

You can watch the full video on Youtube:

So what we will cover today:

  • What is Docker
    • What is a Container
    • What is an Image
    • What is a Dockerfile
    • What is a Container Registry
  • Difference between Docker and VM
  • Docker setup
  • Will Create our first Docker image

Please like, share and subscribe if you like the video. It will really help the channel

Whats is docker:

A container Management System which allows us to package an application with all its dependency and configurations

the package which is a created can be easily moved and shared

Docker is used for automating deployment on the cloud or on-premise

Docker containers can run anywhere

what is a container:

  • A container is a way to package an application with all of its dependency and configurations
  • the package which is a created can be moved around from one machine to another or from one server to another and easily shared
  • The ability to manage, package and configure a container is one of the main benefits of using docker
  • All containers are based on minimal OS, mostly on Linux environment

container repository

Containers are stored in container repository it could be a private or public one

  • Example of public repository is docker hub for docker container

Whats a image

An image is the application binary, and dependencies with the meta data on how to run it. In essence it's the binary that the application needs because the host provides a kernel. We are just starting a single file

Images derives from base images, for example if i am building a .Net application i need to have the .Net SDK image as a base image or if i am building a node app i need the nodejs image.

Difference between Image and Container

An image is the binaries and libraries and source code that that makes our application.

A container is a running instance of that image, we can have many containers based of the same image

Alt Text

Whats is a Docker file

It is the set of instruction that we write to build, run and configure our application inside our Docker container.

What is a Container Registry

The Registry is a stateless, highly scalable server-side application that stores and lets you distribute Docker images.

Hosting all the images in one stored location allows users to commit, identify and pull images when needed.

We can build our own images and we can also get images from registries. Similar to Github, where we can find source code, Docker hub (hub.docker.com) is the registry where we can find images.

How everything fits together

Alt Text

Difference between VM and Docker

Virtual Machine

  • Heavyweight
  • Limited performance based on hardware virtualisation
  • Every VM requires its own OS
  • Higher hardware requirement for virtualisation
  • App communication is harder due to App segregation

docker

  • Lightweight
  • Native performance
  • Shared OS across all containers
  • Runs on every machine with a modern OS
  • Very easy to create networks and facilitate communication between Apps

How to setup Docker

Download it from (https://www.docker.com/products/docker-desktop)

Open an account on Docker Hub: (https://hub.docker.com/)

Hello World, Docker

Alt Text

  1. docker daemon will look locally to see if the image exist
  2. if the image is located it creates a container and starts it
  3. if the image doesn't exist it will communicate with docker hub
  4. look for the image on docker hub
  5. once the image is found on docker hub, if the request doesn't specify a version of the image the latest version is selected
  6. it is downloaded locally to the computer
  7. docker daemon will create a container of this image and starts it

Now let's jump into coding

Open the terminal type the command below

dotnet --version
Enter fullscreen mode Exit fullscreen mode

Now we need to create an asp.net core application using the following command

dotnet new mvc -n "DockerSample"
Enter fullscreen mode Exit fullscreen mode

Now we need to build the project and run it to make sure everything is working as it should

dotnet build
dotnet run
Enter fullscreen mode Exit fullscreen mode

As we can see the application is running, we need now to stop the application and add our Docker file. the Dockerfile will be added in the root directory of our application the D in Dockerfile must be capitalised. Once we add it we can see the whale icon.

# Get the base image
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

# Copy the csproj and restore all of the nugets
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/sdk:5.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerSample.dll"]
Enter fullscreen mode Exit fullscreen mode

As well lets update the program.cs class to force Asp.Net Core to utilise the port 80.

webBuilder.UseUrls("http://*:80");
Enter fullscreen mode Exit fullscreen mode

Build the docker file and make sure it run

docker build -t sampledocker .
docker run -p 8008:80 sampledocker
Enter fullscreen mode Exit fullscreen mode

Top comments (0)