《‘狂’人日记》---Docker从入门到进阶之基础操作(一)

简介: Docker入门篇,以一些简单例子向您展示Docker的便捷

1.安装并启用

1.1 CentOS安装Docker

# 更新源
[root@base ~]# yum -y update
# 安装docker
[root@base ~]# yum -y install docker
# 查看docker版本
[root@base ~]# docker info
# 启动docker、设置开机自启
[root@base ~]# systemctl start docker
[root@base ~]# systemctl enable docker

docker info

start_docker

1.2 Ubuntu安装Docker

# 更新源
f@baseu:~$ sudo apt-get -y update
# 安装Docker
f@baseu:~$ sudo apt-get -y install docker.io
# 查看docker版本信息
f@baseu:~$ sudo docker info
# 启动和设置docker开机自启
f@baseu:~$ sudo systemctl start docker
f@baseu:~$ sudo systemctl enable docker

image.png

image.png

1.3 Windows安装Docker

Docker 官网下载安装

下载Windows版本

2.常用命令

2.1 容器命令

2.1.1 ps命令

2.1.1.1 使用语法

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             显示所有容器(默认显示正在运行)
  -f, --filter filter   根据提供的条件过滤输出
      --format string   使用Go模板的漂亮打印容器
      --help            打印使用
  -n, --last int        显示最后创建的n个容器(包括所有状态)(默认值为-1)
  -l, --latest          显示最新创建的容器(包括所有状态)
      --no-trunc        不要截断输出
  -q, --quiet           仅显示数字ID
  -s, --size            显示总文件大小

2.1.1.2 -a 显示所有容器

[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
609b039cc1e6        nginx               "/docker-entrypoin..."   4 seconds ago       Up 2 seconds               80/tcp              nginx
88958f0f4fc8        centos-7            "/bin/bash"              2 weeks ago         Exited (137) 2 weeks ago                       centos7

image.png

2.1.1.2 -f,--filter 过滤

[root@base ~]# docker ps -af name='centos7'
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
88958f0f4fc8        centos-7            "/bin/bash"         2 weeks ago         Exited (137) 2 weeks ago                       centos7

image.png

2.1.1.3 -q 只显示id

[root@base ~]# docker ps -aq
609b039cc1e6
88958f0f4fc8

image.png

2.1.1.4 组合使用

# 查询所有容器中name为 centos7 的 并且只显示id
[root@base ~]# docker ps -aqf name='centos7'
88958f0f4fc8

image.png

2.1.2 run 运行容器

2.1.2.1 使用语法(常用参数语法)

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      
  -d, --detach                                在后台运行容器并打印container ID
  -i, --interactive                           保持STDIN打开,即使未连接
  -p, --publish list                          将容器的端口发布到主机(默认值[])
  -P, --publish-all                           将所有公开端口发布到随机端口
      --restart string                        容器退出时应用的重新启动策略(默认为“否”)
      --name string                           为容器指定一个名称
  -t, --tty                                   分配一个伪TTY

2.1.2.2 -dit 后台运行,打开STDIN并分配一个伪TTY

[root@base ~]# docker run -dit nginx
056121918e5bdf3072cf9dc0e5533569d86e4a8d0c4faf0526be9a431711ba1c
[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
056121918e5b        nginx               "/docker-entrypoin..."   20 seconds ago      Up 18 seconds              80/tcp              unruffled_chandrasekhar
609b039cc1e6        nginx               "/docker-entrypoin..."   21 hours ago        Exited (0) 21 hours ago                        nginx
88958f0f4fc8        centos-7            "/bin/bash"              2 weeks ago         Exited (137) 2 weeks ago                       centos7

image.png

2.1.2.3 --name 设定名字

[root@base ~]# docker run -dit --name nginx_named nginx
8925665a0958b70246993cecfa24e3580c114ecfd5fa2a15eb8cbc754d765b1a
[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
8925665a0958        nginx               "/docker-entrypoin..."   2 seconds ago       Up 2 seconds               80/tcp              nginx_named
056121918e5b        nginx               "/docker-entrypoin..."   6 minutes ago       Up 6 minutes               80/tcp              unruffled_chandrasekhar
609b039cc1e6        nginx               "/docker-entrypoin..."   21 hours ago        Exited (0) 21 hours ago                        nginx
88958f0f4fc8        centos-7            "/bin/bash"              2 weeks ago         Exited (137) 2 weeks ago                       centos7

image.png

2.1.2.4 -p 指定端口/ -P 随机端口

# -p指定端口
[root@base ~]# docker run -dit --name nginx -p 8080:80 nginx
c484ad61ed99710970889a54c94af47fdb6c50096331ac75e022e2b8383ec14a
[root@base ~]# curl 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>


# -P 随机端口(32768起)
[root@base ~]# docker run -dit --name nginx_P -P nginx
3bbda870da081d5bd79e1bcc5c1ef6364382264f1f59d57ba45441af2f1c5161
[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS                   NAMES
3bbda870da08        nginx               "/docker-entrypoin..."   2 seconds ago        Up 2 seconds               0.0.0.0:32768->80/tcp   nginx_P
c484ad61ed99        nginx               "/docker-entrypoin..."   About a minute ago   Up About a minute          0.0.0.0:8080->80/tcp    nginx
88958f0f4fc8        centos-7            "/bin/bash"              2 weeks ago          Exited (137) 2 weeks ago                           centos7
[root@base ~]# curl localhost:32768
<!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>

image.png

image.png

2.1.3 rm删除容器

2.1.3.1 使用语法

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
      --help      Print usage
  -l, --link      Remove the specified link
  -v, --volumes   Remove the volumes associated with the container

2.1.3.2 删除容器

[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                   NAMES
3bbda870da08        nginx               "/docker-entrypoin..."   21 hours ago        Up 2 seconds               0.0.0.0:32768->80/tcp   nginx_P
c484ad61ed99        nginx               "/docker-entrypoin..."   21 hours ago        Exited (0) 21 hours ago                            nginx
88958f0f4fc8        centos-7            "/bin/bash"              2 weeks ago         Exited (137) 2 weeks ago                           centos7
[root@base ~]# docker rm nginx
nginx
[root@base ~]# docker rm nginx_P
Error response from daemon: You cannot remove a running container 3bbda870da081d5bd79e1bcc5c1ef6364382264f1f59d57ba45441af2f1c5161. Stop the container before attempting removal or use -f

image.png

2.1.3.3 -f 强制删除容器


[root@base ~]# docker rm -f nginx_P
nginx_P
[root@base ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
88958f0f4fc8        centos-7            "/bin/bash"         2 weeks ago         Exited (137) 2 weeks ago                       centos7

image.png

目录
相关文章
|
1月前
|
Docker 容器
【Docker】掌握 Docker 镜像操作:从基础到进阶
【Docker】掌握 Docker 镜像操作:从基础到进阶
|
1月前
|
存储 虚拟化 数据中心
Docker容器化应用程序的入门指南
【4月更文挑战第28天】
177 0
|
1月前
|
应用服务中间件 持续交付 nginx
【Docker专栏】Docker入门指南:快速构建你的第一个容器
【5月更文挑战第7天】Docker 入门指南:容器化应用利器。了解 Docker 核心概念——镜像、容器和仓库。安装 Docker 后,运行官方 `hello-world` 验证安装,再尝试运行 `nginx` Web 服务器。通过端口映射访问容器内服务,学习管理容器命令。创建自定义镜像,编写 Dockerfile,实现 Python Web 应用容器化。Docker 助力高效开发与运维,探索更多自动化部署与微服务场景。
【Docker专栏】Docker入门指南:快速构建你的第一个容器
|
1月前
|
安全 Linux 数据库
Docker 入门详解
通过遵循这些步骤,你将能够快速入门 Docker 并开始探索容器化的世界,关注 V 哥,技术之路一起成长。
|
1月前
|
Shell iOS开发 Docker
|
1月前
|
Ubuntu 应用服务中间件 Shell
Docker入门
Docker入门
39 0
|
1月前
|
存储 Ubuntu Linux
Docker 从入门到实践:Docker介绍
Docker 从入门到实践:Docker介绍
|
1月前
|
存储 Apache Swift
无限套娃_docker入门(镜像、容器、仓库)
无限套娃_docker入门(镜像、容器、仓库)
59 0
|
1月前
|
运维 Cloud Native Linux
【云原生|Docker系列第4篇】Docker的容器的入门实践
【云原生|Docker系列第4篇】Docker的容器的入门实践
|
1月前
|
Cloud Native 应用服务中间件 nginx
【云原生|Docker系列第3篇】Docker镜像的入门实践
【云原生|Docker系列第3篇】Docker镜像的入门实践
106 0