docker容器管理

发布于 2023-11-29  1361 次阅读


创建容器

-d 后台运行
-i 保持标准输入打开
-P 随机分配端口
-p 指定端口映射 宿主机:容器机
-v 指定映射到宿主机的某个目录
-t 分配一个终端
--rm 容器推出后自动删除
--name 指定容器名

docker run

新建并启动容器,退出后容器停止掉了

docker run -it centos bash
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS                          PORTS     NAMES
a785638511c6   centos    "bash"    47 seconds ago       Exited (0) 44 seconds ago                 mycentos
650e6831dcb7   centos    "bash"    About a minute ago   Exited (0) About a minute ago             silly_lamport

端口映射

docker run -d -p 80:80 nginx:1.8 #指定端口映射
docker run -d -P nginx:1.8 #随机端口
[root@localhost ~]# docker run -d -P nginx:1.8
Unable to find image 'nginx:1.8' locally
1.8: Pulling from library/nginx
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/library/nginx:1.8 to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
efd26ecc9548: Pull complete
a3ed95caeb02: Pull complete
24941909ea54: Pull complete
7e605cb95896: Pull complete
Digest: sha256:c97ee70c4048fe79765f7c2ec0931957c2898f47400128f4f3640d0ae5d60d10
Status: Downloaded newer image for nginx:1.8
af5171f5b9775f52ddd4fc1bbce9040976db5dd440acc10e0e954c1235fd5477
[root@localhost ~]# ss -tnl
State      Recv-Q Send-Q              Local Address:Port                             Peer Address:Port
LISTEN     0      128                             *:22                                          *:*
LISTEN     0      100                     127.0.0.1:25                                          *:*
LISTEN     0      128                             *:32768                                       *:*
LISTEN     0      128                             *:32769                                       *:*
LISTEN     0      128                            :::22                                         :::*
LISTEN     0      100                           ::1:25                                         :::*
LISTEN     0      128                            :::32768                                      :::*
LISTEN     0      128                            :::32769                                      :::*
默认从32769开始

指定运行参数

容器需要在创建的时候指定一个运行参数才能在后台运行

暂停容器


取消
docker unpause 容器名

停止容器

docker stop

删除容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                CREATED         STATUS         PORTS     NAMES
e6be584352bb   centos    "tail -f /etc/hosts"   4 minutes ago   Up 4 minutes             jovial_ellis
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                CREATED         STATUS         PORTS     NAMES
e6be584352bb   centos    "tail -f /etc/hosts"   4 minutes ago   Up 4 minutes             jovial_ellis
[root@localhost ~]# docker rm -f e6be584352bb
e6be584352bb

批量删除

docker rm -f `docker ps -qa`

进入容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED              STATUS              PORTS                               NAMES
0674dda52f6d   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:82->80/tcp, :::82->80/tcp   nginx-1
b5f08ef45147   nginx     "/docker-entrypoint.…"   5 minutes ago        Up 3 minutes        0.0.0.0:81->80/tcp, :::81->80/tcp   mynginx
da9632017d99   centos    "/bin/bash"               5 minutes ago        Up 5 minutes                                            goofy_nobel
e2fa74ec99cd   nginx     "/docker-entrypoint.…"   6 minutes ago        Up 6 minutes        0.0.0.0:80->80/tcp, :::80->80/tcp   wonderful_yalow
[root@localhost ~]# docker exec -it da9632017d99 bash
[root@da9632017d99 /]# touch test.txt
[root@da9632017d99 /]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED         STATUS         PORTS                               NAMES
0674dda52f6d   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:82->80/tcp, :::82->80/tcp   nginx-1
b5f08ef45147   nginx     "/docker-entrypoint.…"   5 minutes ago   Up 4 minutes   0.0.0.0:81->80/tcp, :::81->80/tcp   mynginx
da9632017d99   centos    "/bin/bash"               6 minutes ago   Up 6 minutes                                       goofy_nobel
e2fa74ec99cd   nginx     "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   wonderful_yalow
#进入容器执行完命令后容器依旧存在

[root@localhost ~]# docker export da9632017d99 -o /opt/centos.tar
[root@localhost ~]# ll /opt
总用量 232984
-rw-------. 1 root root 238573056 11月 29 18:46 centos.tar

## 导入导出容器
```bash
#导出
[root@docker-server ~]# docker run -d -it centos
43f2397b9456d27a3b84dba0d79ae9a1dd8dddf40440d7d73fca71cddea0e10d
[root@docker-server ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
43f2397b9456   centos    "/bin/bash"   2 seconds ago   Up 2 seconds             awesome_rubin
[root@docker-server ~]# docker export -o /opt/centos.tar 43f
[root@docker-server ~]# ll /opt/centos.tar
-rw------- 1 root root 216525312 6月   9 13:28 /opt/centos.tar
#导入
[root@docker-server ~]# docker import /opt/centos.tar mycentos:v1
sha256:acf250a6cabb56e0464102dabedb0a562f933facd3cd7b387e665459da46bf29
[root@docker-server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mycentos      v1        acf250a6cabb   9 seconds ago   209MB
nginx         latest    d1a364dc548d   2 weeks ago     133MB
hello-world   latest    d1165f221234   3 months ago    13.3kB
centos        latest    300e315adb2f   6 months ago    209MB
  • alipay_img
  • wechat_img
一朝风月,万古长空
最后更新于 2023-11-29