Home System Tutorial LINUX Master teaches you how to install and use Docker on Linux

Master teaches you how to install and use Docker on Linux

Jun 02, 2024 pm 10:42 PM
linux linux tutorial Red Hat linux system linux command linux certification red hat linux linux video

Containers allow you to scale delivery capabilities (both internal and external) in an unprecedented way. For example, you can quickly deploy multiple NGINX instances (even for multiple different stages - such as development and production). Unlike virtual machines, containers do not consume too many system resources.

Docker makes it extremely easy to create, deploy, and manage containers. Even better, installing and using Docker is particularly convenient on the Linux platform.

I will show you how convenient it is to install Docker on Linux and get you started with Docker. My demonstration platform is Ubuntu 16.04 Server, but the process is similar on most other Linux platforms.

I assume here that you have started Ubuntu Server 16.04.

Install

Due to the lack of a graphical interface in Ubuntu Server 16.04, I will install and use Docker entirely through the command line. Before you install, you need to update apt and then perform the necessary upgrades. It is important to note that if the system kernel is upgraded, you will need to restart the system. Therefore, it is best to choose a time when the server can be restarted.

Run the following command to update apt:

sudo apt update
Copy after login

After completion, use the command to upgrade the system:

sudo apt upgrade
Copy after login

If the kernel is upgraded, you need to restart the server with the following command:

sudo reboot
Copy after login

If the kernel has not been upgraded, you can install Docker (no restart required). The command to install Docker is:

sudo apt install docker.io
Copy after login

If you are using other Linux distributions and find that there is no docker.io package when you try to install it using the corresponding package manager, you should install the docker package. For example, to install on Fedora you should use the command:

sudo dnf install docker
Copy after login

If you are using CentOS 7, it is best to use the installation script to install docker. First use the command sudo yum check-update to update the system. After the upgrade is complete, enter the following command to download and run the required scripts:

curl -fsSL https://get.docker.com/ | sh
Copy after login

By default, only administrator privileges can run docker commands. For security reasons, you don't want to run Docker as root or use sudo. To solve this problem, you need to add your own user to the docker group. The command is as follows:

sudousermod-a -G docker $USER
Copy after login

After completing the operation, log out of the system and then log in again, and it should be done. However, if your platform is Fedora, you will find that this group does not exist when you add a user to the docker group. So what to do? You need to create this group first. The command is as follows:

sudogroupadd docker &&sudo gpasswd -a ${USER} docker &&sudosystemctl restart docker
newgrp docker
Copy after login

Log out and then log in again. You can start using Docker.

Start, pause and enable Docker
After installing Docker, you can have the Docker daemon automatically start at system startup. Use the following two commands to achieve this:

sudo systemctl start docker
sudo systemctl enable docker
Copy after login

If you need to pause or restart the Docker daemon process, the command is:

sudo systemctl stop docker
sudo systemctl restart docker
Copy after login

Now you can use Docker to deploy containers.

Pull the image

对 Docker 来说,镜像是构建容器的基石。你可以拉下一个镜像(比如 NGINX)然后根据这个镜像部署任意多个容器出来。使用镜像前,你首先需要把镜像拉取到系统中。镜像从注册仓库中拉取,默认情况下安装好的 Docker 包含了一个默认的注册仓库 Docker Hub —— 这个注册仓库包含了大量别人所贡献的镜像(既包括官方的镜像,也包括用户自己贡献的镜像)。

假设你想要拉取一个 Nginx Web 服务器相关的镜像。在开始拉取前,先检查一下系统中已经有了哪些镜像。输入 docker images 命令你会发现现在还没有镜像存在(图 1)。

大神教你在 Linux 上安装和使用 Docker

图 1:还没有镜像。

让我们来拉取一个镜像。使用下面命令可以从 Docker Hub 中下载 Nginx 镜像:

docker pull nginx
Copy after login

上面命令会从 Docker Hub 下载最新的(官方的) Nginx 镜像。现在再运行 docker images 命令就能看到有列出镜像了(图 2)。

大神教你在 Linux 上安装和使用 Docker

图 2:NGINX 镜像已经被拉取下来了。

注意到我这里说的是“官方” Nginx 镜像了吗?在 Docker Hub 上有很多的非官方 Nginx 镜像。这些非官方镜像都是基于各种不同的目的而创建出来的。你可以使用下面过命令来搜索 Docker Hub 中的所有 Nginx 镜像:

docker search nginx
Copy after login

你会发现(图 3 中),有很多基于不同目的所创建的 Nginx 镜像(反向代理、PHP-FPM(LCTT 译注:FastCGI 进程管理器FastCGI Process Manager是一个 PHPFastCGI 管理器,旨在将 FastCGI 进程管理整合进 PHP 包中)功能、LetsEncrypt(LCTT 译注:由 ISRG 提供的免费 SSL 项目),Bitnami,在树莓派上使用的 Nginx 和 Drupal,等等很多很多)。

大神教你在 Linux 上安装和使用 Docker

图 3:Docker Hub 上找到的各种 NGINX 镜像。

假设,你想要下载内建有反向代理功能的 Nginx 镜像,有个非官方的镜像 jwilder/nginx-proxy。因此输入下面命令来拉取这个镜像:

docker pull jwilder/nginx-proxy
Copy after login

再输入 docker images 命令来查看新拉下来的这个镜像(图 4)。

大神教你在 Linux 上安装和使用 Docker

图 4:已经有了两种不同的 NGINX 镜像了。

处于谨慎考虑,我建议只使用官方镜像,毕竟你无法确定非官方镜像是否包含了恶意代码。

有了镜像后就可以用它来部署容器了。下次我们再聊聊如何通过这些 Nginx 镜像来发布容器。

Docker 是一个超级强力的系统可以让你的工作更简单,让你的公司更具有伸缩性,也更灵活。想知道 Docker 还能做什么,运行 man docker 然后阅读它的帮助文档吧。

The above is the detailed content of Master teaches you how to install and use Docker on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

See all articles