Docker镜像常用命令
1 一定要按照前一篇强调内容中的讲解配置加速器
2 docker命令需要加root权限执行,需要加sudo,不然会报错permission denied
- 搜索镜像
docker search java
执行该命令后Docker会在Docker Hub上搜索含有java关键字的镜像仓库 - 下载镜像
docker pull java
执行该命令,Docker会从Docker Hub的Java仓库中下载最新的Java镜像。
该指令可指定下载的镜像标签和Docker Register地址
docker pull reg.itmuch.com/java:7 - 列出镜像
docker images
执行该命令,可列出已下载的镜像 - 删除镜像
docker rmi
删除所有镜像
docker rmi -f $(docker images)
示例如下,注意要用root权限,或者sudo
xq@xq-VPCEG17YC:/etc/docker$ sudo docker search java
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
java Java is a concurrent, class-based, and obj... 1370 [OK]
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.23 over... 212 [OK]
isuper/java-oracle This repository contains all java releases... 56 [OK]
develar/java 55 [OK]
lwieske/java-8 Oracle Java 8 Container - Full + Slim - Ba... 33 [OK]
ibmjava Official IBM® SDK, Java™ Technology Editio... 28 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 26 [OK]
nimmis/alpine-java This is docker images of Alpine 3.5 with d... 11 [OK]
nimmis/java This is docker images of Ubuntu 14.04 LTS ... 10 [OK]
1science/java Java Docker images based on Alpine Linux 6 [OK]
blacklabelops/java Oracle Java Base Images. Alpine and CentOS... 4 [OK]
andreluiznsilva/java Docker images for java applications 4 [OK]
frekele/java docker run --rm --name java frekele/java 4 [OK]
stangenberg/java A java container 1 [OK]
dwolla/java Dwolla’s custom Java image 1 [OK]
beevelop/java Personal Java image based on Ubuntu 15.10 ... 1 [OK]
esycat/java Oracle Java 8 on Ubuntu LTS 0 [OK]
instructure/java A java base image based on instructure/core 0 [OK]
anduin/java Anduin Java base image 0 [OK]
bigtruedata/java Base image for java applications 0 [OK]
synopsis/java Docker image with Java 0 [OK]
axeclbr/java Java on Alpinelinux in Docker. 0 [OK]
konstruktoid/java Java base image 0 [OK]
infotechsoft/java Java on CentOS 0 [OK]
vicamo/java 0 [OK]
xq@xq-VPCEG17YC:/etc/docker$ sudo docker pull java
Using default tag: latest
latest: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
xq@xq-VPCEG17YC:/etc/docker$ sudo docker images
[sudo] xq 的密码:
REPOSITORY TAG IMAGE ID CREATED SIZE
java latest d23bdf5b1b1b 4 months ago 643 MB
hello-world latest 48b5124b2768 4 months ago 1.84 kB
xq@xq-VPCEG17YC:/etc/docker$ sudo docker rmi -f 48b5124b2768
Untagged: hello-world:latest
Untagged: hello-world@sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Deleted: sha256:48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233
Docker容器常用命令
- 新建并启动容器
docker run
下面列出一些常用选项
-d 后台运行
-P 随机端口映射
-p 指定端口,有以下四种 ip:hostPort:containerPort ip::containerPort hostPort:containerPort containerPort
–network 指定网络模式 –network=bridge 连接默认的网桥;–network=host 容器使用宿主机的网络;–network=container.NAME_or_ID 新建的容器使用已有容器的网络;–network=none 不配置网络,用户可自定义 - 列出容器
docker ps
可列出运行中的容器 - 停止容器
docker stop 容器ID/容器名称 - 强制停止容器
docker kill 容器ID/容器名称 - 启动已停止的容器
docker start 容器ID/容器名称 - 重启容器
docker restart 容器ID/容器名称 - 进入容器
docker attach 容器ID/容器名称
多个窗口使用attach进入同一个容器时,所有窗口会同步显示;若某个窗口阻塞,则其他窗口均无法执行。
使用nsenter进入容器
需要找到容器第一个进程的PID
docker inspect –format “{{.State.Pid}}” CONTAINERID获取PID之后,就可以通过nserter命令进入容器nsenter–target“ PID” –mount –uts –ipc –net –pid - 删除容器
docker rm 容器ID/容器名称
该命令只删除已停止的容器;需要删除正在运行的容器,需要加上-f参数。 - 删除所有的容器
docker rm -f ${docker ps -a -p}
示例如下:
xq@xq-VPCEG17YC:/etc/docker$ sudo docker run java /bin/echo 'Hello world'
Hello world
xq@xq-VPCEG17YC:/etc/docker$ sudo docker run -d -p 91:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
ff3d52d8f55f: Pull complete
b05436c68d6a: Pull complete
961dd3f5d836: Pull complete
Digest: sha256:12d30ce421ad530494d588f87b2328ddc3cae666e77ea1ae5ac3a6661e52cde6
Status: Downloaded newer image for nginx:latest
12ce62529271ff1284dced96f5ffbe0fc6aca72ae5158a49dc02f918b2827b79
之后可以访问91端口
xq@xq-VPCEG17YC:/etc/docker$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12ce62529271 nginx "nginx -g 'daemon ..." 5 minutes ago Up 5 minutes 0.0.0.0:91->80/tcp musing_liskov
xq@xq-VPCEG17YC:/etc/docker$ sudo docker restart 12ce62529271
12ce62529271
xq@xq-VPCEG17YC:/etc/docker$ sudo docker inspect --format "{{.State.Pid}}" 12ce62529271
15858
xq@xq-VPCEG17YC:/etc/docker$ sudo nsenter --target 15858 --mount --uts --ipc --net --pid
root@12ce62529271:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
nsenster命令封住了一个shell,我们就可以进去容器了