LabWeek4 Tutorial DockerBasics
LabWeek4 Tutorial DockerBasics
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.
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.
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
3. Start Docker Desktop using the shortcut created on the desktop. Accept the agreement.
Docker Desktop starting.
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:
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.
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
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.
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
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.
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)
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
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.