Docker for Beginners: Containerizing Your Applications
Docker
A service for managing containers is called Docker. Docker is an open platform for developing, shipping, and running applications. The entire purpose of Docker is to make it simple for developers to create apps, load them into containers, and then use them wherever.
Container
With Docker, you can bundle and execute an application in a loosely isolated container. Containers are lightweight and contain everything needed to run the application.
Develop your application and its supporting components using containers.
The container becomes the unit for distributing and testing your application.
When you're ready, deploy your application into your production environment, as a container or an orchestrated service.
This works the same whether your production environment is a local data center, a cloud provider, or a hybrid of the two.
Containers have a smaller footprint than virtual machines. This is because containers share the host operating system's kernel, while virtual machines each have their own kernel. As a result, containers can start and stop faster, and they use fewer resources.
Containers are more lightweight than VMs, as their images are measured in megabytes rather than gigabytes.
Getting Started with Docker
Docker Desktop
Docker Desktop is an easy-to-install application for your Mac, Windows, or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper.
Docker Images - Template to create a docker container.
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image that is based on the Ubuntu image but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
You might create your own images or you might only use those created by others and published in a registry. To build your image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers that have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.
Docker Containers - Running instance of docker image
A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container's network, storage, or other underlying subsystems are from other containers or the host machine.
A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that aren't stored in persistent storage disappear.
Docker Registries
A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker looks for images on Docker Hub by default. You can even run your private registry.
When you use the docker pull or docker run commands, Docker pulls the required images from your configured registry. When you use the docker push command, Docker pushes your image to your configured registry.
Docker objects
When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.
DockerFile
Text documents contain all the commands that a user can call on the command line to assemble an image.
Instructions for pulling an image from Docker Hub and running your first container.
Step-1: Verify the Docker version and also log in to Docker Hub
docker version
docker login
Step 2: Pull the Image from the Docker Hub
docker pull stacksimplify/dockerintro-springboot-helloworld-rest-api:1.0.0-RELEASE
Step-3: Run the downloaded Docker Image & Access the Application
docker run --name app1 -p 80:8080 -d stacksimplify/dockerintro-springboot-helloworld-rest-api:1.0.0-RELEASE
Step-4: List Running Containers
docker ps
docker ps -a
docker ps -a -q
Step-5: Connect to Container Terminal
docker exec -it <container-name> /bin/sh
Step-6: Container Stop, Start
docker stop <container-name>
docker start <container-name>
Step-7: Remove Container
docker stop <container-name>
docker rm <container-name>
Step-8: Remove Image
docker images
docker rmi <image-id>
Building Docker Images
To build a Docker image from Dockerfile, let's see the docker build command in action. In the below steps, we will build a simple Docker image, a web server to serve out a web page through installation. You're going to be building, running, and testing it on your local computer.
Step 1 - Create a working directory
Create a directory or folder to use for this demonstration ("docker-demo" is used here) and navigate to that directory by running the following commands in your terminal:
mkdir docker-demo
cd docker-demo
Create a file called "index.html" in the directory and add the following content to it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple App</title>
</head>
<body>
<h1>Hello World</h1>
<p>This is running in a docker container</p>
</body>
</html>
This will serve as the web page to be served up by the server.
Step 2 - Select a base image
Next, select a suitable base image from Docker Hub or a local repository. The base image forms the foundation of your custom image and contains the operating system and essential dependencies. Almost every single image for Docker is based on another image. For this demonstration, you'd be using nginx:stable-alpine3.17-slim as the base image.
Step 3 - Create a Dockerfile
Now create a file named "Dockerfile". This file will define the build instructions for your image. By default, when you run the docker build command, docker searches for the Dockerfile.
Step 4 - Add build instructions to the Dockerfile
Open the Dockerfile in a text editor and add the following lines:
FROM nginx:stable-alpine3.17-slim
COPY index.html /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
The instruction above will create a Docker image that serves the provided "index.html" file through the Nginx web server when a container is launched based on that image.
The FROM instruction initializes a new build stage and sets the base image for subsequent instructions. The COPY instruction copies files or directories (usually the source code) from the source to the specified location inside the image. It copies the file to the "/usr/share/nginx/html" directory, which is the default location for serving static files in the Nginx web server. The main purpose of the CMD instruction is to provide defaults for executing containers. The instructions defined in Dockerfiles differ based on the kind of image you're trying to build.
Step 5 - Build the image using the Docker build command
Before building the Docker image, confirm that you have Docker installed by running the docker --version command.
To build your container, make sure you're in the folder where you have your Dockerfile and run the following command in the terminal:
docker build -t sampleapp:v1 .
This command initiates the Docker build process to create a Docker image based on the instructions specified in the Dockerfile located in the current directory (.)
The -t flag specifies a tag for the image, allowing you to assign a name and version to it. In this case, the image will be tagged as "sampleapp" with the version "v1" providing a descriptive identifier for the image, making it easier to reference and manage.
You should see the build process start and an output indicating that it has finished when it is done.
Step 6 - Verify the built Docker image
After a successful build, verify the image by running the docker images command to list all the available images on your Docker host. You should see your newly created image listed with its assigned tag and other relevant details, ready to be used for running containers or pushing to a container registry for distribution.
Step 7 - Run the Docker image
Next, run the Docker image as a container using:
docker run -p 8080:80 sampleapp:v1
This command tells Docker to run the sampleapp container. The -p flag specifies the port mapping, which maps a port from the host machine to a port inside the container. Here, you are mapping port 8080 of the host machine to port 80 of the container. You can modify the host port as per your preference. Ensure you specify the image name and version you used when building the image.
Step 8 - Access the application
With the container running, you can go ahead to access the application. Open a web browser and navigate to localhost:8080 and you should see the sample web page displayed on your web browser.