1、基本概念

镜像(Image)

容器的静态模板,包含了应用程序运行所需的所有依赖和文件。

容器(Container)

镜象是一个运行实例,具有自己的文件系统、进程、网络等,且时动态的。容器在镜像启动,并在运行时保持可变。

2、常用命令

命令功能实例
docker run启动一个新的容器并运行命令docker run -d ubuntu
docker ps列出当前正在运行的容器docker ps
docker ps -a列出所有容器docker ps -a

3、容器操作

获取镜像

如果本地没有所需要的镜像,可以使用 "docker pull" 命令从Docker Hub上下载

[root@lavm-5wutyiasaf ~]# docker pull ubuntu:22.04
22.04: Pulling from library/ubuntu
89dc6ea4eae2: Pull complete 
Digest: sha256:01a3ee0b5e413cefaaffc6abe68c9c37879ae3cced56a8e088b1649e5b269eee
Status: Downloaded newer image for ubuntu:22.04
docker.io/library/ubuntu:22.04
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED        SIZE
ubuntu          22.04     b103ac8bf22e   12 days ago    77.9MB

启动容器

使用ubuntu启动一个交互式容器

[root@lavm-5wutyiasaf ~]# docker run -it ubuntu:22.04 /bin/bash
root@3f6a11edb0e6:/# 
root@3f6a11edb0e6:/# 

参数说明

  • -i: 交互式操作
  • -t:终端
  • /bin/bash:容器启动后执行的命令

后台运行

使用-d参数让容器在后台运行

[root@lavm-5wutyiasaf ~]# docker run -d --name test-ubuntu ubuntu:22.04 /bin/bash
fcbc5cf91f3b33f2c3a05ee55d051227ed9f230834e216f9896aa7dbb57347bf

进入容器

有两种方式可以进入容器

使用 docker attach 进入容器(不推荐)

[root@lavm-5wutyiasaf ~]# docker attach 3f6a11edb0e6
root@3f6a11edb0e6:/# 
root@3f6a11edb0e6:/#

注意:使用attach命令退出容器时,容器会停止运行。


使用 docker exec 进入容器(推荐)

[root@lavm-5wutyiasaf ~]# docker exec -it  b76ed162fe12 /bin/bash
root@b76ed162fe12:/# 
root@b76ed162fe12:/# 

注意:使用exec命令推出容器时,容器不会停止运行,所以推荐此方法。

导入和导出容器

# 导出容器
[root@lavm-5wutyiasaf ~]# docker export 129077e4abb4 > ubuntu.22.04.tar
[root@lavm-5wutyiasaf ~]# ls
anaconda-ks.cfg  install_panel.sh  original-ks.cfg  ubuntu.22.04.tar

# 导入容器
[root@lavm-5wutyiasaf ~]# docker import ubuntu.22.04.tar test/ubuntu:v1
sha256:906851c5bf30575863fe4f249da96512eb71a3975c03922fba3baa44530ab884
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
test/ubuntu     v1        906851c5bf30   5 seconds ago   77.9MB

# 或者也可以这么导入
[root@lavm-5wutyiasaf ~]# cat ubuntu.22.04.tar | docker import - test/ubuntu:v2
sha256:3e987db64805f8dc339715d3ab86fbb5292b20a6d86d54bf36aab37110581035
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED              SIZE
test/ubuntu     v2        3e987db64805   7 seconds ago        77.9MB
test/ubuntu     v1        906851c5bf30   About a minute ago   77.9MB
ubuntu          22.04     b103ac8bf22e   12 days ago          77.9MB

运行web应用

以下示例展示如何运行一个Web应用

# 拉取镜像
[root@lavm-5wutyiasaf ~]# docker pull nginx:latest
latest: Pulling from library/nginx
dad67da3f26b: Pull complete 
3b00567da964: Pull complete 
56b81cfa547d: Pull complete 
1bc5dc8b475d: Pull complete 
979e6233a40a: Pull complete 
d2a7ba8dbfee: Pull complete 
32e44235e1d5: Pull complete 
Digest: sha256:6784fb0834aa7dbbe12e3d7471e69c290df3e6ba810dc38b34ae33d3c1c05f7d
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

# 运行容器
[root@lavm-5wutyiasaf ~]# docker run -d -p 8080:8080 nginx:latest
6f8cf788e20cb223c286fd73bb794c590a112ffcbf485e6320ba8159b00ac0dc
[root@lavm-5wutyiasaf ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                                               NAMES
6f8cf788e20c   nginx:latest    "/docker-entrypoint.…"   9 seconds ago   Up 7 seconds   80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   laughing_rubin

参数说明

  • -P:随机映射端口。
  • -p 8080:8080:指定端口映射。将容器的8080端口映射到主机的8080端口。

验证是否可以正常访问

[root@lavm-5wutyiasaf ~]# curl http://localhost:8080
curl: (56) Recv failure: Connection reset by peer
# 这里提示报错细想一下是因为什么?

排错

nginx的默认端口是80,而我却把它的8080端口映射出来了。

好,有了思路我们现在来解决,有两种方式:

1、把刚才创建的容器删掉重建,重新映射80端口,这里不细讲。

2、进入容器把nginx的默认端口80给他修改为8080,再重启容器里的nginx即可(这里重启的是容器里的nginx,可不是nginx这个容器哈)。

# 顺便复习下怎么进入容器
[root@lavm-5wutyiasaf ~]# docker exec -it 6f8cf788e20c /bin/bash
root@6f8cf788e20c:/#

# 找到nginx的配置文件
root@6f8cf788e20c:/# cd /etc/nginx/conf.d/
root@6f8cf788e20c:/etc/nginx/conf.d# ls
default.conf

# 修改nginx的默认端口,容器里没有vi的命令,所有我们用sed来替换。注意listen和80中间有多少个空格,替换的时候看清楚。
root@6f8cf788e20c:/etc/nginx/conf.d# cat default.conf |grep listen
    listen       80;
    listen  [::]:80;
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
root@6f8cf788e20c:/etc/nginx/conf.d# sed -i 's/listen       80;/listen       8080;/' default.conf
root@6f8cf788e20c:/etc/nginx/conf.d# cat default.conf |grep listen
    listen       8080;
    listen  [::]:80;
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

# 上面看到了端口已经修改完成,现在重启nginx服务
root@6f8cf788e20c:/etc/nginx/conf.d# nginx -t                                    # 自检配置文件是否ok
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@6f8cf788e20c:/etc/nginx/conf.d# nginx -s reload                    # 重启nginx    
2025/06/13 09:59:33 [notice] 127#127: signal process started

# 验证nginx是否可正常访问
[root@lavm-5wutyiasaf ~]# curl http://localhost: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、常见问题

权限问题不足的问题

执行docker命令时出现权限不足错误

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

解决方法1:使用sudo

再docker命令前加上sudo提权。

解决方法2:将用户添加到用户组

sudo groupadd docker         # 添加 docker 用户组
sudo gpasswd -a $USER docker    # 将当前用户加入到 docker 用户组
newgrp docker                     # 更新用户组
docker ps                        # 测试 docker 命令