docker

一 安装 docker

yum install docker -y

二 docker操作

获取docker镜像

[root@localhost ~]# docker pull centos

Using default tag: latest Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 8a29a15cefae: Pull complete Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700 Status: Downloaded newer image for docker.io/centos:latest

查看

[root@localhost ~]# docker images

仓库          标记          镜像ID          创建时间       大小 REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 470671670cac 2 months ago 237 MB

# 注意:
镜像的ID唯一标识了镜像,如果ID相同,说明是同一镜像。

TAG信息来区分不同发行版本,如果不指定具体标记,默认使用latest标记信息。


# 查询容器内的运行日志
docker logs 容器id
docker logs -f 容器id # 实时刷新容器内的日志,例如检测nginx等日志信息
# 进入一个容器
docker exec -it 153 /bin/bash
# 查看容器里的进程
ps -ef

 

删除

如果要移除本地的镜像,可以使用docker rmi命令(在删除镜像之前先用docker rm删除依赖于这个镜像的所有容器)。注意docker rm 命令是移除容器。

[root@docker ~]# docker rmi imageID  #删除docker镜像

导出docker镜像

如果要导出镜像到本地文件,可以使用docker save命令。

[root@docker ~]# docker save centos > /opt/centos.tar.gz  #导出docker镜像至本地

[root@docker ~]# ll /opt/



-----------------------------------------------------------------------------


可以使用docker load从本地文件中导入到本地docker镜像库

[root@docker ~]# docker load < /opt/centos.tar.gz   #导入本地镜像到docker镜像库

[root@docker~]# docker images  #查看镜像导入情况

启动docker容器的方式

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。

因为Docker的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器

新建容器并且启动

我们需要的命令是 docker run

# 例子
# 先运行一下centos

[root@localhost ~]# docker run centos

# 查看centos进行 你会发现啥也没有啊 ------原因是 centos 下没有任何任务 容器起来一下就挂掉了 
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

# 用下面的命令 查看正在运行和已经挂掉的容器
[root@localhost ~]# docker ps -a
CONTAINER ID     IMAGE            COMMAND               CREATED STATUS       PORTS    NAMES
6923068885ab centos "/bin/bash" 19 seconds ago Exited (0) 12 seconds ago              dreamy_kare

[root@localhost ~]# docker run --name '一个名字' -it centos /bin/bash#启动一个bash终端,允许用户进行交互。

 

注意:docker run 在运行一个容器的时候 如果没有这个容器 docker run 可以给我下载

[root@localhost ~]# docker run hello-world

Unable to find image 'hello-world:latest' locally Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world 1b930d010525: Pull complete Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e Status: Downloaded newer image for docker.io/hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/

 

posted @ 2019-12-29 13:56  流年中渲染了微笑  阅读(278)  评论(0编辑  收藏  举报