قالب وردپرس درنا توس
Home / Tips and Tricks / How to Install Docker and Docker Compose on Linux – CloudSavvy IT

How to Install Docker and Docker Compose on Linux – CloudSavvy IT



Docker gives you everything you need to build and run containers on your Linux system. Containers are comparable to lightweight virtual machines. They let you create portable application images that run with the kernel of your host’s operating system.

You can use Docker on Debian, Fedora, CentOS and Raspbian platforms. Derivative products of those benefits will also work. Docker offers x86 / x64 and ARM builds for most supported platforms. We are using a Debian system with the apt package manager for this guide. Instructions specific to other distributions can be found in the Docker docs.

Docker Compose allows you to build and run stacks of multiple containers. You create a YAML file that configures your application̵

7;s containers. You can then use a single command to start all containers and link them together. Compose allows you to separate the components of your stack into separate container services.

Compose is distributed independently from Docker. Install Docker before adding Compose or Compose will not work.

Install Docker

You can use your operating system’s package manager to install the latest Docker release. You need to add the Docker repository, update your package lists and then install Docker.

Debian / Ubuntu

Start adding dependencies necessary for the installation process:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

Then add the GPG key from Docker’s repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

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

Now you can install Docker:

sudo apt-get install docker-ce docker-ce-cli containerd.io

Fedora

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

Install Docker:

sudo dnf install docker-ce docker-ce-cli containerd.io

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

Install Docker:

sudo yum install docker-ce docker-ce-cli containerd.io

Using Docker without Sudo

The Docker daemon runs as root. Usually you need to prefix Docker commands with sudo. This can get annoying if you use Docker often. Add yourself to it docker group lets you use Docker without it sudo.

sudo usermod -aG docker $USER

Once you are in the group, log out and log in again. You can also do it newgrp docker command to immediately log in to the updated group. You should be able to drop now sudo of Docker commands.

Test your Docker installation

Now that Docker is installed, it’s time to test if it works! You can change the minimum hello-world statue.

docker run hello-world:latest

You should see a few lines of output appear in your terminal. Docker first looks for the hello-world:latest image on your machine. Since it does not exist, the image is then pulled from the Docker Hub repository.

Once the image has been retrieved, it will be created into a new container. The container is configured to broadcast a standard “hello world” message and then shut down. If you see the text, your Docker installation is working!

Manage the Docker service

You can check if Docker is running by inspecting the service with systemctl. There are two components to consider, docker and containerd. docker is the Docker Engine daemon to which the CLI sends commands. containerd is the underlying runtime that actually runs your containers.

sudo systemctl status docker.service
sudo systemctl status containerd.service

You can manage the daemons like any other services on your system. Use systemctl stop if you want to temporarily stop Docker and free up system resources used by your containers:

sudo systemctl stop docker.service

You can restart the service with systemctl start.

Add Docker-Compose

docker-compose is a separate binary file that is best downloaded directly from the project’s GitHub releases. Most popular Linux distributions include Compose in their package managers, but it can be significantly outdated.

Go to the Docker Compose release page and note the latest version number. At the time of writing, it was 1.29.0.

Replace the version you see instead of 1.29.0 in the command below. This will download the correct binary file for your system and put it in /usr/local/bin.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Then make the file executable:

sudo chmod +x /usr/local/bin/docker-compose

You can now use the docker-compose command in your terminal. Try to run docker-compose --version to check.

Create a simple Docker-Compose file

Use docker-compose you must have one docker-compose.yml File. This describes the containers that make up the stack of your application. You can specify port bindings, environment variables, networks and volumes used by your containers.

Containers indicated in the same docker-compose.yml file automatically become part of the same stack. They are linked together in a Docker network so that they can communicate with each other through their docker-compose.yml service names as host names.

Here’s a simple one docker-compose.yml to run a PHP web server and a MySQL database:

version: "3"

services:
  app:
    image: php8.0-apache
    environment:
      - MYSQL_SERVER=mysql
      - MYSQL_USERNAME=root
      - MYSQL_PASSWORD=example
    ports:
      - ${PORT:-80}:80
    restart: unless-stopped
  mysql:
    image: mysql:8.0
    expose:
      - 3306
    environment:
      - MYSQL_ROOT_PASSWORD
    volumes:
      - mysql:/var/lib/mysql
    restart: unless-stopped

volumes:
  mysql:

You can start both services by running docker-compose up -d. You will see both images being pulled from Docker Hub. The containers are then started. You can stop them both docker-compose stop. Use docker-compose down to remove the containers.

The sample YAML file shows some Docker Compose features. Containers are defined under the services node, where each container has its Docker image. You can pass shell environment variables to containers (environments) and setup Docker port binds (ports). Environment variables can be interpolated into strings using the ${VARIABLE:-DEFAULT} syntax. If the variable is not set in your shell, DEFAULT is used as a value instead. You can learn more about Docker Compose in our dedicated guide.

Resume

You can easily install Docker using the package managers of popular Linux distributions. Add the Docker repository and then install docker-ce, docker-ce-cli and containerd.io. These components provide you with everything you need to build and run your containers.

Once you’re up and running, you can get a Dockerfile and use docker build -t my-app:latest . to build it. Then use docker run my-app:latest to start your container.

Docker-Compose is an additional layer of abstraction that makes it easier to run a “stack” of multiple containers. With Compose, you don’t have to manually run multiple commands in series. You can write and use a declarative YAML file docker-compose up to start all your containers at once.


Source link