从仓库获取镜像
从docker registry获取镜像的命令是docker pull。命令格式是:
docker pull [选项][docker registry地址] 仓库名:标签
docker register地址:地址的格式一般是 域名:端口,默认地址是docker hub
仓库名:仓库名是两段格式,用户名/软件名,如果不写用户,默认docker hub用户名是library,也就是官方镜像
获取hello-world镜像
[root@localhost opt]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Status: Downloaded newer image for hello-world:latest
查看获取到的镜像
[root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 2 months ago 1.84kB [root@localhost opt]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 2 months ago 1.84kB
运行docker镜像,产生容器实例
[root@localhost ~]# docker run hello-world
通过IMAGE ID 运行
[root@localhost ~]# docker run fce
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/
查看容器的进程
docker容器,必须有后台程序在运行,否则容器就退出
查看正在运行的容器进程 [root@localhost ~]# docker ps [root@localhost ~]# docker container ls 查看所有容器进程 [root@localhost ~]# docker ps -a [root@localhost ~]# docker container ls -a
搜索查找docker镜像
[root@localhost ~]# docker search django
交互式运行容器
[root@localhost ~]# docker run -it centos /bin/bash [root@11913aafe44a /]# 退出交互模式删除容器记录 [root@localhost ~]# docker run -it --rmcentos /bin/bash [root@11913aafe44a /]# [root@localhost ~]# docker run -it 镜像ID /bin/bash [root@11913aafe44a /]#
删除容器记录
删除单个容器记录 [root@localhost ~]# docker rm 容器ID 批量删除容器记录,反引号,只能删除挂掉的容器记录 [root@localhost ~]# docker rm `docker ps -aq` 强制删除所有容器记录 [root@localhost ~]# docker rm -f`docker ps -aq`
删除镜像记录
删除单个镜像文件 [root@localhost ~]# dokcer rmi 镜像ID 批量删除镜像文件 [root@localhost ~]# dokcer rmi `docker images -aq`
导出docker镜像
[root@localhost ~]# docker save centos > /opt/centos.tar.gz
导入docker镜像
[root@localhost ~]# docker load centos < /opt/centos.tar.gz
提交自定义镜像
[root@localhost ~]# docker commit 11913aafe44a wanglan/centos-vim sha256:20d02edad8f31de9608e37f089d74971b2d98b217024f94e3f0321f36b3d7a35 11913aafe44a :容器ID wanglan:docker hub地址,写上可以提交到docker hub,也可以不写
为docker镜像改名
[root@localhost ~]# docker tag 镜像ID 镜像新名字
为容器记录命名
为容器命名为redis [root@localhost ~]# docker run -it --name myredis 1e1 /bin/bash [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9577b1624526 1e1 "/bin/bash" 13 seconds ago Exited (0) 10 seconds ago myredis 552ef020b7e4 ubuntu "/bin/bash" 2 minutes ago Exited (0) About a minute ago mysql
后台启动docker
[root@localhost ~]# docker run -d centos /bin/bash #后台不间断运行一个 shell语句 docker run -d centos /bin/sh -c "while true;do echo hello world; sleep 1;done" -d 后台运行 centos 指定镜像文件 /bin/sh 指定shell解释器 -c 指定一个shell语句
查看容器内的标准输出
[root@localhost ~]# docker logs 容器ID 实时查看输出 [root@localhost ~]# [root@localhost ~]# docker logs -f 容器ID
启动/停止容器
启动 [root@localhost ~]# docker start 容器ID 关闭 [root@localhost ~]# docker stop 容器ID
进入正在运行的容器
[root@localhost ~]# docker exec -it 容器ID /bin/bash
docker端口映射
容器中可以运行网络应用,但是要让外部也可以访问这些应用,可以通过-p或-P参数指定端口映射
-P 参数会随机映射端口到容器开放的网络端口,大写的P [root@localhost ~]# docker run -d -P training/webapp python app.py -p参数指定映射端口,小写p 指定服务器的9000端口,映射到容器内的5000端口 [root@localhost ~]# docker run -d -p 9000:5000 training/webapp python app.py 查看端口映射 [root@localhost ~]# docker port 容器ID
查看容器内的进程
[root@localhost ~]# docker top 容器ID
容器挂掉之后数据不会丢失,可以通过docker ps -a 找到后在运行,要删除镜像必须先删除由他创建的容器记录