Install Docker di Linux

Dari Dokumentasi Robie
Revisi sejak 28 November 2024 08.14 oleh Robie (bicara | kontrib)
Loncat ke navigasi Loncat ke pencarian
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc 

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update


sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

1. Install Portainer on Docker using docker-compose

Portainer is usually deployed as a Docker container using the Docker Engine. In our previous article, we deployed Portainer on Ubuntu using Docker. To recap, you can deploy portainer using a persistent volume using the following Docker commands:

The first command creates a persistent volume where data will be permanently stored.

docker volume create portainer_data

The next command spins a Portainer container using the persistent volume.

docker run -d -p 9000:9000 --name portainer_ce --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data  portainer/portainer-ce:latest

The above command has several flags:

The d flag runs the container in detached mode. This simply means that the container runs in the background.

The -p flag maps port 9000 on the container to port 9000 on the Docker host.

The --name option assigns a custom name to the container, in this case, portainer_ce. The flag makes it possible to assign a human-friendly name compared to the default one assigned by Docker which can be quite complex.

The --restart always option instructs Docker to restart the container always in case of a reboot or when stopped.

The -v /var/run/docker.sock:/var/run/docker.sock command maps the Docker socket to the Portainer container so that Portainer can access and interact with the Docker daemon.

The -v portainer_data:/data maps the data storage on the portainer to the persistent volume called portainer_data residing on the Docker host.

A better approach to deploy and run Portainer is to use Docker Compose. This lets you deploy a container application from a YAML configuration file with just a single command.

To install Docker Compose, run the command:

sudo apt install docker-compose docker-compose-plugin -y

Once Docker Compose is installed, verify its version as follows.

docker-compose version

Once installed, create a Docker compose YAML file.

nano docker-compose.yml

Add the following lines of code that spell out the resources to be deployed.

version: "3"
services:
  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    ports:
      - 9000:9000
    volumes:
      - portainer_data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped
volumes:
  portainer_data:

The YAML file includes all the parameters stipulated in the docker run command. In this case:

The image field is configured to portainer/portainer-ce:latest to use the latest Portainer CE image from Docker Hub. The container_image field is set to portainer which will be the name of the Portainer Docker container.

The ports field is set to map port 9000 on the container to port 9000 on the Docker host. Thus, the Portainer container will be accessed by browsing https://host-ip:9000.

The volumes field configures a data volume that is mounted to the /data directory inside the container. The volume is a persistent storage location that persists the Portainer data even when the container restarts or exists.

The Docker socket, /var/run/docker.sock, is bind mounted straight into the container. This lets Portainer access and manage the Docker installation within which it is running.

With the compose file in place, save the changes and exit the YAML file. To launch Portainer, run the following command:

docker compose up -d

Once the command is executed, Docker will pull the Portainer image and create the Portainer container alongside other resources defined in the Docker compose file.

#2. Access Portainer

With the Portainer container running, access Portainer by launching your browser and heading over to the URL https://host-ip:9000.

You will come across a security warning informing you that the connection is not private. This shows Portainer is using a self-signed SSL certificate to encrypt web traffic. To proceed, click the Advanced link and then click Proceed to [insert server IP address].

Next, you will be required to create an administrator. Provide a password and confirm it. Then click Create user to create the user.

Once the user is created, you will be redirected to a setup wizard. Click the Get Started button to proceed with the local Docker socket mounted into the container.

This ushers you to the dashboard of your local environment as shown. Click anywhere within the highlighted region shown.

This takes you to a dashboard that provides a summary of your environment including the number of stacks, containers, images, volumes, and networks.