Docker安装与入门实战(CentOS7)

一、Docker安装

1. 卸载旧版本(可忽略)

(若之前安装过Docker)清理旧的Docker,避免冲突。

执行命令:

sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 若服务器上曾经安装过docker,执行命令;若没有安装过,也无所谓

2. 安装依赖工具

# 执行命令,yum-utils是一个工具包,能够帮助我们配置软件源
sudo yum install -y yum-utils

3. 添加Docker官方仓库

# (在国内它是没办法去访问的,使用阿里云的docker官方仓库)
sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 安装Docker

使用命令安装Docker:

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

5. 启动Docker并设置开机自启

# 启动docker
systemctl start docker

# 设置开机自启
systemctl enable docker

6. 验证Docker是否安装成功

查看docker的版本和查看docker的状态:

# 查看版本
docker version

# 查看docker的状态说明
docker info

若这两个命令都能正常输出,证明docker已经安装成功了。


二、配置镜像加速

1. 拉取镜像遇到的问题

docker部署好后,通过什么命令来拉取我们想要的一个包呢?

# 例如拉取nginx,指定拉取1.24版本
docker pull nginx:1.24

这时候并不能拉取成功,会提示报错:

Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# docker pull nginx:1.24
Error response from daemon: Get "https://registry-1.docker.io/v2/": dial tcp 66.220.149.32:443: i/o timeout
# 这里显示拉取超时timeout

这是因为docker官方仓库在国内是无法访问的,我们只能通过国内的镜像加速。


2. 配置国内镜像加速地址

# 首先创建一个文件,在其中添加国内的镜像加速地址
vim /etc/docker/daemon.json

文件内容(2026年3月 当前比较推荐的可用加速地址):

{
  "registry-mirrors": [
    "https://docker.xuanyuan.me",
    "https://docker.1panel.live",
    "https://mirror.ccs.tencentyun.com"
  ]
}

或直接使用以下命令(建议直接用该命令):

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.xuanyuan.me",
    "https://docker.1panel.live",
    "https://mirror.ccs.tencentyun.com"
  ]
}
EOF

3. 重启Docker服务

镜像地址添加完成后,需要重载内容,并重启docker:

systemctl daemon-reload
systemctl restart docker

4. 重新拉取镜像

重启好后,重新进行拉取:

docker pull nginx:1.24

三、运行第一个容器

1. 运行hello-world容器

运行学习中的第一个容器:

docker run hello-world

输出内容:

[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Retrying in 1 second 
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete 
Digest: sha256:85404b3c53951c3ff5d40de0972b1bb21fafa2e8daa235355baf44f33db9dbdd
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2. 查看镜像

docker images

输出:

[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    1b44b5a3e06a   7 months ago   10.1kB
nginx         1.24      6c0218f16876   2 years ago    142MB

3. 查看运行过的容器

docker ps -a

输出:

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED        STATUS                    PORTS     NAMES
936ae76a1ecc   hello-world   "/hello"   21 hours ago   Exited (0) 21 hours ago             funny_roentgen

四、运行Nginx服务

1. 后台运行nginx容器

# 命令讲解:
# docker run:把一个容器运行起来(这里表示运行的1.24版本的nginx)
# -d参数:表示后台运行
# -p参数:表示将本机的8080端口映射到容器的80端口(即访问服务器的8080端口的时候就是访问的容器80端口)
docker run -d -p 8080:80 nginx:1.24

2. 查看运行中的容器

docker ps

输出:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                   NAMES
beb978021b80   nginx:1.24   "/docker-entrypoint.…"   14 seconds ago   Up 12 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   optimistic_villani

3. 使用curl命令验证

通过curl命令,查看能够访问页面。

curl 127.0.0.1:8080

输出:

[root@localhost ~]# curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

可以看到访问成功了。


4. 通过浏览器访问

# 查看服务器地址
ip a s

查询出当前服务器的ip地址后,直接在浏览器中访问该ip地址,可以看到页面,证明部署成功。

更多推荐