3.2 Docker基本使用(增删改查)

Docker 容器基础操作

初次启动 Docker 容器命令

docker run -d(后台运行容器) -p 8080:8080(绑定端口) xiaopang/centos-nodejs-1

解释:
通过docker run 首先从本地查询是否有这个镜像,如果有直接启动本镜像,如果没有镜像,直接从官方镜像下载镜像,并启动容器

启动已停止的Docker 容器命令

docker start eab4ad610972(容器ID)

查看正在运行的Docker容器

docker ps

查看所有的停止或正在运行的容器

docker ps -a

停止Docker 容器

docker stop eab4ad610972(容器ID)

查看Docker 容器运行的日志

docker logs -f eab4ad610972(容器ID)

删除已停止的Docker 容器

docker rm eab4ad610972(容器ID)

登录容器(类似ssh)

docker exec -it eab4ad610972(容器ID) bash(执行的命令)

挂载带有数据卷的容器

docker run -p 8080:8080 -d --mount source=my-vol,target=/webapp centos-nodejs:1.0
[root@localhost ~]# docker run -p 8080:8080 -d --mount source=my-vol,target=/webapp centos-nodejs:1.0
5f73feb5e99a215255d0b95bce5cb5ef08549c5121af11afce63a4618acadce3

查看容器的挂载信息

 docker inspect 5f73feb5e99a
"Mounts": [
               {
                   "Type": "volume",
                   "Source": "my-vol",
                   "Target": "/webapp"
               }
           ],

删除一个不在使用的数据卷

docker volume rm my-vol
[root@localhost ~]# docker volume rm my-vol
Error response from daemon: remove my-vol: volume is in use - [5f73feb5e99a215255d0b95bce5cb5ef08549c5121af11afce63a4618acadce3]

清理所有没有占用的数据卷

docker volume prune
[root@localhost ~]# docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

Docker 仓库

公有仓库

Docker hub 注册
Docker hub 注册地址:https://hub.docker.com/

搜索镜像

docker search centos

拉取镜像

docker pull centos

登录公有仓库

docker login

推送镜像到共有仓库

  1. 给你的镜像打标签
docker tag centos-nodejs:1.0 xiaohai0407/centos-nodejs:1.0
  1. 查看打好标签的本地镜像
docker image ls
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
centos-nodejs               1.0                 88fbf99d311d        3 minutes ago       343MB
xiaohai0407/centos-nodejs   1.0                 88fbf99d311d        3 minutes ago       343MB
centos                      latest              5182e96772bf        8 days ago          200MB
ubuntu                      latest              735f80812f90        2 weeks ago         83.5MB
  1. 上传镜像到docker 官方镜像库
docker push xiaohai0407/centos-nodejs
[root@localhost first]# docker push xiaohai0407/centos-nodejs
The push refers to repository [docker.io/xiaohai0407/centos-nodejs]
c1f3e6373b76: Pushed 
bdba1884d9f2: Pushed 
a37663980625: Pushed 
2efbd85d8018: Pushed 
1d31b5806ba4: Mounted from library/centos 
1.0: digest: sha256:9c0fef1f79934c6a102437b3e0c6da61e48be61a0ce6f0d11cc157fb1a53704b size: 1362
  1. 查看上传镜像是否成功
[root@localhost first]# docker search xiaohai0407
NAME                        DESCRIPTION                                  STARS               OFFICIAL            AUTOMATED
xiaohai0407/centos-nodejs   base of centos 7 ,create nodejs hello word   0

私有仓库

安装私有仓库

docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
[root@localhost ~]# docker run -d \
>     -p 5000:5000 \
>     -v /opt/data/registry:/var/lib/registry \
>     registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
4064ffdc82fe: Pull complete 
c12c92d1c5a2: Pull complete 
4fbc9b6835cc: Pull complete 
765973b0f65f: Pull complete 
3968771a7c3a: Pull complete 
Digest: sha256:20bbbc0f6384cf7dc6e292ccbe75935b73c92ec776543c970904bc60feceb129
Status: Downloaded newer image for registry:latest
36d7fb0a86534d32eb7f5c4090ebb5db35e801822433fb8138bf94db55477240

将私有仓库的存储路径通过-v 参数设置
到/opt/data/registry路径下

上传镜像到私有仓库

标记镜像到私有仓库

docker tag ubuntu:latest 127.0.0.1:5000/centos-nodejs:1.0

上传镜像到私有仓库

docker push 127.0.0.1:5000/centos-nodejs:1.0
[root@localhost registry]# docker push 127.0.0.1:5000/centos-nodejs:1.0
The push refers to repository [127.0.0.1:5000/centos-nodejs]
268a067217b5: Pushed 
c01d74f99de4: Pushed 
ccd4d61916aa: Pushed 
8f2b771487e9: Pushed 
f49017d4d5ce: Pushed 
1.0: digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 size: 1357

查看仓库中已经上传的镜像

curl 127.0.0.1:5000/v2/_catalog
[root@localhost registry]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos-nodejs"]}

仓库中已经包含了centos-nodejs这个tag的镜像文件

删除本地镜像

docker image rm 127.0.0.1:5000/centos-nodejs:1.0
[root@localhost registry]# docker image ls
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
centos-nodejs               1.0                 88fbf99d311d        24 hours ago        343MB
xiaohai0407/centos-nodejs   1.0                 88fbf99d311d        24 hours ago        343MB
centos                      latest              5182e96772bf        9 days ago          200MB
ubuntu                      latest              735f80812f90        2 weeks ago         83.5MB
registry                    latest              b2b03e9146e1        5 weeks ago         33.3MB
[root@localhost registry]# 

从本地仓库下载镜像

docker pull 127.0.0.1:5000/centos-nodejs:1.0    
[root@localhost registry]# docker pull 127.0.0.1:5000/centos-nodejs:1.0             
1.0: Pulling from centos-nodejs
Digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5
Status: Downloaded newer image for 127.0.0.1:5000/centos-nodejs:1.0
[root@localhost registry]# docker image ls
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos-nodejs                  1.0                 88fbf99d311d        24 hours ago        343MB
xiaohai0407/centos-nodejs      1.0                 88fbf99d311d        24 hours ago        343MB
centos                         latest              5182e96772bf        9 days ago          200MB
127.0.0.1:5000/centos-nodejs   1.0                 735f80812f90        2 weeks ago         83.5MB
ubuntu                         latest              735f80812f90        2 weeks ago         83.5MB
registry                       latest              b2b03e9146e1        5 weeks ago         33.3MB

3.2 Docker基本使用(增删改查)

全文结束