Docker and Docker Compose are powerful tools that allow you to run and manage applications in containers, making it easy to set up and run applications on any machine, whether it be a laptop, desktop or server.
Here’s a quick overview from Fireship.io.
Overview
Docker is a platform that enables developers to create, deploy, and run applications in containers.
Containers are lightweight, portable, and self-contained units of software that can run virtually anywhere.
With Docker, developers can package an application and its dependencies into a single container that can run on any system that has Docker installed.
The isolation and security allows you to run many containers simultaneously on a given host.
Components
Dockerfile
A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image to use, the commands to run to set up the environment, and any additional configuration or customization needed.
Once the Dockerfile is created, it can be used to build a Docker image that can then be used to run container instances.
Here’s an example of a simple Dockerfile that sets up a Python environment with Flask:
|
|
Another example you can take a look at is Pi-hole’s Dockerfile
Images
A Docker image is a snapshot of an application and its dependencies, which can be used to run one or more containers.
Images are created from Dockerfiles and can be stored in a registry.
Images are lightweight, portable, and provide a consistent environment for running applications.
Docker Compose
Docker Compose is a tool that enables you to define and run multi-container Docker applications.
With Docker Compose, you can define your application’s services, networks, and volumes in a single YAML
file, making it easy to manage your application’s components.
Here’s a general outline of a docker-compose.yml
.
|
|
Getting Started
Docker Desktop
Docker provides Docker Desktop for Windows, macOS, and Linux. Which provides a GUI environment to manage your containers without relying entirely on the CLI.
Docker Engine
If you wish to utilize just the Docker Engine and CLI, download the engine here for your given platform.
Manage Docker as a non-root user
The Docker daemon binds to a Unix socket, not a TCP port. By default it’s the root user that owns the Unix socket, and other users can only access it using sudo
. The Docker daemon always runs as the root user.
If you don’t want to preface the docker command with sudo
, create a Unix group called docker
and add users to it.
When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.
On some Linux distributions, the system automatically creates this group when installing Docker Engine using a package manager. In that case, there is no need for you to manually create the group.
To create the docker group and add your user:
Create the docker group.
|
|
Add your user to the docker group.
|
|
Log out and log back in so that your group membership is re-evaluated.
Registry (Docker Hub)
To run a container, you first need to find an image for the container you want to run.
Docker Hub is a popular repository of Docker images, you can search Docker Hub for an image that meets your needs.
Docker Run
Once you have found an image, you can use the docker run
command to start a container from that image.
For example, to run an instance of the nginx
web server, you can run the following command:
|
|
This command starts a container from the nginx
image, maps port 80 on the host to port 80 in the container, and runs the container in the background (-d)
.
You can access the container from your browser at localhost
. You’ll then be greeted with the default “Welcome to nginx!” message.
Docker Compose
Docker Compose] is typically installed as part of the Docker Desktop installation on Windows and macOS. On Linux, you can install Docker Compose using your distribution’s package manager or by downloading directly from the Docker website.
Once you have Docker Compose installed, you can create a docker-compose.yml
file to define your application’s services, networks, and volumes. Here is an example docker-compose.yml
file that defines a simple web application with a web server and a database:
|
|
This docker-compose.yml
file defines two services: web
and db
.
The web
service runs an instance of the nginx web server, and maps port 80 on the host to port 80 in the container.
The db
service runs an instance of the MySQL database server and sets the root password to password
.
To start the application, you can run the following command:
|
|
This command starts the application in the background (-d) using the configuration in the docker-compose.yml
file.
Versioning
Docker Compose can also be used with version control systems like Git to help manage, deploy, and revert changes of multi-container Docker applications.
Conclusion
Docker and Docker Compose are powerful tools that enable you to easily run and manage applications in containers.
With Docker, you can package your application and its dependencies into a single container that can run on any system.
With Docker Compose, you can define and run multi-container applications using a single configuration file.