docker镜像相关命令

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/suchenbin/article/details/102621972

简单介绍docker镜像相关的命令

可以使用--help查看详细的命令,比如

[root@*** ~]# docker login --help

Usage:	docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username

docker login -u 用户 -p 密码 镜像仓库地址:登录docker仓库,不加仓库地址,默认是docker官方仓库;

docker logout:退出登录;

[root@*** ~]# docker login -u XXX registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@*** ~]# cat /root/.docker/config.json
{
	"auths": {
		"registry.cn-hangzhou.aliyuncs.com": {
			"auth": "**********************"
		}
	},
	"HttpHeaders": {
		"User-Agent": "Docker-Client/19.03.3 (linux)"
	}
}
[root@*** ~]# docker logout
Not logged in to https://index.docker.io/v1/

docker search xxx :从docker仓库查找xxx的镜像;

[root@*** ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8699                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3040                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   643                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   63                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   53                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      36                                      [OK]
.
.
.

docker pull 镜像名称:镜像版本:拉取镜像,不加上版本则默认获取最新版本的镜像;

docker push 镜像名称:镜像版本:登录了镜像仓库的前提下使用,将本地镜像上传到镜像仓库;

[root@*** ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
80369df48736: Pull complete 
.
.
.
d482603a2922: Pull complete 
Digest: sha256:44b33224e3c406bf50b5a2ee4286ed0d7f2c5aec1f7fdb70291f7f7c570284dd
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
.
.
.

Login Succeeded
[root@*** ~]# docker push mysql:5.7
The push refers to repository [docker.io/library/mysql]
ccbee7d06322: Layer already exists 
392376ec36d5: Layer already exists 
05ef222f4cd4: Layer already exists 
.
.
.

docker images:查找出本地镜像;

[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 cd3ed0dfff7e        26 hours ago        437MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB

docker rmi :删除本地镜像,可以使用使用IMAGE ID或者镜像仓库:版本号作为参数, -f:强制删除;

[root@*** ~]# docker rmi -f  fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 cd3ed0dfff7e        27 hours ago        437MB

docker tag 源仓库:版本 新仓库:版本:将本地镜像复制,设置版本号,存放到指定仓库;

[root@*** ~]# docker tag hello-world:latest hello:test
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 cd3ed0dfff7e        27 hours ago        437MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
hello               test                fce289e99eb9        9 months ago        1.84kB

  docker history:查看镜像的创建历史;

[root@*** ~]# docker history hello:test
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
fce289e99eb9        9 months ago        /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
<missing>           9 months ago        /bin/sh -c #(nop) COPY file:f77490f70ce51da2…   1.84kB              

docker build:使用 Dockerfile创建镜像,首先需要有一个Dockerfile文件,这里写一个最简单的hello world来演示下;

[root@*** ~]# touch Dockerfile
[root@*** ~]# ls
anaconda-ks.cfg  conf  data  Dockerfile  logs
[root@*** ~]# cat Dockerfile 
FROM centos

#CMD echo "hello world"
CMD ["/bin/bash","-c","echo 'hello world!'"]
[root@*** ~]# docker build -t test:1111 .
Sending build context to Docker daemon  207.2MB
Step 1/2 : FROM centos
latest: Pulling from library/centos
729ec3a6ada3: Pull complete 
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
 ---> 0f3e07c0138f
Step 2/2 : CMD ["/bin/bash","-c","echo 'hello world!'"]
 ---> Running in 5a2239155e17
Removing intermediate container 5a2239155e17
 ---> 012db2fe7991
Successfully built 012db2fe7991
Successfully tagged test:1111
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                1111                012db2fe7991        14 seconds ago      220MB
mysql               5.7                 cd3ed0dfff7e        28 hours ago        437MB
centos              latest              0f3e07c0138f        2 weeks ago         220MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
hello               test                fce289e99eb9        9 months ago        1.84kB

docker save -o xxx.tar  xxx:xx:将xxx:xx镜像保存成 xxx.tar;

[root@*** ~]# docker save -o hello.tar  hello:test
[root@*** ~]# ls
anaconda-ks.cfg  conf  data  hello.tar  logs

docker load -f xxx.tar:加载保存成xxx.tar文件的镜像;

[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                1111                012db2fe7991        12 minutes ago      220MB
mysql               5.7                 cd3ed0dfff7e        28 hours ago        437MB
centos              latest              0f3e07c0138f        2 weeks ago         220MB
hello               test                fce289e99eb9        9 months ago        1.84kB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
[root@*** ~]# docker rmi hello:test
Untagged: hello:test
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                1111                012db2fe7991        13 minutes ago      220MB
mysql               5.7                 cd3ed0dfff7e        28 hours ago        437MB
centos              latest              0f3e07c0138f        2 weeks ago         220MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
[root@*** ~]# docker load -i hello.tar
Loaded image: hello:test
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                1111                012db2fe7991        13 minutes ago      220MB
mysql               5.7                 cd3ed0dfff7e        28 hours ago        437MB
centos              latest              0f3e07c0138f        2 weeks ago         220MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
hello               test                fce289e99eb9        9 months ago        1.84kB

docker import xxx.tar  xxx:xx:通过xxx.tar文件创建xxx:xx镜像;

[root@*** ~]# docker import hello.tar  hello2:v1
sha256:6e3907ca9bd5c3656c2107d2e65fbbd389a6d0b54d9a92c37abcd975d6991a56
[root@*** ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello2              v1                  6e3907ca9bd5        9 seconds ago       6.56kB
test                1111                012db2fe7991        19 minutes ago      220MB
mysql               5.7                 cd3ed0dfff7e        28 hours ago        437MB
centos              latest              0f3e07c0138f        2 weeks ago         220MB
hello-world         latest              fce289e99eb9        9 months ago        1.84kB
hello               test                fce289e99eb9        9 months ago        1.84kB

如果有写的不对的地方,请大家多多批评指正,非常感谢!

猜你喜欢

转载自blog.csdn.net/suchenbin/article/details/102621972