This Docker Tutorial is suitable for beginners to advanced levels. In this course, we are going to start from the basics of Docker fundamentals and gradually increase the level to Intermediate and advanced levels. You can also avail a Free Docker Lab to Practice Docker commands and concepts.
Table of Contents
Docker Introduction: Docker Tutorial
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first released in 2013 and was developed by Docker, Inc.
Enrolling in Docker Lab
- You can add Docker Lab to the cart from here https://www.itpanther.com/product/docker-virtual-lab/
- Go to Cart and Checkout https://www.itpanther.com/cart/
- Apply the coupon code to get 100% Discount LearnDockerNT3BRE7P
- Wait for an email which contains Docker Server Details and a URL to connect to the Docker Server
Installing and Running Docker Services
Installing Docker on Linux
Let’s connect to the server using the URL that you received in the email. After a successful connection to the server, we can use below command to install Docker.
sudo yum install -y docker
Starting Docker Services
Now we have successfully installed Docker. The next step is to start Docker services. We can do that using the following command.
sudo systemctl start docker
Checking Docker Service Status
Let’s check the docker daemon status by running the following command:
sudo systemctl status docker
Adding a user in the Docker Group
As you can see, to run docker services, we are prefixing sudo. Let’s add our current user to the Docker Group, so we don’t need to prefix sudo each time we use using Docker daemon client.
sudo usermod -aG docker $USER
Running Docker Containers
Pulling a Docker Image
We can pull the image using the docker pull command. Let’s pull nginx image using the following command:
docker pull nginx
List all the Docker Images
We can list all the docker images using docker images command.
Running Nginx Docker Container
docker run -d -p 80:80 --name my-web-server nginx
Checking Running Docker Containers
Stopping a Docker Container
docker stop <container_name or container_id>
Starting a Stopped Docker Container
Removing Docker Containers
Running Website on Nginx Docker Container
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h1>Welcome to My Website</h1>
<p>This is a sample HTML website. You can replace this text with your own content.</p>
Attach Local Storage to Docker Volume
docker run -d -p 80:80 -v /path/to/your/local/directory:/usr/share/nginx/html --name my-web-server nginx
Installing Redis Database on a Docker Container
docker pull redis
docker run -d --name my-redis-db -p 6379:6379 redis
Login to Docker Container
docker exec -it my-redis-db /bin/bash
Inserting Data to Redis
set course_name docker-demo
set objective mastering-docker-containers
set deadline mastering-in-3-days
Reading data from Redis
Restarting a Docker Container
docker container restart <container_name or container_id>
Removing a Docker Container
docker rm my-redis-db
Mount Volume to Redis Docker Container
docker run -d --name my-redis-db -p 6379:6379 -v $(pwd)/redis_data:/data redis
Understanding Docker Storage
Docker storage is basically two types of files:
- Container data: Data created and consumed by containers during execution time, such as documents, profiles, and databases.
- Image files: Files stored in the Docker image, including system files and metadata required to run the container.
Docker storage mode
Container file system: Each container has its own independent file system, allowing it to store and transfer data independently of other containers.
Container Binding : A binding is a book or document that is placed inside the container, outside the container. They provide consistent storage and can be shared across multiple containers.
Image Layers: Docker images consist of multiple text-only layers stacked on top of each other. Each layer represents the transformation of data.
Image Registry: Images can be stored locally or in a remote repository (e.g. Docker Hub) called an image registry. The directory supports sharing and distribution of images across different media.
Key points in Docker storage
Docker uses the concept of copy-on-write to optimize storage. When a container updates a file, Docker creates a new layer that only changes, leaving the old file unchanged. This minimizes storage overhead and speeds up container creation.
Docker volumes provide a way to save files created by containers. They can manage independently of the box, allowing data to be saved even if the box is removed. Containers can be placed in one or more containers to facilitate information sharing and collaboration.
Binding allows mapping a host or archive to a container file. Unlike the disk volume, the binding mount does not create new files but mounts the user manual directly to the box. A change in one direction will affect the other.
The Docker storage driver is responsible for managing images and data containers on the host. Different drivers offer different levels of performance, stability, and compatibility. Drivers include overlay2, aufs, overlay and devicesmapper.
- Use disk volumes to store persistent data: Emphasize the use of disk volumes to store persistent data to ensure data integrity and portability.
- Improve image quality: Reduce image size and complexity to improve performance and reduce overhead.
- Backup and recovery: Regularly back up important files stored on Docker disk to prevent data loss and ensure disaster recovery.
- Choose drives carefully: Choose a storage drive based on your specific needs for performance, security, and compatibility.