Skip to content

Latest commit

 

History

History
130 lines (103 loc) · 3.6 KB

00.docker.md

File metadata and controls

130 lines (103 loc) · 3.6 KB

Hands-on Lab - Day 0

Exercise 1 - Understanding docker commands

In this exercise, you will pull a couple of Docker images and run them as containers.

The first image will be Nginx. Nginx is a free, open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache.

The second one will be Apache Tomcat, an open-source implementation of Java Serverlet and Java Server Pages technologies.

Task 1 - Download the Nginx image

docker pull nginx

Task 2 - Check that you have the image locally

docker images

Task 3 - Run a container with the Nginx image

docker run -d -p 8080:80 --name mynginx nginx

Task 4 - Check your container is running

With your browser go to the ip address of your VM and check port 8080

http://<ip address of your VM>:8080

Task 5 - Check your container is running

docker ps -a

Task 6 - Stop the Nginx container (mynginx)

docker stop mynginx

Task 7 - Run Tomcat 8.0 (interactively)

docker run -it --rm -p 8080:8080 tomcat:8.0

Note that this time your container is running interactively. If you want to stop it press Ctrl+C

Task 8 - Check that Tomcat is running

With your browser, go to the IP address of your VM using port 8080

Task 9 - Remove all your containers

docker rm $(docker ps -a -q)

Exercise 2 - How to Containerize an application

In the second exercise, you will create a new application image. First, you will get the application code, and then, using a Dockerfile, you will create a new image.

Task 1 - Clone a sample application

git clone https://github.com/carlosalexei/demoapps.git

If you do not have git installed you can navigate to the GitHub Repository and download a ZIP file with the application code.

Task 2 - Explore the application

cd demoapps/HelloWorld

Take a moment to check this is a simple MVC .NET application

Task 3 - Check the Dockerfile file

# Define base image that includes dev tools
FROM mcr.microsoft.com/dotnet/sdk:6.0 as DevEnv

# Create a folder
WORKDIR /app

# Copy the project file
COPY *.csproj ./

# Restore dotnet libraries for the project
RUN dotnet restore

# Copy all application files
COPY . ./

# Compile project and create production binaries
RUN dotnet publish -c Release -o /app/out

# Download another image with the ASP.NET runtime
FROM mcr.microsoft.com/dotnet/aspnet:6.0

# Copy from the first image (DevEnv) binaries
COPY --from=DevEnv /app/out .

# Define the process to be executed in the container
ENTRYPOINT [ "dotnet", "HelloWorld.dll" ]

Task 4 - Build the image

docker build -t helloworld:1.0 .

Task 5 - Run the container

docker run -d -p 8080:80 helloworld:1.0

Task 6 - Run the container

Go to https://hub.docker.com and Sign up to have your image repository there.

Task 7 - Tag your image

docker tag helloworld:1.0 <<your account>>/helloworld:1.0

e.g., docker tag helloworld:1.0 carlosalexei/helloworld:1.0

Task 8 - Login into your registry

To be able to publish your image to the repository, you need to provide the right credentials. Use the following command to provide the name of your docker hub account and password (Created on task 6).

docker login

Task 9 - Push your image to the registry

docker push <<your account>>/helloworld:1.0

Task 10 - Check your repo and complete documentation

Go to https://hub.docker.com and ensure you provide the documentation for people to use your image. They will use:

docker pull <<your account>>/helloworld:1.0