CUSTOMISED
Expert-led training for your team
Dismiss
Comprehensive Guide: How to Use Docker for Beginners

13 September 2023

Comprehensive Guide: How to Use Docker for Beginners

Docker is a platform that allows developers to easily pack, ship, and run applications within lightweight containers. Containers let you quickly deploy and reliably run applications by providing a standard way to package the code, configurations, and dependencies into a single object.

This article is a support resource for our popular Docker Course 

In this beginner's guide, we will take you through everything you need to start understanding and using Docker.

What Exactly is Docker and Why is it Useful?

Docker is an open platform for developing, shipping, and running applications within containers. Containers allow developers to package up an application with all of the components it needs, such as libraries and other dependencies, and deploy it as one package.

Here are some of the key benefits of using Docker:

  • Portability - You can build locally and deploy containers to any environment like testing, staging or production.
  • Speed - Containers give you faster boot times and deployment cycles compared to virtual machines.
  • Efficiency - Containers share resources and run on the host machine's OS, so you can run more apps on the same hardware.
  • Isolation - Containers isolate environments, prevent conflicts between components and provide reproducible environments.
  • Microservices - Docker is ideal for microservices architecture and distributing apps across many containers.
  • Continuous integration/deployment - Docker enables easier system integration and continuous software delivery.
  • Lightweight - Containers share the host system kernel and do not require an OS per application, utilizing less memory.

So in summary, Docker provides speed, efficiency, portability and resource isolation for easily building and deploying applications as portable containers.

Core Docker Components and Concepts

To use Docker effectively, you need to understand some key components and terminology:

Images

A Docker image is a read-only template that contains a preconfigured environment and application. Images form the basis of containers.

Containers

Containers are runtime instances of Docker images. You can run, start, stop and delete containers from images.

Dockerfile

A Dockerfile contains instructions to assemble an image. It automates building images.

Volumes

Volumes provide persistent data storage for containers.

Networking

Docker provides different network modes to communicate between containers and the host system.

Installing Docker

Docker can be installed on various operating systems. Here are instructions for the major platforms:

Linux

 $ sudo apt update $ sudo apt install docker.io -y 

Windows 10/11

Install Docker Desktop from Docker Hub. Double-click to install.

Mac

Download Docker Desktop for Mac from Docker Hub and double-click the app to install.

Once installed, you can verify the Docker version:

 $ docker --version Docker version 20.10.12, build 20.10.12-0ubuntu1~20.04.2 

You can now run a test Docker image:

 $ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. ... 

Running and Managing Docker Containers

Now that Docker is installed, let's go over how to run and manage containers.

First, pull a base image from Docker Hub:

 $ docker pull ubuntu 

Then run a container from this image:

 $ docker run -it ubuntu bash 

This will download the ubuntu image (if not already cached) and start a new container in interactive mode with a bash shell.

To view running containers:

 $ docker ps 

To stop a container:

 $ docker stop <container_id> 

Other useful commands include start, restart, attach, exec, logs and rm.

Containers can be robustly configured using ports, volumes, environment variables and more. Overall, Docker provides a standard way to deploy and run applications in an isolated, reproducible environment.

Persisting Data with Volumes

One of the downsides of containers is that data doesn't persist when the container is removed. Docker volumes solve this problem.

Volumes provide the ability to connect persistent storage to Docker containers. Each volume is a directory that bypasses the storage driver and mounts directly into the container.

To create a volume:

 $ docker volume create my-volume 

This can then be mounted into containers using the -v flag:

 $ docker run -v my-volume:/data ubuntu 

This mounts the volume my-volume into the /data directory in the container.

Volumes are useful for persisting data, sharing data between containers and accessing host files from within containers.

Networking with Docker Containers

By default, Docker provides three networks for you to use:

  • Bridge - Default network where containers can communicate via IP addresses.
  • Host - Removes network isolation between container and host.
  • None - Removes all networking.

When creating containers on the default bridge network, you can communicate between containers using links. Links create a secure tunnel between containers that doesn't expose ports externally.

To link two containers:

 $ docker run -d --name db redis $ docker run -it --link db:db ubuntu 

Now the ubuntu container can access the db container using db as the hostname.

You can also create custom networks for more flexibility:

 $ docker network create my-network $ docker run -d --net my-network nginx 

This adds advanced networking capabilities to containers.

Multi-Container Apps with Docker Compose

Docker Compose allows you to run multi-container apps with one simple configuration file.

The docker-compose.yml file defines the services, networks and volumes required to run the containers.

Here is a simple example:

 version: "3.9" services: web: image: nginx ports: - "80:80" db: image: postgres 

This configures a web and db service. To start it:

 $ docker-compose up 

Compose can configure networking between containers out of the box. It also provides useful commands like logs, ps, stop, restart and rm to manage the app lifecycle.

Overall, Docker Compose makes orchestrating multi-container apps simple and automated.

CI/CD Pipelines with Docker

Docker is commonly used in CI/CD pipelines to provide speed, reliability and portability.

Some examples and benefits:

  • Build Environments - Spin up Docker build agents to create reproducible builds.
  • Testing - Deploy apps and run integration tests inside containers.
  • Packaging - Build Docker images within the pipeline.
  • Deployment - Deploy Docker images to different environments.
  • Rollbacks - Quickly roll back to previous passing versions.

Docker enables reliable and repeatable CI/CD workflows. Images can be pushed to registries and then pulled on deployment. This facilitates continuous delivery and zero downtime deployments.

Docker Security Best Practices

While containers provide isolation and security compared to VMs, here are some best practices:

  • Use the latest OS patches and Docker version
  • Scan images for vulnerabilities regularly
  • Avoid privileged containers whenever possible
  • Limit container resources and capabilities
  • Configure firewalls and app-level security
  • Secure the Docker daemon socket
  • Enable Docker Security Scanning

Following security best practices ensures containers are securely deployed and managed.

Troubleshooting Common Docker Issues

Here are some common Docker issues and how to troubleshoot them:

Container exiting immediately - Use docker logs to check why it exited.

Permission denied errors - Add your user to the docker group or adjust permissions appropriately.

Networking issues - Verify the correct network driver is being used.

Cannot connect to Docker daemon - Check if the Docker socket file exists and has correct permissions.

Image pull errors - May be a network issue, try manually downloading the image as a workaround.

Performance issues - Enable caching, increase resources and limits, host optimization.

Unauthorized access - Properly configure Docker Trust and registry authentication.

Pay attention to error messages and logs. Many issues can be resolved through proper configurations.

Next Steps with Docker

This covers the key concepts for understanding how to use Docker for development!

Here are some suggestions for leveling up your Docker skills:

  • Try advanced networking like overlays and service discovery
  • Optimize build speed through multi-stage builds and layer caching
  • Deploy production-grade Swarm clusters with TLS, secrets and security
  • Build images within CI/CD pipelines and enable continuous delivery
  • Take one of JBI Trainings Docker Training Courses

Docker provides a huge advantage for building, distributing and running modern apps. Continue learning more to use Docker like a pro!

Frequently Asked Questions

Here are some common questions about Docker:

How does Docker compare to virtual machines?

Docker containers share the host OS kernel and do not require the overhead of a hypervisor or OS per application. This makes containers more lightweight, portable and efficient.

What are the disadvantages of Docker?

Added complexity, integration issues with legacy systems, networking challenges, persistent storage and security considerations.

What is a Docker registry?

A registry is a repository for Docker images. Docker Hub is the default public registry. Organizations often run private registries.

How do I build a Docker image?

Docker images are built from a Dockerfile - a text file containing instructions for assembling the image. You can build images using docker build.

What is the difference between Docker Compose and Docker Swarm?

Docker Compose is used for running multi-container apps locally in development. Docker Swarm provides native clustering to run containers across multiple hosts in production.

Conclusion

This guide has taken you through the fundamentals of Docker - how it works, core components, installing it, running containers, networking, volumes, Compose, Swarm integration, security, troubleshooting and more. You should now have a solid understanding of how to leverage Docker for your applications.

For more comprehensive Docker training and tutorials, check out the resources on the official JBI website and documentation. Docker provides powerful benefits for running modern apps - with this foundation you can continue building your Docker skills. Or check out our next article Setting up a Docker Development Environment on (Linux / Windows / Mac)

Here are some recommended Docker related courses from JBI Training:

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course