Docker 笔记(三)--容器

Docker 笔记(三)–容器

记录Docker 安装操作记录,便于查询。

参考

  1. 链接: Docker 入门到实战教程(三)镜像和容器
  2. 链接: docker run中的-itd参数正确使用
  3. 链接: docker官方文档

环境

Centos 7.9

操作

1.启动容器

[root@centos7-18 docker]# docker run -it hello-world

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/
  • -i: 交互式操作。
  • -t: 终端。
  • hello-world: 镜像名。

2.查看容器

[root@centos7-18 docker]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED              STATUS                      PORTS     NAMES
f3cb195bb73d   redis         "docker-entrypoint.s…"   About a minute ago   Exited (0) 47 seconds ago             goofy_cohen
075affeee29d   nginx         "/docker-entrypoint.…"   2 minutes ago        Exited (0) 2 minutes ago              adoring_ride
40da8fbb8f46   hello-world   "/hello"                  7 minutes ago        Exited (0) 7 minutes ago              boring_neumann
[root@centos7-18 docker]# 
  • STATUS 显示容器的运行状态。 注意!停止运行的容器并不会销毁,如果需要容器停止时销毁可以使用–rm 参数.

3.重启容器

[root@centos7-18 docker]# docker restart f3cb195bb73d
f3cb195bb73d
[root@centos7-18 docker]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS      NAMES
f3cb195bb73d   redis     "docker-entrypoint.s…"   18 minutes ago   Up 10 minutes   6379/tcp   goofy_cohen
[root@centos7-18 docker]# 

4.后台运行

[root@centos7-18 docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    605c77e624dd   22 months ago   141MB
redis         latest    7614ae9453d1   23 months ago   113MB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
[root@centos7-18 docker]# 
[root@centos7-18 docker]# docker run -itd --name nginx-demo nginx:latest
74901d661a79a3116d49f6867d5a8e682feb7c9934482ada35a8bd78f01ff35b
  • -d 后台运行
  • –name 容器别名
  • nginx:latest 镜像名称:TAG
[root@centos7-18 docker]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS      NAMES
74901d661a79   nginx:latest   "/docker-entrypoint.…"   16 seconds ago   Up 14 seconds   80/tcp     nginx-demo
f3cb195bb73d   redis          "docker-entrypoint.s…"   30 minutes ago   Up 23 minutes   6379/tcp   goofy_cohen
[root@centos7-18 docker]# 

5.停止运行

[root@centos7-18 docker]# docker stop f3cb195bb73d
f3cb195bb73d
[root@centos7-18 docker]# docker ps

6.进入容器

[root@centos7-18 docker]# docker ps 
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS      NAMES
74901d661a79   nginx:latest   "/docker-entrypoint.…"   24 minutes ago   Up 24 minutes   80/tcp     nginx-demo
f3cb195bb73d   redis          "docker-entrypoint.s…"   55 minutes ago   Up 10 minutes   6379/tcp   goofy_cohen
[root@centos7-18 docker]# docker exec -it f3cb195bb73d /bin/bash
root@f3cb195bb73d:/data# 
[root@centos7-18 docker]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS      NAMES
74901d661a79   nginx:latest   "/docker-entrypoint.…"   27 minutes ago   Up 27 minutes   80/tcp     nginx-demo
f3cb195bb73d   redis          "docker-entrypoint.s…"   57 minutes ago   Up 13 minutes   6379/tcp   goofy_cohen
[root@centos7-18 docker]# docker exec -it nginx-demo /bin/bash
root@74901d661a79:/# 
  • 可以使用容器ID或容器名称进入容器

7.容器快照

7.1 导出快照
[root@centos7-18 docker]# docker export 74901d661a79 > nginx-demo.tar
[root@centos7-18 docker]# ls
daemon.json  nginx-demo.tar
7.2 导入快照
[root@centos7-18 docker]# docker import nginx-demo.tar nginx-demo:1.0
sha256:c2c8866bef605d74ac27fa1f671803139d09f4b9483f7d2312928036323be955
[root@centos7-18 docker]# docker images nginx-demo:1.0
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx-demo   1.0       c2c8866bef60   3 minutes ago   140MB
[root@centos7-18 docker]# 

注意!导入的快照是镜像。

8.删除容器

8.1 删除停止的容器
[root@centos7-18 docker]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED       STATUS                   PORTS      NAMES
f3cb195bb73d   redis         "docker-entrypoint.s…"   2 hours ago   Up 57 minutes            6379/tcp   goofy_cohen
075affeee29d   nginx         "/docker-entrypoint.…"   2 hours ago   Exited (0) 2 hours ago              adoring_ride
40da8fbb8f46   hello-world   "/hello"                  2 hours ago   Exited (0) 2 hours ago              boring_neumann
[root@centos7-18 docker]# docker rm 40da8fbb8f46
40da8fbb8f46
8.2 强制删除容器
[root@centos7-18 docker]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED             STATUS          PORTS      NAMES
74901d661a79   nginx:latest   "/docker-entrypoint.…"   55 minutes ago      Up 55 minutes   80/tcp     nginx-demo
f3cb195bb73d   redis          "docker-entrypoint.s…"   About an hour ago   Up 41 minutes   6379/tcp   goofy_cohen
[root@centos7-18 docker]# docker rm -f 74901d661a79
74901d661a79
8.3 清除所有停止容器
[root@centos7-18 docker]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED          STATUS                      PORTS     NAMES
d4d7b7b33e22   hello-world   "/hello"                  31 seconds ago   Exited (0) 31 seconds ago             quizzical_banach
f3cb195bb73d   redis         "docker-entrypoint.s…"   2 hours ago      Exited (0) 4 seconds ago              goofy_cohen
075affeee29d   nginx         "/docker-entrypoint.…"   2 hours ago      Exited (0) 2 hours ago                adoring_ride
[root@centos7-18 docker]# docker container prune 
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
d4d7b7b33e224d0a757ac2a2dd70ff69bf95bfcf584f0c413d774a1382d98355
f3cb195bb73dfb23a0eca591572b97505d57af5afa9880171836285ea5483b3d
075affeee29db528f9c267f3f8ee6263d9aad39f6da9766a91b39a0b2bf5913d

Total reclaimed space: 1.098kB
[root@centos7-18 docker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@centos7-18 docker]# 

你可能感兴趣的:(docker,笔记,容器)