Docker系列(二)---基本操作和使用

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

操作命令

Docker提供了一组功能强大的操作命令,通过它们可以完成管理、操作等任务。

命令格式:

docker [option] [command] [arguments]

命令说明:

  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

查看子命令的帮助:

docker docker-subcommand --help

使用镜像

Docker 容器是根据 Docker Image构建的。 默认情况下,Docker 从 Docker Hub 中提取这些镜像,Docker 是 Docker 项目背后的公司,管理着 Docker 注册中心。 任何人都可以在 Docker Hub 上托管他们的 Docker 映像,所以你需要的大多数应用程序和 Linux 发行版都会在那里托管映像。

测试是否可用

docker run hello-world

Docker 最初无法在本地找到 hello-world 映像,因此它从 Docker Hub (默认存储库)下载了这个映像。 下载IMage后,Docker 根据Image和执行的容器内的应用程序创建一个容器,显示消息。

常用命令

搜索镜像

magc@magc-ThinkPad-T480:~$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   10811               [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   419                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   244                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   214                                     [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   106                 [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                                      [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   68                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44                  [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                                      [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   19
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                                      [OK]
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                                      [OK]
eclipse/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   12                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                                      [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   4
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                                       [OK]
pivotaldata/ubuntu16.04-build                             Ubuntu 16.04 image for GPDB compilation         2
smartentry/ubuntu                                         ubuntu with smartentry                          1                                       [OK]
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              1
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]

更换国内Docker仓库

在服务器本机的/etc/docker/下新建daemon.json,并输入以下内容:如果需要更换不同的镜像地址,直接替换掉“https://alzgoonw.mirror.aliyuncs.com”即可

{ 
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"] 
}

重启并加载配置:

systemctl daemon-reload   //载入daemon.json
systemctl restart docker   //重启docker

查看是否生效:
通过docker info命令,可以在底部看到仓库地址已经更新了。

下载镜像

docker pull ubuntu

在下载了一个映像之后,您可以使用 run 子命令使用下载的映像运行一个容器。 正如您在 hello-world 示例中看到的那样,如果使用 run 子命令执行 Docker 时没有下载映像,Docker 客户机将首先下载映像,然后使用它运行一个容器。

查看本地镜像

magc@magc-ThinkPad-T480:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4e5021d210f6        4 weeks ago         64.2MB

本地的镜像可以运行形成容器,它可以被修改,生成新的镜像文件,

运行容器

使用镜像启动容器

作为一个例子,让我们使用最新的 Ubuntu 映像运行一个容器。 I 和 t 开关的组合使您可以对容器进行交互式 shell 访问:

docker run -it ubuntu

注:其中i和t参数的组合会使你可以使用与容器进行交互式的Shell
启动后,将得到类似下面的终端提示:

root@d9b100f2f636:/#

注意命令提示符中的容器 id。 在这个示例中,它是 d9b100f2f636。 稍后您将需要该容器 ID 来标识需要移除的容器。

现在,您可以在容器内运行任何命令。 例如,让我们更新容器内的包数据库。 你不需要给任何命令加上 sudo 的前缀,因为你是以 root 用户的身份在容器中操作的:

apt-get update

这里试着安装一下nodejs:

sudo apt-get install nodejs

安装之后,查看Nodejs的版本:

node -v

注:在容器中所作的修改只适用于当前容器。直接退出后将消失。

退出容器

使用exit命令来退出当前容器

exit

管理Docker

查看容器

使用 Docker 一段时间后,您的计算机上将有许多活动(正在运行)和非活动容器。 要查看活动的文件,请使用:

# 查看活动的容器
docker ps 
# 查看所有容器(活动的和非活动的)
docker ps -a
# 查看最新创建的容器
docker ps -l

例如:

magc@magc-ThinkPad-T480:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
cb997f81f82c        ubuntu              "/bin/bash"         2 minutes ago       Exited (127) 30 seconds ago                       charming_dewdney
6e5462d590b6        ubuntu              "/bin/bash"         8 minutes ago       Exited (0) 3 minutes ago                          modest_blackburn

magc@magc-ThinkPad-T480:~$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
cb997f81f82c        ubuntu              "/bin/bash"         4 minutes ago       Exited (127) 2 minutes ago                       charming_dewdney

启动容器

若要启动已停止的容器,请使用 docker start,后跟容器 ID 或容器名称。 让我们启动 ID 为 cb997f81f82c的基于 ubuntu 的容器:

magc@magc-ThinkPad-T480:~$ docker start cb997f81f82c
cb997f81f82c
magc@magc-ThinkPad-T480:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
cb997f81f82c        ubuntu              "/bin/bash"         7 minutes ago       Up 12 seconds                           charming_dewdney

注: 启动新容器时,可以通过参数 --name 来指定一个名字, 还可以使用–rm参数,代表该容器停止时就自动删除

连接到正在运行的容器

先找到要连接的容器的ID,
docker exec -it /bin/bash

停止容器

若要停止正在运行的容器,请使用 docker stop,后跟容器 ID 或名称。

magc@magc-ThinkPad-T480:~$ docker stop cb997f81f82c
cb997f81f82c
magc@magc-ThinkPad-T480:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

删除容器

当您决定不再需要容器时,使用 docker rm 命令移除它,同样使用容器 ID 或者名称。 使用 docker ps-a 命令查找与 hello-world 映像关联的容器的容器 ID 或名称并将其删除。

注:容器可以转换成IMage,您可以使用这些IMage构建新的容器。

保存容器的修改到镜像中

启动 Docker 映像时,您可以像使用虚拟机一样创建、修改和删除文件。 您所做的更改只适用于该容器。 您可以启动和停止它,但是一旦使用 docker rm 命令销毁它,更改将永远丢失。

在这里,我们将展示如何将容器中修改保存到镜像中,以便下次直接使用最新的镜像内容。

例如,在上面我们在ubuntu的一个容器中安装了Nodejs,此时容器的状态与原始镜像已经不同,如果我们想将最新的变更保存到镜像中,以便后面可以直接使用成果,可以用下面的办法,生成一个新的镜像:

magc@magc-ThinkPad-T480:~$ docker commit -m "added Nodejs" -a "magc" 6e5462d590b6 magc/ubuntu-nodejs
sha256:824e7cdb2d5020ea901474b6b5d764ae5dc97c23223ee95c0bf17f60f1943ab1

查看本地的镜像:

magc@magc-ThinkPad-T480:~$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
magc/ubuntu-nodejs   latest              824e7cdb2d50        28 seconds ago      153MB
ubuntu               latest              4e5021d210f6        4 weeks ago         64.2MB

可以发现,其中magc/ubuntu-nodejs 就是新生成的镜像文件,大小已经比原来ubuntu镜像大了不少。

debootstrap定制arm平台linux debootstrap制作arm平台linux一、组件安装 sudo apt install qemu-user-static sudo apt install ubuntu-dev-tools sudo apt install debootstrap、命令说明 sudo debootstrap --arch [平台] [发行版本代号] [目录]三、安装debian sudo debootstrap --arch armhf buster /mnt/debian https://mirrors.tuna. 阅读详情

相关推荐

ubuntu20上安装使用Docker

1:安装Docker 首先更新所有的安装包: sudo apt update 然后安装一些必备软件包,以便通过HTTPS使用软件包: sudo apt install apt-transport-https ca-certificates curl software-properties-common 将官方Docker存储库的GPG密钥添加到系统中: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

曾立文的博客 3416

Win7 x64系统下PHP开发环境搭建---Apache2.4.18+PHP5.6.16+MySQL5.7.10+phpMyAdmin4.5.3.1

Win7 x64系统下PHP开发环境搭建,包含详细操作步骤及所需安装包百度盘下载 (Apache2.4.18+PHP5.6.16+MySQL5.7.10+phpMyAdmin4.5.3.1)

Docker实验()搭建Docker并导入镜像与Docker基本操作

一.概念讲解 1.镜像的分层结构 共享宿主机的kerne base镜像提供的是最小的Linux发行版 同一docker主机支持运行多种Linux发行版 采用分层结构的最大好处是:共享资源 2. Copy-on-Write可写容器层 容器层以下所有镜像层都是只读的 docker从上往下依次查找文件 容器层保存镜像变化的部分,并不会对镜像本身进行任何修改 一个镜像最多127层 3.镜像的构...

qq_39376481的博客 976

win10家庭版安装docker过程及问题解决记录

小白入门学习记录自用

Promiselove888的博客 1132

ThinkPad WIN7 Docker Quickstart Terminal启动报虚拟disable

win7启动docker,卡在虚拟disable问题,未开启电脑CPU虚拟化,百度说是要bios界面去修改,开启CPU虚拟化,这里记录下怎么进去BIOS界面,以及怎么修改配置 重启电脑,这里是ThinkPad win7。重启后在加载ThinkPad界面时就按F1键,看其他文章,有的说是F2或F10或F12等,这里是F1。所以电脑不一样按键不一样。这时会出现一个介绍界面,细看下介绍,以及按钮操作,比如上下翻页、保存等 左右翻页到Security选项卡,选择Virtualization,修改配置为Enab.

你当像鸟飞往你的山~~ 318

在win10专业版里安装Docker

    Docker 是一个开源的、轻量级的容器引擎,主要运行于 Linux Windows,用于创建、管理编排容器。 VMware 虚拟机相比,Docker 使用容器承载应用程序,而不使用操作系统,所以它的开销很少,性能很高。但是,Docker 对应用程序的隔离不如虚拟机彻底,所以它并不能完全取代 VMware。     系统平台:win10专业版     注意:由于Docker需要WSL2,以及BIOS的虚拟化技术支持,所有在配置的过程中,需要多次手动重启电脑。 1、下载Docker及其WSL2

sanqima的专栏 824

docker + win10 (联想G50-45)

1、先下载一个检测小软件,1M左右,可以检测电脑是否支持VT: 左侧的对勾表示电脑支持VT,如果是红色的叉号就是电脑不支持,就不要费劲了。。。 左侧是对勾,右侧是对勾可以直接安装docker了,如果右侧是叉号,需要设置打开VT。 2、在控制面板打开Hyper-V 控制面板 -> 程序功能 -> 启用或关闭Windows功能 -> 勾选Hyper-V Hyper-V下面的子选项也要确认勾选了。 3、在cmd中输入以下命令检查Hyper-V是否启动成功: systeminfo 如果出现下

qq_43470820的博客 1350

Docker系列整理()docker基本操作

文章目录查看容器查看所有的容器查看最新创建的容器查看最新创建的n个容器创建容器创建容器创建容器+容器启动后台型容器交互型容器容器启动容器重启容器停止容器删除单个删除批量删除 docker 中的容器就是一个轻量级的虚拟机,是镜像运行起来的一个状态,下面来看看容器的一些的基本操作。镜像就像是一个安装程序,而容器则是程序运行时的一个状态。 查看容器 启动 docker 后,我们可以使用 docker p...

zhouym_的博客 1096

Docker基本操作

docker

weixin_59496235的博客 9066

Docker系列Docker 镜像

Docker 镜像是 Docker 技术的核心,它通过分层存储只读模板的方式,为容器提供了高效、一致的运行环境。通过拉取推送镜像,开发者可以轻松地在不同环境中共享部署应用程序。通过编写Dockerfile优化镜像构建过程,可以创建高效、轻量级的镜像。镜像的版本管理则确保了应用程序在不同环境中的一致性可追溯性。掌握 Docker 镜像的相关知识,对于现代软件开发运维至关重要。👍🏻呗。❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄👍 👍 👍。

CSDN站内信: https://i.csdn.net/#/msg/chat/qyj19920704 1万+

Docker入门指南:快速学习Docker基本操作

了解docker使用docker,熟悉原理

Javaxaiobai的博客 1554

docker基本操作

简化部署过程:可以将应用及其依赖打包成一个镜像,确保在不同环境中能够一致地部署。无论目标环境是开发服务器、测试环境还是生产环境,应用都能以相同的方式运行,减少了因环境差异导致的部署问题。

m0_53291740的博客 1571

Docker基本操作

文章目录一、Docker概述1.1、Docker简介2.Docker与虚拟机的区别1.3、Docker的优势1.4 (image(镜像) repository(仓库) container(容器)2.部署docker2.1安装依赖包2.2镜像加速器个人阿里镜像加速器,可以在阿里镜像服务中的镜像加速器中获得地址 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors3.docker基本操作3.1查看下载好的镜像3.2更改镜像的标签3.3删除镜像

Rikkatang的博客 547

Docker 安装使用

本文描述了 Docker 的一些基本概念,并给出创建启动容器的基本方法。

Lion Chen i 咖啡 1470

Docker系列技术分享() Docker基础操作

文章目录Docker安装系统要求卸载老版本开始安装设置yum仓库安装 Docker Engine启动 DockerDocker工作流程Docker仓库Docker镜像Docker运行Docker架构关于我们 上文 Docker系列技术分享() 容器技术Docker 介绍了容器的历史应用场景,本文将介绍如何从零上手使用Docker,帮助读者更直观的理解容器技术 Docker安装 Docker支持 Linux 、Windows MacOS 等多个平台,当然主阵地还是Linux,因为服务器大多使用

qijigougou的博客 1437

Docker-简介、基本操作

其中 -v 后面的 html 是我们刚刚创建的数据卷(如果没创建会自动创建),而后面的 /user/share/nginx/html 是对应容器中的目录地址。

weixin_44985964的博客 1685

Docker基本操作

Docker基本操作

weixin_45837888的博客 2320

Docker容器的基本操作

文章目录一、Docker镜像的基本操作(image)1.获取镜像2.查看镜像信息——docker images/inspect/tag①基本查看 —— images②根据镜像的唯一标识 ID 号来获取镜像的详细信息——inspect③为本地的镜像添加新的标签——tag3.表示镜像唯一性的方法4.镜像的导出导入——docker save/load5.删除镜像——docker rmiDocker容器操作1.容器的创建与使用docker create/ps/start/stop/run2.容器的进入——

weixin_53560205的博客 1342

Docker() ----Docker基本操作

Docker 镜像,Docker 容器的基本操作docker pull, docker start, docker exec, docker run

Stephen GS的博客 663
上一篇: Docker系列(一) ---安装
下一篇: 深入了解Linux审计子系统(一)--概述
Linux先生
博客等级 码龄20年 97粉丝 43原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值