docker安装与启动(1)

-v, --volume=[] Bind mount a volume

–volume-driver Optional volume driver for the container

–volumes-from=[] Mount volumes from the specified container(s)

-w, --workdir Working directory inside the container

启动并创建一个交互式的docker容器:

[root@localhost /]# docker run -ti -d 686672a1d0cc

//-d为后台启动

通过docker ps 来查看当前运行的容器,看下docker ps的相关指令:

[root@localhost /]# docker ps --help

Usage: docker ps [OPTIONS]

List containers

-a, --all Show all containers (default shows just running)

-f, --filter=[] Filter output based on conditions provided

–format Pretty-print containers using a Go template

–help Print usage

-l, --latest Show the latest created container (includes all states)

-n=-1 Show n last created containers (includes all states)

–no-trunc Don’t truncate output

-q, --quiet Only display numeric IDs

-s, --size Display total file sizes

[root@localhost /]# docker ps//查看当前正在运行的容器

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7d95b3df8674 686672a1d0cc “/bin/bash” 5 hours ago Up About an hour admiring_kowalevski

[root@localhost /]# docker ps -a//查看所有的容器信息

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7d95b3df8674 686672a1d0cc “/bin/bash” 5 hours ago Up About an hour admiring_kowalevski

1b4ac575eda0 686672a1d0cc “/bin/bash” 23 hours ago Exited (137) About an hour ago elated_turing

通过docker start,docker stop来启动和停止容器:

[root@localhost /]# docker start --help

Usage: docker start [OPTIONS] CONTAINER [CONTAINER…]

Start one or more stopped containers

-a, --attach Attach STDOUT/STDERR and forward signals

–detach-keys Override the key sequence for detaching a container

–help Print usage

-i, --interactive Attach container’s STDIN

[root@localhost /]# docker stop --help

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER…]

Stop a running container.

Sending SIGTERM and then SIGKILL after a grace period

–help Print usage

-t, --time=10 Seconds to wait for stop before killing it

[root@localhost /]# docker restart --help

Usage: docker restart [OPTIONS] CONTAINER [CONTAINER…]

Restart a container

–help Print usage

-t, --time=10 Seconds to wait for stop before killing the container

使用docker exec 可以进入到已经启动的容器中,低版本的docker可能不行。

[root@localhost Desktop]# docker exec -ti 7d95b3df8674 /bin/bash

[root@7d95b3df8674 /]#

容器的工作是建立在镜像的基础之上的,如果需要删除镜像的话,需要先删除使用该镜像的容器,然后才能删除镜像,否则删除镜像的时候,会有如下的类似的错误信息提示:Failed to remove image

(e7b): Error response from daemon: conflict: unable to delete e7b2de517efa (must be forced) - image is being used by stopped container 4fbc3cd00987,可以通过docker rm删除容器,docker rmi 删除镜像。

[root@localhost /]# docker rm --help

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER…]

Remove one or more containers

-f, --force Force the removal of a running container (uses SIGKILL)

–help Print usage

-l, --link Remove the specified link

-v, --volumes Remove the volumes associated with the container

[root@localhost /]# docker rmi --help

Usage: docker rmi [OPTIONS] IMAGE [IMAGE…]

Remove one or more images

-f, --force Force removal of the image

–help Print usage

–no-prune Do not delete untagged parents

删除停止的容器

docker rm $(docker ps --all -q -f status=exited)

删除没有使用的镜像

docker rmi -f $(docker images | grep “” | awk “{print $3}”)

批量删除容器

docker ps -a | awk ‘{print $1}’ | xargs docker rm

批量删除镜像

docker images | awk ‘{print $3}’ | xargs docker rmi

持久化容器与镜像

1.通过容器生成新的镜像

运行中的镜像称为容器。你可以修改容器(比如删除一个文件),但这些修改不会影响到镜像。不过,你使用docker commit 命令可以把一个正在运行的容器变成一个新的镜像。

docker commit [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选。

2.持久化容器

docker export用于持久化容器。

[root@localhost /]# docker export --help

Usage: docker export [OPTIONS] CONTAINER

Export a container’s filesystem as a tar archive

–help Print usage

-o, --output Write to a file, instead of STDOUT

[root@localhost /]# docker export container.tar

3.持久化镜像

======================================================================

docker save用于持久化镜像:

[root@localhost /]# docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE…]

Save an image(s) to a tar archive (streamed to STDOUT by default)

–help Print usage

-o, --output Write to a file, instead of STDOUT

[root@localhost /]# docker save image.tar

4.导入持久化镜像,容器:

============================================================================

使用docker import,docker load导入镜像容器:

[root@localhost /]# docker import --help

Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

-c, --change=[] Apply Dockerfile instruction to the created image

–help Print usage

-m, --message Set commit message for imported image

[root@localhost /]# docker load --help

Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

–help Print usage

-i, --input Read from a tar archive file, instead of STDIN

5.对镜像打tag

[root@localhost /]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB

[root@localhost /]# docker tag 686672a1d0cc centos:base

[root@localhost /]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos base 686672a1d0cc 5 weeks ago 194.6 MB

docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB

6.export-import与save-load的区别

===========================================================================================

导出后再导入(export-import)的镜像会丢失所有的历史,而保存后再加载(save-load)的镜像没有丢失历史和层(layer)。这意味着使用导出后再导入的方式,你将无法回滚到之前的层(layer),同时,使用保存后再加载的方式持久化整个镜像,就可以做到层回滚。(可以执行docker tag 来回滚之前的层)。

docker logs $CONTAINER_ID #查看docker实例运行日志,确保正常运行

docker inspect $CONTAINER_ID #docker inspect <image|container> 查看image或container的底层信息

docker build 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image

docker build -t repo[:tag] 同上,可以指定repo和可选的tag

docker build - < 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image

docker port 查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到。

7.docker文件存放目录

=============================================================================

Docker实际上把所有东西都放到/var/lib/docker路径下了。

至此一个简单干净的docker环境搭建完毕。

docker images //查看本地镜像

docker ps -a //查看所有容器

docker ps //查看当前有哪些容器正在运行

docker rmi 镜像名称/镜像ID 删除镜像

docker rm 容器名称/容器ID 删除容器(删除前必须先停止容器的运行)

docker start 容器名称/容器ID 启动一个容器

docker restart 容器名称/容器ID 重启一个容器

docker stop 容器名称/容器ID 停止一个在运行的容器

docker run -d -p 8081:8080 --name tomcat01 tomcat 利用镜像创建一个容器

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值