search
HomeBackend DevelopmentPHP TutorialHow to install and use Docker on Linux

How to install and use Docker on Linux

Mar 13, 2018 am 09:14 AM
dockerlinuxuse

This article mainly introduces you to the method of installing and using Docker on Linux. The editor thinks it is quite good. Now I share it with you, hoping to help everyone.

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 easy 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.

Installation

Since Ubuntu Server 16.04 lacks a graphical interface, 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

After completion, use the command to upgrade the system:


sudo apt upgrade

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


sudo reboot

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

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


sudo dnf install docker

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, enter the following command to download and run the required scripts:


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

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:


sudo usermod -a -G docker $USER

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, when you add a user to the docker group, you will find that this group does not exist. So what to do? You need to create this group first. The command is as follows:


sudo groupadd docker && sudo gpasswd -a ${USER} docker && sudo systemctl restart docker
newgrp docker

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

Start, pause and enable Docker

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


sudo systemctl start docker
sudo systemctl enable docker

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


sudo systemctl stop docker
sudo systemctl restart docker

Now you can use Docker to deploy containers.

Pull the image

For Docker, the image is the cornerstone of building containers. You can pull down an image (such as NGINX) and deploy as many containers as you like based on this image. Before using the image, you first need to pull the image into the system. The image is pulled from the registration warehouse. By default, the installed Docker contains a default registration warehouse Docker Hub - this registration warehouse contains a large number of images contributed by others (including official images and those contributed by users themselves). mirror).

Suppose you want to pull an Nginx web server-related image. Before starting to pull, first check what images are already in the system. Enter the docker images command and you will find that no image exists yet (Figure 1).

Figure 1: No mirror yet.

Let’s pull an image. Use the following command to download the Nginx image from Docker Hub:


docker pull nginx

The above command will download the latest (official) Nginx image from Docker Hub. Now run the docker images command and you will see the images listed (Figure 2).

Figure 2: The NGINX image has been pulled down.

Did you notice that what I said here is the "official" Nginx image? There are many unofficial Nginx images on Docker Hub. These unofficial mirrors are created for various purposes. You can use the following command to search all Nginx images in Docker Hub:


docker search nginx

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


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

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


docker pull jwilder/nginx-proxy

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

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

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

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

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

相关推荐:

使用docker基于daocloud自动化部署到自己的主机

如何使用Docker部署PHP开发环境

关于如何使用docker构建php的开发环境

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

Statement
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
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools