Containers are executable components that combine the source code and OS libraries and dependencies so the code can run in any environment. Docker allows you to manage these containers.
Containers are like lightweight virtual machines that allow you to create portable application images which run on your host’s operating system kernel.
Docker compose is used to run multiple containers as a single service. Both these containers run individually but they can interact with each other when required. YAML language is used to write Docker compose files which are also called Yet Another Markup Language.
Let's say if you have an application that requires a server and a database then docker-compose can be used to run both of these containers as a single service.
How to install Docker in Linux
We are going to install Docker in Linux so you can use Linus's package manager to install the latest version of Docker. You will learn to add Docker's repository, update the package lists and then we will install Docker.
First we need to add the dependencies for the installation -
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
Then we will add Docker’s repository GPG key by the following command -
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Enter the following command to add the repository to your sources and update your package lists -
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Alright, we are ready to install Docker -
sudo apt-get install docker-ce docker-ce-cli containerd.io
2). For Fedora
First of all, add Docker’s package repository -
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io
3). For CentOS
Add Docker’s package repository:
sudo yum -y install yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
then Install Docker:
sudo yum install docker-ce docker-ce-cli containerd.io
Test if the docker is installed
Alright, we have successfully installed Docker in our Linux Operating System, Now we will check if it is properly installed or not. You can use the
docker run hello-world:latest
The Docker will first look for the hello-world:latest image on your machine, if it doesn't exist on your machine then docker will get it from the Docker Hub repository.
Now, Let's add Docker Compose
Go to the Docker Compose's releases page and check out the latest version or the version you want to install on your machine.
Docker compose is a separate binary so it can be directly downloaded from the project's GitHub releases. At the time of writing, the latest version is 2.7.0.
sudo curl -L "https://github.com/docker/compose/releases/download/2.7.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Replace the 2.7.0 with the version you want to install. This command will download the binary in /use/local/bin.
Now, you have to make the executable file -
sudo chmod +x /usr/local/bin/docker-compose
You can use the docker-compose --version command in your terminal to check if the docker-compose is installed or not.
Create a Docker-Compose File
To use docker-compose, you'll need to have a docker-compose.yml file. You can specify many things that are to be used by the docker containers like port bindings, environment variables, etc. This docker-compose.yml file describes the containers.
Containers that are declared in the same docker-compose.yml file are linked to a Docker network so the containers can communicate with each other using the service names of docker-compose.yml.
You can install Docker and Docker Compose using the above commands. You can also install Docker easily with the package managers of Linux Distributions.