13 September 2023
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.
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:
So in summary, Docker provides speed, efficiency, portability and resource isolation for easily building and deploying applications as portable containers.
To use Docker effectively, you need to understand some key components and terminology:
A Docker image is a read-only template that contains a preconfigured environment and application. Images form the basis of containers.
Containers are runtime instances of Docker images. You can run, start, stop and delete containers from images.
A Dockerfile contains instructions to assemble an image. It automates building images.
Volumes provide persistent data storage for containers.
Docker provides different network modes to communicate between containers and the host system.
Docker can be installed on various operating systems. Here are instructions for the major platforms:
$ sudo apt update $ sudo apt install docker.io -y
Install Docker Desktop from Docker Hub. Double-click to install.
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. ...
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
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.
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
$ 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.
By default, Docker provides three networks for you to use:
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.
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
rm to manage the app lifecycle.
Overall, Docker Compose makes orchestrating multi-container apps simple and automated.
Docker is commonly used in CI/CD pipelines to provide speed, reliability and portability.
Some examples and benefits:
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.
While containers provide isolation and security compared to VMs, here are some best practices:
Following security best practices ensures containers are securely deployed and managed.
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.
This covers the key concepts for understanding how to use Docker for development!
Here are some suggestions for leveling up your Docker skills:
Docker provides a huge advantage for building, distributing and running modern apps. Continue learning more to use Docker like a pro!
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
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.
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: