Docker 之 容器基本操作

一、查看本地容器进程

[[email protected] ~]# docker ps -a     #显示所有容器进程
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
9bd32b3b2ad0        hello-world         "/hello"            20 hours ago        Exited (0) 20 hours ago                       lucid_murdock
fb2d81c98cd2        hello-world         "/hello"            3 weeks ago         Exited (0) 3 weeks ago                        great_bartik
[[email protected] ~]# docker ps          #显示当前运行的容器进程
CONTAINER ID        IMAGE   

二、容器启动命令

[[email protected] ~]# docker run --help
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS:选项
-i : 表示启动一个可交互的容器,并持续打开标准输入
-t : 表示使用终端关联到容器的标准输入输出上
-d : 表示将容器放置后台运行
--rm : 退出后即删除容器
--name : 表示定义容器唯一名称
IMAGE: 表示要运行的镜像
COMMAND : 表示启动容器时运行的命令

三、启动一个交互式的容器

[[email protected] ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
alpine                latest              cc0abc535e36        13 days ago         5.59MB
zhoumingkang/alpine   v3.10.3             cc0abc535e36        13 days ago         5.59MB
alpine                3.9.4               055936d39205        8 months ago        5.53MB
hello-world           latest              fce289e99eb9        12 months ago       1.84kB
[[email protected] ~]# docker run -ti -- name mingkang-test alpine:latest /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
18: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:07:05:02 brd ff:ff:ff:ff:ff:ff
    inet 172.7.5.2/24 brd 172.7.5.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # cat /etc/issue
Welcome to Alpine Linux 3.11
Kernel \r on an \m (\l)

[[email protected] ~]# docker ps      #查看当前运行的容器进程
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
08b02f026afe        alpine:latest       "/bin/sh"           7 seconds ago       Up 5 seconds                            mingkang-test

四、容器退出后即删除--rm(docker ps -a 查询不了记录)

[[email protected] ~]# docker run --rm alpine:latest /bin/echo hello
hello

五、以后台的形式运行一个容器-d

[[email protected] ~]# docker run -d --name mingkang-test3 alpine:latest /bin/sleep 3000
744b31fbaadfc9605d5dbe8d302ef9bd3de8208142aa00c1feffd86804347b01
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
744b31fbaadf        alpine:latest       "/bin/sleep 3000"   18 seconds ago      Up 17 seconds                           mingkang-test3

六、进入一个容器exec

[[email protected] ~]# docker exec -ti 744b31fbaadf /bin/sh      #长长的字符串为容器ID
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sleep 3000
    6 root      0:00 /bin/sh
   11 root      0:00 ps
/ # 

七、停止/重启/启动一个容器(stop/restart/start)

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
744b31fbaadf        alpine:latest       "/bin/sleep 3000"   3 minutes ago       Up 3 minutes                            mingkang-test3
[[email protected] ~]# docker stop 744b31fbaadf    #容器的ID号
744b31fbaadf
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

八、删除docker ps 的内容

[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
744b31fbaadf        alpine:latest       "/bin/sleep 3000"   12 minutes ago      Exited (137) 8 minutes ago                        mingkang-test3
08b02f026afe        alpine:latest       "/bin/sh"           25 minutes ago      Exited (0) 23 minutes ago                         mingkang-test
56760413d596        alpine:latest       "/bin/sh"           28 minutes ago      Exited (0) 27 minutes ago                         mingkang_test
eae500caa715        alpine:latest       "/bin/sh"           34 minutes ago      Exited (0) 29 minutes ago                         vigilant_boyd
c6fed43d8a09        alpine:latest       "/bin/sh"           34 minutes ago      Exited (0) 34 minutes ago                         keen_volhard
058aee8ef1eb        alpine:latest       "/bin/sh"           36 minutes ago      Exited (0) 35 minutes ago                         quirky_morse
6d81bca654ab        alpine:latest       "/bin/sh"           37 minutes ago      Exited (127) 36 minutes ago                       practical_torvalds
adc1a16a7bf6        alpine:latest       "/bin/sh"           39 minutes ago      Exited (0) 38 minutes ago                         unruffled_carson
20a5432e3266        alpine:latest       "/bin/sh"           39 minutes ago      Exited (0) 39 minutes ago                         elated_ellis
08bbe9c13d26        alpine:latest       "/bin/sh"           40 minutes ago      Exited (0) 40 minutes ago                         nifty_kalam
9bd32b3b2ad0        hello-world         "/hello"            21 hours ago        Exited (0) 21 hours ago                           lucid_murdock
fb2d81c98cd2        hello-world         "/hello"            3 weeks ago         Exited (0) 3 weeks ago                            great_bartik
[[email protected] ~]# docker rm keen_volhard quirky_morse practical_torvalds unruffled_carson elated_ellis  nifty_kalam  lucid_murdock great_bartik
keen_volhard
quirky_morse
practical_torvalds
unruffled_carson
elated_ellis
nifty_kalam
lucid_murdock
great_bartik
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
744b31fbaadf        alpine:latest       "/bin/sleep 3000"   13 minutes ago      Exited (137) 9 minutes ago                       mingkang-test3
08b02f026afe        alpine:latest       "/bin/sh"           26 minutes ago      Exited (0) 24 minutes ago                        mingkang-test
56760413d596        alpine:latest       "/bin/sh"           28 minutes ago      Exited (0) 28 minutes ago                        mingkang_test

备注:如需删除在运行的容器,使用docker rm -f 容器名称/容器ID
循环全部删除:
for i in `docker ps -a | grep -i exited|awk ‘{print $1}‘`;do docker rm -f $i;done

九、容器的提交(把已写入内容的容器提交为镜像)

[[email protected] ~]# docker run -d --name myalpine alpine:latest /bin/sleep 30000   #后台长时间运行一个容器
3ccd27be4d19a60f317d187ee99d7fcd76d368dccb0326f8789d6617fb39f457
[[email protected] ~]# docker ps    #查看运行的容器
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
3ccd27be4d19        alpine:latest       "/bin/sleep 30000"   6 seconds ago       Up 4 seconds                            myalpine
[[email protected] ~]# docker exec -ti myalpine /bin/sh    #以交互的方式进入容器
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # touch hello.txt
/ # echo "hello world" > hello.txt
/ # cat hello.txt
hello world
[[email protected] ~]# docker commit -p myalpine myalpine:latest_with_hello.txt
#把名称为myalpine的容器提交为一个新的镜像
sha256:3ce9b8b899ba2018c01ca88fba6c24da6ff1287905a4ff4c60e968f129a22863
[[email protected] ~]# docker images
REPOSITORY            TAG                     IMAGE ID            CREATED             SIZE
myalpine              latest_with_hello.txt   3ce9b8b899ba        20 seconds ago      5.59MB
alpine                latest                  cc0abc535e36        13 days ago         5.59MB
zhoumingkang/alpine   v3.10.3                 cc0abc535e36        13 days ago         5.59MB
alpine                3.9.4                   055936d39205        8 months ago        5.53MB
hello-world           latest                  fce289e99eb9        12 months ago       1.84kB

#启动该myalpin:latest_with_hello.txt 镜像容器后,发现文件已经存在。

十、容器的导出与加载

导出myalpine:latest_with_hello.txt
-----
[[email protected] ~]# docker images
REPOSITORY            TAG                     IMAGE ID            CREATED             SIZE
myalpine              latest_with_hello.txt   3ce9b8b899ba        20 minutes ago      5.59MB
alpine                latest                  cc0abc535e36        13 days ago         5.59MB
zhoumingkang/alpine   v3.10.3                 cc0abc535e36        13 days ago         5.59MB
alpine                3.9.4                   055936d39205        8 months ago        5.53MB
hello-world           latest                  fce289e99eb9        12 months ago       1.84kB
[[email protected] ~]# docker save myalpine:latest_with_hello.txt > myalpine-latest_with_hello.txt.tar
[[email protected] ~]# ls
anaconda-ks.cfg  myalpine-latest_with_hello.txt.tar
-----
删除刚刚导出的镜像
[[email protected] ~]# docker rmi myalpine:latest_with_hello.txt
Untagged: myalpine:latest_with_hello.txt
Deleted: sha256:3ce9b8b899ba2018c01ca88fba6c24da6ff1287905a4ff4c60e968f129a22863
Deleted: sha256:1d89a6f64295ef6889fef9ae917281e94d4d9e5c9b3eb98d5cc25c129f34f4b3
[[email protected] ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
alpine                latest              cc0abc535e36        13 days ago         5.59MB
zhoumingkang/alpine   v3.10.3             cc0abc535e36        13 days ago         5.59MB
alpine                3.9.4               055936d39205        8 months ago        5.53MB
hello-world           latest              fce289e99eb9        12 months ago       1.84kB
-----
重新加载该镜像
[[email protected] ~]# docker load < myalpine-latest_with_hello.txt.tar
e63a9b4f5484: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: myalpine:latest_with_hello.txt
[[email protected] ~]# docker images
REPOSITORY            TAG                     IMAGE ID            CREATED             SIZE
myalpine              latest_with_hello.txt   3ce9b8b899ba        22 minutes ago      5.59MB
alpine                latest                  cc0abc535e36        13 days ago         5.59MB
zhoumingkang/alpine   v3.10.3                 cc0abc535e36        13 days ago         5.59MB
alpine                3.9.4                   055936d39205        8 months ago        5.53MB
hello-world           latest                  fce289e99eb9        12 months ago       1.84kB

十一、容器的日志查看

[[email protected] ~]# docker run 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/

[[email protected] ~]# docker ps -a | grep -i hello      #查看曾经运行的进程
1c5c26a209d4        hello-world:latest   "/hello"            12 seconds ago      Exited (0) 10 seconds ago                       angry_ellis
[[email protected] ~]# docker logs 1c5c26a209d4       #查看该容器运行的日志

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/

[[email protected] ~]# 

原文地址:https://blog.51cto.com/12965094/2465089

时间: 2024-10-10 12:31:07

Docker 之 容器基本操作的相关文章

Docker 容器基本操作

启动容器 运行一次命令并结束进程 $ docker run [--name=cname] IMAGE [COMMAND][ARG...] # run在新容器中执行命令 # --name=cname 自定义容器名字 由于第一使用ubuntu,而本地并不存在ubuntu的image,所以会自动执行pull操作拉取image $ docker run ubuntu echo 'hello the cruel world!' Unable to find image 'ubuntu:latest' lo

跟我一起学docker(四)--容器的基本操作

1.创建容器 Docker的容器十分轻量级,用户可以随时创建或删除容器. 新建容器:docker create Example:docker create –ti ubuntu 说明:使用docker create命令创建的容器处于停止状态,可以使用docker start命令启动它. 新增加了一个name等于test_create的,status等于create 新建并启动容器:docker run Example: docker run ubuntu/bin/echo "Hello Worl

Docker 镜像 &amp;&amp; 容器的基本操作

镜像 && 容器 docker 镜像好比操作系统的镜像(iso) docker 容器好比是已安装运行的操作系统 所以说 docker 镜像文件运行起来之后,就是我们所说的 docker 容器了 Docker Image Operation 1)列出镜像 docker image ls -a 执行效果: 参数说明: REPOSITORY:镜像所在的仓库名称 TAG:镜像标签 IMAGEID:镜像ID CREATED:镜像的创建日期(不是获取该镜像的日期) SIZE:镜像大小 为了区分同一个仓

Docker入门学习2 ——容器基本操作

摘要:介绍Docker容器相关的操作命令. 知识点: run ps start attach exec top stop kill inspect rm logs images rmi pull push commit build 注:可以使用docker COMMAND --help来查看COMMAND的详细用法,本文只介绍常用的子集. 一.启动容器 启动之前未启动过的容器: docker run [-d] [-i] [-t] IMAGE [Command] [Arguments] · -d:

CentOS安装docker,及其基本操作

CentOS安装docker,及其基本操作 一.安装docker Docker要求运行在Centos 7上,要求系统为64位,系统内核版本3.10以上 1.uname -an 查看当前系统版本 2.yum -y install docker 下载安装docker 3.service docker start  启动docker服务 4.docker version  检查docker是否安装成功 当看到下图的信息,就是表示本机docker已经安装成功,很简单 二.镜像操作 创建容器要以镜像为基础

Docker 学习笔记【1】Docker 相关概念,基本操作

计划:Docker 学习笔记[2] Docker 基础操作实操记录,Docker仓库.数据卷,网络基础学习---40 注:所有操作在root下执行 --1--概念: 1.Docker镜像: 镜像就是一个只读的模板,用于创建docker容器. Docker提供了简单的机制创建或者更新现有镜像,也可以从别处拿来现成镜像直接使用. 2.Docker容器: 可以认为是精简版的linux运行环境包含 [root权限,进程空间,用户空间,网络空间等]和应用程序 另外:镜像是只读的,容器基于镜像启动后创建一层

Docker的容器

容器是一个打包了应用和服务的环境,是一个轻量级的虚拟机,每一个容器都由一组特定的应用和必要的依赖库组成. 容器的管理操作 容器常见的命令:查看.创建.启动.终止和删除 创建容器 docker create docker run 二者的区别在于docker create创建的容器处于停止状态,docker run 创建的容器处于启动状态 用docker create创建一个停止状态的容器 [[email protected] ~]# docker create centos:6.7 Unable

理解Docker单机容器网络

在” 理解Docker单机容器网络 “一文中,还有一个Docker容器网络的功能尚未提及,那就是Docker容器的端口映射.即将容器的服务端口P’ 绑定到宿主机的端口P上,最终达到一种效果:外部程序通过宿主机的P端口访问,就像直接访问Docker容器网络内部容器提供的服务一样. Docker针对端口映射前后有两种方案,一种是1.7版本之前docker-proxy+iptables DNAT 的方式:另一种则是1.7版本(及之后)提供的完全由iptables DNAT实现的端口映射.不过在目前do

docker迁移容器

Docker中容器的备份.恢复和迁移1. 备份容器首先,为了备份Docker中的容器,我们会想看看我们想要备份的容器列表.要达成该目的,我们需要在我们运行着Docker引擎,并已创建了容器的Linux机器中运行 docker ps 命令 # docker ps 在此之后,我们要选择我们想要备份的容器,然后去创建该容器的快照.我们可以使用 docker commit 命令来创建快照. # docker commit -p 30b8f18f20b4 container-backup 该命令会生成一个