Featured image of post Docker Setup

Docker Setup

Install and setup Docker

Install Docker

Official Docker install page

We will use the shell script from Docker

Linux

1
2
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Mac

1
brew install docker

💡 If you don’t have Homebrew installed, you can install it by running the following command in your macOS terminal:

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Windows

For Windows, Docker provides a desktop version that simplifies the installation process.

  1. Go to the Docker Desktop download page.
  2. Download the Docker Desktop installer for Windows.
  3. Run the installer and follow the on-screen instructions.

Once installed, Docker Desktop should automatically start, and you can verify the installation by opening a terminal and running:

1
docker --version

Fix Docker Permission Denied

If you encounter a “Permission Denied” error when running Docker commands, it may be due to Docker requiring root privileges. To fix this and allow Docker to run as a non-root user, follow these steps:

References: StackOverflow Docker Documentation

  1. Add your user to the docker group:
1
2
sudo groupadd docker
sudo usermod -aG docker $USER
  1. Apply the new group membership:
1
newgrp docker
  1. Restart Docker to apply the changes:
1
sudo systemctl restart docker

This will allow you to run Docker commands without using sudo.

Root

If you need to run Docker as the root user, you can do so by prefixing Docker commands with sudo. However, this method is not recommended for security reasons.

Rootless

If you prefer to use Docker without requiring root privileges, you can follow the official guide to set up Docker in rootless mode. This method allows users to run Docker without needing sudo at all, offering a more secure environment. To learn more, check out the Rootless Docker documentation.

Docker Usage Example

Once Docker is installed and running, you can start using it to create and manage containers. Here’s an example of how to run a simple Docker container:

  1. Pull an image from Docker Hub:
1
docker pull hello-world
  1. Run the container:
1
docker run hello-world

This will download the hello-world image from Docker Hub (if not already present) and run it inside a container. The output will display a welcome message confirming that Docker is working.

  1. List running containers:
1
docker ps
  1. List all containers (including stopped ones):
1
docker ps -a
  1. Stop a running container:
1
docker stop <container_id>

Replace <container_id> with the actual ID of the container you want to stop, which you can find by running docker ps.

Docker Mirror for Users in China

If you’re located in China, you might experience difficulties pulling Docker images due to the country’s ban on Docker Hub. To resolve this, you can configure Docker to use a mirror source.

Tencent Docker Mirror (Tencent VPS only)

Tencent offers a reliable Docker mirror for users in China. To use it, follow these steps:

  1. Edit or create the Docker daemon configuration file at /etc/docker/daemon.json:
1
sudo nano /etc/docker/daemon.json
  1. Add the following configuration to use Tencent’s mirror:
1
2
3
{
  "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
  1. Save the file and restart Docker:
1
sudo systemctl restart docker

Now, Docker will use the Tencent mirror when pulling images, improving speed and reliability for users in China.

Additional Mirrors

If you’d like more options, here are some other popular Docker mirrors:

  • Aliyun (Alibaba Cloud) Docker Mirror:
1
2
3
{
  "registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]
}

The config file is stored in the following path

~/.docker/daemon.json

You can add other sources

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
  "registry-mirrors": [
    "https://dockerpull.org",
    "https://docker.1panel.dev",
    "https://docker.foreverlink.love",
    "https://docker.fxxk.dedyn.io",
    "https://docker.xn--6oq72ry9d5zx.cn",
    "https://docker.zhai.cm",
    "https://docker.5z5f.com",
    "https://a.ussh.net",
    "https://docker.cloudlayer.icu",
    "https://hub.littlediary.cn",
    "https://hub.crdz.gq",
    "https://docker.unsee.tech",
    "https://docker.kejilion.pro",
    "https://registry.dockermirror.com",
    "https://hub.rat.dev",
    "https://dhub.kubesre.xyz",
    "https://docker.nastool.de",
    "https://docker.udayun.com",
    "https://docker.rainbond.cc",
    "https://hub.geekery.cn",
    "https://docker.1panelproxy.com",
    "https://atomhub.openatom.cn",
    "https://docker.m.daocloud.io",
    "https://docker.1ms.run",
    "https://docker.linkedbus.com",
    "https://dytt.online",
    "https://func.ink",
    "https://lispy.org",
    "https://docker.xiaogenban1993.com"
  ]
}
Licensed under CC BY-NC-SA 4.0
~~~Thank you for visiting~~~

Theme Stack designed by Jimmy