0% found this document useful (0 votes)
29 views

LabWeek4 Tutorial DockerBasics

Uploaded by

Trupti Jogi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

LabWeek4 Tutorial DockerBasics

Uploaded by

Trupti Jogi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

SA Lab Week 4: Tutorial - Docker Basics

Introduction
What is Docker?
Image vs Container
What can Docker be used for?
What is a Docker Registry?
Setting Up
Install Docker Desktop on Linux
Install Docker Desktop on Mac
Install Docker Desktop on Windows
Enable Windows Subsystem for Linux (WSL) 2 feature on Windows
Install Docker Desktop for Windows
Run a test container
Installing Docker on Ubuntu
Exploring the Basics
Docker Images and Tags
Checking Available Images
Running a Container from an Image
Listing Active Docker Containers
Interacting with Docker Containers
Committing Changes to a Docker Container
Stopping a Running Docker Container
Deleting Docker Containers
Deleting Docker Images
Dockerizing your app with Dockerfile
Building an Image using Dockerfile
Sharing Images using an Image Library/Repository
Creating a new Repository on DockerHub
Connecting to DockerHub through Terminal
Pushing a Custom Image to DockerHub
References

Introduction
What is Docker?
Docker is a tool that promises to easily encapsulate the process of creating a distributable artifact for any
application, deploying it at scale into any environment, and streamlining the workflow and
responsiveness of agile software organizations.
Image vs Container
An image is a template of a container. One image can be used to build multiple containers. You can also
create multiple versions of an image by adding tags.
A container is a lightweight virtual environment that is created using an image. It is highly portable and
runs on top of the underlying OS kernel.

What can Docker be used for?


Containers are great for setting up automated CI-CD workflows. Docker makes it easy to configure consistent
environments using images and containers for development, testing, and production and is developer-friendly!

Consider the following example scenario:


Your developers write code locally and share their work with their colleagues using Docker containers.
They use Docker to push their applications into a test environment and execute automated and manual
tests. When developers find bugs, they can fix them in the development environment and redeploy them
to the test environment for testing and validation. When testing is complete, getting the fix to the
customer is as simple as pushing the updated image to the production environment.

What is a Docker Registry?


A docker registry such as DockerHub is a library or repository for storing, versioning and distributing
docker images.
DockerHub (https://hub.docker.com/) is a public docker registry where you can find official container
images and also upload your own custom images and setup private repositories. It can also be used to
automatically build and push an image each time you push new code to your source provider as part of
DevOps workflows.

Setting Up
Setting Up Docker Desktop is a one-click-install application for your Mac, Linux, or Windows environment
that enables you to build and share containerized applications and microservices. It provides a
straightforward GUI (Graphical User Interface) to manage your containers, applications, and images
directly from your machine. Docker Desktop can be used either on it’s own or as a complementary tool to
the CLI.

Install Docker Desktop on Linux


Follow the steps given at the below link to correct package for your Linux distribution:
https://docs.docker.com/desktop/install/linux-install/

Install Docker Desktop on Mac


Follow the steps given at the below link:
https://docs.docker.com/desktop/install/mac-install/

Install Docker Desktop on Windows


Steps include:
● Ensure you have Windows 10 version 1903 or higher, or Windows 11.
● Using the Windows Subsystem for Linux (WSL) 2 feature, install the Linux distribution.
● Download and install Docker Desktop for Windows.

You can also refer to https://docs.docker.com/desktop/install/windows-install/ for more information.


Enable Windows Subsystem for Linux (WSL) 2 feature on Windows
1. In the Windows Search bar, type “Turn Windows features on or off”, and open it.

2. Enable “Virtual Machine Platform”, and “Windows Subsystem for Linux”.


Make sure “Windows Hypervisor Platform” is disabled. Then click OK.

3. It will ask for a Restart. Restart your machine for changes to take effect.
4. Open PowerShell or Windows Command Prompt in administrator mode by selecting "Run as
administrator", enter the below command:
wsl --install -d Ubuntu-20.04

This command will enable the features necessary to run WSL and install the Ubuntu distribution of
Linux.
You'll be asked to wait for files to decompress and be stored on your machine.
Create a user account and password for your newly installed Linux distribution.

Once done, the terminal will start for your newly created Linux distribution.

5. List the installed Linux distribution and check the version of WSL it is set to using the below
command within PowerShell terminal:
wsl -l -v

wsl.exe -l -v
6. If the WSL version is 1, set the default version to WSL 2 using the below command:
wsl --set-default-version 2

Install Docker Desktop for Windows


1. Download Docker Desktop for Windows using the below link:
https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe
2. Start the installer.
Add the shortcut to the desktop.

It will start unpacking files.


Once installation is complete, close the wizard.

3. Start Docker Desktop using the shortcut created on the desktop. Accept the agreement.
Docker Desktop starting.

Once started, it will look similar to below.


Run a test container

Open PowerShell or Command Prompt and run the below command:


docker run -it -d -p 80:80 docker/getting-started

Check the Docker Desktop, you should see a container running.


Run hello-world
docker run hello-world

Hence, Docker Desktop was installed successfully.

Installing Docker on Ubuntu


1. Open the terminal for your EC2 Ubuntu instance.
2. Remove any Docker files that are running in the system, using the following command:
sudo apt-get remove docker docker-engine docker.io
After entering the above command, you will need to enter the password of the root user and press enter.

3. Get the system up-to-date using the following command:


sudo apt-get update

4. Install Docker using the following command:


sudo apt install docker.io
You’ll get a prompt asking you to choose between y/n - choose y
5. Start docker service and enable the service to start at boot using the following commands:

6. Before testing Docker, check the version installed using the following command:

docker --version
You can also check full version details for Docker Client and Server using:

sudo docker version

7. Pull an image from the Docker hub using the following command:
sudo docker run hello-world

Here, hello-world is an existing docker image which should start running in a new container.
Exploring the Basics
Docker Images and Tags
A docker image is identified by its image name which consists of two parts, name:tag.

The name is the general name of the image and will ultimately be the image repository name when and if
it is shared. An image name may contain lowercase letters, digits and separators, but it cannot contain
underscores.
It is optionally prefixed by a registry hostname, registry usename/image name.

e.g. For the image name appdynamics/sample-app-web, appdynamics is the username of the registry
host and sample-app-web is the name of the image and image repository name.

Docker tags convey useful information about a specific image version/variant. You can create multiple
images (a group of images) which share the same name, but have more specialized properties. Hence,
you can use a tag to define and identify a specialized version of an image. If a tag name is not specified
while creating an image, the default value is applied i.e. latest.

A tag name may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag
name may not start with a period or a dash and may contain a maximum of 128 characters.

Hence and image name shared through a registry will usually look like this: username/image name:tag name

e.g. appdynamics/sample-app-web:latest
e.g. atlassian/confluence-server:latest, atlassian/confluence-server:eap-jdk11,
atlassian/confluence-server:8.0.0- m90-ubuntu-jdk17 are versions of confluence-server image shared by
atlassian on DockerHub.
e.g. ubuntu:latest, ubuntu:jammy-20221101, ubuntu:22.10 are image names of ubuntu versions available
on DockerHub under the ubuntu repo.

Note: Docker commands need root privileges to be executed. Although you can login as root user and
run the commands directly, it is recommended to login as a non-root user for running these commands.
In the following sections, we are logged in as ’ubuntu’, hence the commands are prepended with ’sudo’ to
grant root access for the ubuntu user to run these commands. If you do not use sudo, you might face
access denied errors while running docker commands.

Pulling an Image from a Registry


Download a docker image by using the below command:
sudo docker pull <image name>:<tag>

e.g. sudo docker pull appdynamics/sample-app-web


Note: If the tag is not specified, the default tag i.e. latest will be used.
Checking Available Images
sudo docker images or sudo docker image ls

Running a Container from an Image


sudo docker run -it -p <port no.>:<container port no.> -d --name <new container name> <image
name>
e.g. sudo docker run -p 3000:3000 -d --name sample-app appdynamics/sample-app-web

If your container is hosting a web server or app like in the given example, you should be able to access it
through your local browser using http://[Your Public IP]:[Host port no.] as long as the specified host port
is exposed in your EC2 security groups.
Note:
• -d stands for detached mode which means the container will run in background and you can keep
running commands on the terminal
• -p or –publish is used for publishing the specified container port to the host.
• use –name option for giving a name to your container
• -it stands for interactive terminal mode
• each container will be assigned a unique id

Listing Active Docker Containers


The below command will list all running containers i.e. active containers with status as ’up’. Test if your
container process is running and mark its container id and name.
sudo docker ps

To list all active containers, run the following command:


sudo docker ps -a

Notice the values listed under ’Container ID’, ’Image’ and ’Names’ columns:
• The Container ID is a unique identifier given to a container.
• The Image column will display the name of the image used to run the container.
• The Name column displays a unique name for the container which can be assigned using the
name flag. if no name is provided by the user while creating/running a Docker container, Docker
automatically assigns the container a name.

Interacting with Docker Containers


You can open the bash terminal of your running container to interact with it using the below command:
sudo docker exec -it <container id or name> bash
Here you can try running shell commands, installing software etc within your container. To exit out of the
shell session, simply type exit.

Committing Changes to a Docker Container


You can commit a container’s file changes or settings into a new image by using the below command:
sudo docker commit <container id or name> <new image name>

Stopping a Running Docker Container


You can stop a running container gracefully using the below command:
sudo docker stop <container id or name>

Deleting Docker Containers


If you no longer want to use a container you can delete it using the below command. This will clear up the
memory allocated for the container as well.
sudo docker rm <container id or name>

To delete a running container by force, you can use the -f switch.


sudo docker rm -f <container id or name>

Deleting Docker Images


You can delete any unused images using the following command:
sudo docker rmi <image name>

Dockerizing your app with Dockerfile


A Dockerfile is a text config file that is needed to dockerize an application. It will define how your
container is built, and hence should contain all the steps for setting up and configuring the environment
and deploying your application within the container.

We will take an example where we have two files in our application source code folder.

Create a mywebapp folder and create an index.html file within it. Add the contents as shown in the screenshot.
mkdir mywebapp
cd mywebapp
nano index.html

Now create antoher file, a sample text file readme.txt having some lines of text as shown in screenshot:
nano readme.txt

Your webapp project is now ready. Now follow the steps below to dockerize the sample website project,
i.e. create a dockerfile which should have the steps to spin up a docker container having a web server
(apache2) running on Ubuntu OS and which will also deploy the webapp files to the running web server.
1. Create a file named Dockerfile at the root of your application (within mywebapp folder)
sudo nano Dockerfile

2. Update the Dockerfile with the following commands:


FROM ubuntu
RUN apt-get update
RUN apt-get -y install
apache2 ADD . /var/www/html
ENTRYPOINT apachectl -D FOREGROUND

Note:

• FROM < image> : < tag> command is used to specify the base image on which your container will
be built. It must be the first non-comment instruction in the Dockerfile.
• RUN command or RUN [” executable ”,” param1 ”,” param2 ”] is used to specify any shell commands to be
executed

• You can use multiple commands in a single RUN statement using the syntax:
RUN ⟨command1⟩ & & \
⟨command2⟩ & & \
⟨command3⟩

• You can also execute multiple commands within your container using an executable script file:
ADD file.sh /tmp/ file.sh
RUN /tmp/file.sh

• ADD source destination instruction is used to copy files either from the local filesystem or from a remote URL.
Here the ’.’ signifies that we want to copy all the files within the folder into the specified container target
folder ’/var/www/html’

• ENTRYPOINT or CMD instruction is used to specify the command to launch the process that you want to run
within the container on instantiation.

• ENV allows to set the key value pair as shell/environment variables

Building an Image using Dockerfile


Following is the sytax for creating a new image from a Dockerfile:
sudo docker build <directory> -t <image-name>:<tag>

Run the below command to create a custom image for your webapp using the Dockerfile:
sudo docker build . -t mywebapp
Note:
• The directory value should point to the folder where the Dockerfile is added which is usually the
root folder of your application. Use ’.’ if you are running the command in the same directory as your
Dockerfile.
• -t flag stands for tag and you can use it to provide the image name (optionally with a tag) for your
custom image. The default tag is ’latest’.
• Every command or step in the Dockerfile is added as a new layer while building the container. This is
achieved by creating and removing intermediate containers. Each layer is cached separately so that
subsequent builds are much faster.
Once your custom image is ready, you can view it by listing the available docker images. You can also run a
container from the image, and test the website (use command given in section - Running a Container
from an Image)

Sharing Images using an Image Library/Repository


Sign up on DockerHub (https://hub.docker.com/) to start creating your repositories and storing custom images.

Creating a new Repository on DockerHub


Click on ’Create Repository’ button on your DockerHub Repositories tab.

Give a name for your repository. The name should match the name for your custom image.
Connecting to DockerHub through Terminal
Use the below command and your DockerHub credentials to login to DockerHub within your instance terminal.
sudo docker login

Pushing a Custom Image to DockerHub


Use the below command to push an image to your DockerHub repo. Note: The specified image name
should match the full name of your DockerHub repository (username/repo).

sudo docker push <image name>:<tag>

Your DockerHub repository should reflect the uploaded image and tag.

References
1. Deployment and Operations for Software Engineers - by Len Bass and John Klien
2. Docker Docs https://docs.docker.com/ - by Docker Inc.

You might also like