docker容器一镜像、容器的简单操作

  • 一、镜像操作
    • 1.搜索镜像
    • 2.下载镜像
    • 3.查看镜像列表
    • 4.导出镜像
    • 5.删除镜像
    • 6.导入镜像
  • 二、容器操作
    • 1.启动容器
    • 2.指定nginx镜像创建docker-nginx容器
    • 3.启动docker-nginx
    • 4.进入正在运行的容器
    • 5.删除容器

一、镜像操作

1.搜索镜像

[root@docker-server ~]# docker search nginx|head -2
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        9563                [OK]        

2.下载镜像

[root@docker-server ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
802b00ed6f79: Pull complete 
e9d0e0ea682b: Pull complete 
d8b7092b9221: Pull complete 
Digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
Status: Downloaded newer image for nginx:latest

3.查看镜像列表

[root@docker-server ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 days ago          1.84kB
nginx               latest              06144b287844        6 days ago          109MB
alpine              latest              11cd0b38bc3c        2 months ago        4.41MB

4.导出镜像

[root@docker-server ~]# docker save nginx >/root/nginx.tar.gz
[root@docker-server ~]# ll /root/nginx.tar.gz 
-rw-r--r--. 1 root root 112784384 Sep 11 12:34 /root/nginx.tar.gz

5.删除镜像

[root@docker-server ~]# docker image rm nginx
Untagged: nginx:latest
Untagged: nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
Deleted: sha256:06144b2878448774e55577ae7d66b5f43a87c2e44322b3884e4e6c70d070b262
Deleted: sha256:824a442ee3c96744d75be3737a22cc6a47aebad1b30be67f3c2f8f29cb0aa879
Deleted: sha256:8e3d1e9e4945f930c93c30617512998437f6edafd86676770d29b1581f2520bb
Deleted: sha256:8b15606a9e3e430cb7ba739fde2fbb3734a19f8a59a825ffa877f9be49059817
[root@docker-server ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 days ago          1.84kB
alpine              latest              11cd0b38bc3c        2 months ago        4.41MB

6.导入镜像

[root@docker-server ~]# docker load < nginx.tar.gz 
8b15606a9e3e: Loading layer  58.44MB/58.44MB
67d3ae5dfa34: Loading layer  54.32MB/54.32MB
579c75bb43c0: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest
[root@docker-server ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 days ago          1.84kB
nginx               latest              06144b287844        6 days ago          109MB
alpine              latest              11cd0b38bc3c        2 months ago        4.41MB

二、容器操作

1.启动容器

[root@docker-server ~]# docker run nginx echo "Hello World"
Hello World
查看所有容器(启动与未启动)
[root@docker-server ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
46e63b64fa44        nginx               "echo 'Hello World'"   21 seconds ago      Exited (0) 19 seconds ago 

2.指定nginx镜像创建docker-nginx容器

[root@docker-server ~]# docker run --name docker-nginx -i -t nginx /bin/bash
root@4c127ca38bf4:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@4c127ca38bf4:/# exit
exit
====》参数
-t  建立伪终端
-i  打开标准输入

3.启动docker-nginx

[root@docker-server ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
4c127ca38bf4        nginx               "/bin/bash"            8 minutes ago       Exited (0) 15 seconds ago                       docker-nginx
46e63b64fa44        nginx               "echo 'Hello World'"   14 minutes ago      Exited (0) 14 minutes ago 

[root@docker-server ~]# docker start docker-nginx
docker-nginx
[root@docker-server ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4c127ca38bf4        nginx               "/bin/bash"         9 minutes ago       Up 5 seconds        80/tcp              docker-nginx

4.进入正在运行的容器

方式一:(退出后进程结束)
[root@docker-server ~]# docker attach docker-nginx 
root@4c127ca38bf4:/#  
方式二:(退出后进程为结束)
获取容器pid
[root@docker-server ~]# docker inspect -f "{{ .State.Pid }}" docker-nginx  
14423
[root@docker-server ~]# nsenter -t 14423 -m -u -i -n -p
root@4c127ca38bf4:/# 
方式三
[root@docker-server ~]# docker exec -it docker-nginx /bin/bash
root@4c127ca38bf4:/# 

5.删除容器

[root@docker-server ~]# docker rm docker-nginx

你可能感兴趣的:(Docker)