Docker实战
2020-01-02 本文已影响0人
phpnet
Docker实战
前言
Docker 是一个开源的、C/S 架构的应用容器引擎,是 PaaS 提供商 dotCloud 基于 libContainer 推出的,它基于 Go 语言并遵从 Apache 2.0 开源协议。
应用场景
加速本地开发。通过Docker能够快速搭建好开发和运行环境,并且该环境可以直接传递给测试和产品部署。
- 自动打包和部署应用。
- 创建轻量、私有的PaaS环境。
- 自动化测试和持续集成/部署。
- 部署并扩展Web应用、数据库和后端服务器。
- 创建安全沙盒。
- 轻量级的桌面虚拟化。
镜像加速
Docker 官方和国内很多云服务商都提供了国内加速器服务:
- Docker官方提供的中国镜像库:https://registry.docker-cn.com
- 七牛云加速器:https://reg-mirror.qiniu.com
- 网易加速器:http://hub-mirror.c.163.com
说明:阿里云也提供有镜像,不过没有公开,需要登录阿里云账号获取,https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors。
$ vim /etc/docker/daemon.json
{"registry-mirrors":["https://registry.docker-cn.com"]}
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
相关命令
# 查看所有命令
$ docker
# 获取镜像
$ docker pull ubuntu
# 创建容器(交互式)
$ docker run -it --name ubuntu ubuntu /bin/bash
-i: 交互式操作
-t: 终端
-d:后台运行(后台式)
# 查看容器
$ docker ps -a
-a:查看所有容器,包含未启动的容器
-l:查看最后创建的容器
-n:最后创建的 n 个容器
# 启动容器
$ docker start id或者name
# 停止容器
$ docker stop id或者name
# 进入容器(在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过 attach 或 exec 命令)
# docker exec 命令,因为此退出容器终端,不会导致容器的停止。
$ docker exec -it id或者name /bin/bash
# 导出容器
$ docker export 1e560fca3906 > ubuntu.tar
# 导入容器
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
# 删除容器
$ docker rm -f id或name
创建容器有两个命令,一个是docker create,另一个是docker run。二者的区别在于前者创建的容器处于停止状态,而后者不仅创建了容器,而且启动了容器。
更多命令,可以通过命令 docker command --help 更深入的了解。
所有命令
$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/Users/Jorly/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/Users/Jorly/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/Users/Jorly/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/Users/Jorly/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.