Docker是容器的一种,容器是一种轻量级的虚拟技术。与容器不同,虚拟机是一种重量级的虚拟技术。Docker具有:
跨平台的可移植性
面向应用
版本控制
组件复用
共享性
工具生态系统
Docker作为一款容器,并且比传统的容器具有更多的改进和优点。以下是Docker的几种典型应用场景:
- 加速本地开发。通过Docker能够快速搭建好开发和运行环境,并且该环境可以直接传递给测试和产品部署。
- 自动打包和部署应用。
- 创建轻量、私有的PaaS环境。
- 自动化测试和持续集成/部署。
- 部署并扩展Web应用、数据库和后端服务器。
- 创建安全沙盒。
- 轻量级的桌面虚拟化。
以下介绍几个Docker中常用的命令。
0x01 获取镜像
> docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6abc03819f3e: Pulling fs layer
05731e63f211: Pulling fs layer
0bd67c50d6be: Download complete
0x02 查看镜像
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 055936d39205 4 weeks ago 5.53MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
python 2.7 3c01ed1c16af 4 weeks ago 914MB
python 3.7 a4cc999cf2aa 4 weeks ago 929MB
redis latest a4fe14ff1981 4 weeks ago 95MB
nginx latest 53f3fd8007f7 4 weeks ago 109MB
mongo latest d98005b752b4 4 weeks ago 411MB
mysql latest d72169616e20 6 weeks ago 443MB
0x03 创建并启动容器
> docker run --name mysql-server -v /data/tmp/mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=53f3fd8007f7 -p 3392:3306 -d mysql
c22162c0ae39284591a4a0b1e4e724fb05c2455abd96f2862a12a3adbe293c8c
以MySQL为例,对上述几个参数进行解释:
--name mysql-server
创建一个名称为mysql-server
的容器。-v /data/tmp/mysql-data:/var/lib/mysql
对容器中的文件目录进行映射,将宿主机中的/data/tmp/mysql-data
目录同容器中的/var/lib/mysql
进行映射。-e MYSQL_ROOT_PASSWORD=53f3fd8007f7
传入参数-p 3392:3306
将宿主机的3392
端口同容器中的3306
进行映射。-d
容器后台运行mysql
运行容器的镜像,如果后面没有指明版本号则为latest
. 如果需要指明版本号可以通过:
进行指明,如:mysql:5.7
0x04 查看容器
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c22162c0ae39 mysql "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 33060/tcp, 0.0.0.0:3392->3306/tcp mysql-server
docker ps
命令只查看正在运行的容器。
> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c22162c0ae39 mysql "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 33060/tcp, 0.0.0.0:3392->3306/tcp mysql-server
0908250ff8d7 osminogin/tor-simple "/usr/bin/tor" 2 weeks ago Exited (0) 11 hours ago tor
53149e4d6a72 mongo "docker-entrypoint.s…" 2 weeks ago Exited (0) 2 weeks ago mongo-db
ada3bd8fc57b ssr "python local.py -c …" 3 weeks ago Exited (137) 11 hours ago ssr
b506117c93e6 python:3.7 "bash" 3 weeks ago Exited (0) 2 weeks ago python3
cb2722de812e redis "docker-entrypoint.s…" 3 weeks ago Exited (0) 2 weeks ago redis-server
docker ps -a
命令则可查看所有容器。
0x05 删除镜像
> docker rmi alpine
Untagged: alpine:latest
Untagged: alpine@sha256:769fddc7cc2f0a1c35abb2f91432e8beecf83916c421420e6a6da9f8975464b6
0x06 启动、停止、重启容器
启动容器
> docker start mysql-server mysql-server
停止容器
> docker stop mysql-server mysql-server
重启容器
> docker restart mysql-server mysql-server
0x07 删除容器
> docker rm mysql-server
mysql-server
0x08 查看容器信息
> docker inspect redis-server
[
{
"Id": "cb2722de812e5491da0aad0d442940e344ac4e9712274fc0eae4bd8c2ff1e0d6",
"Created": "2019-05-14T13:32:48.1947863Z",
"Path": "docker-entrypoint.sh",
"Args": [
"redis-server"
],
...
...
...
,
"Image": "sha256:a4fe14ff19812e816e1b21f7daa5fa25ab9bff4365252fe38d8eef4bdf1150ee",
...
...
...
"IPAddress": "172.17.0.2",
...
"Networks": {
...
"IPAddress": "172.17.0.2",
....
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
docker inspect
用于查看容器的配置信息,包含容器名、环境变量、运行命令、主机配置、网络配置和数据卷配置等。
0x09 查看容器进程
> docker top redis-server
PID USER TIME COMMAND
3498 999 0:00 redis-server *:6379
0x0a 查看容器日志
> docker logs -f --tail=5 redis-server
1:M 10 Jun 2019 13:53:25.985 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 10 Jun 2019 13:53:25.985 # Server initialized
1:M 10 Jun 2019 13:53:25.986 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 10 Jun 2019 13:53:25.986 * DB loaded from disk: 0.001 seconds
1:M 10 Jun 2019 13:53:25.987 * Ready to accept connections
-f
使用此标志,之后的日志也会不断更新出来。
--tail=5
首先输出日志的最后5行。
0x0b 容器内执行命令
> docker exec -it redis-server echo hello
hello
通过这种方式可以进入容器内的命令行。
> docker exec -it redis-server bash
root@cb2722de812e:/data#
0x0c 容器的导入与导出
导出容器
> docker export redis-server > redis-server.tar
导入容器
> docker import redis-server.tar redis-server sha256:67b3ac0c90bbd9d5fd901331142abc544bef5f0f1ea345787353dda9a42bc4cc
0x0d 使用Dockerfile创建镜像
> docker build -t naive-honix .
Sending build context to Docker daemon 64kB
Step 1/10 : FROM python:3.7
---> a4cc999cf2aa
Step 2/10 : MAINTAINER ismdeep@protonmail.com
---> Using cache
---> eb65fc3ae7a2
Step 3/10 : RUN mkdir /root/.pip
---> Using cache
---> 9e25a8b6fa4f
Step 4/10 : COPY ./pip.conf /root/.pip
---> Using cache
---> 5aa11374cc97
Step 5/10 : COPY ./honix /opt/honix
---> Using cache
---> 3b3addd353d1
Step 6/10 : COPY ./docker-entrypoint.sh /
---> Using cache
---> 21dda863be59
Step 7/10 : RUN chmod +x /docker-entrypoint.sh
---> Using cache
---> af83ea0eadee
Step 8/10 : RUN pip install ismdeep-utils
---> Using cache
---> 25fe683853d1
Step 9/10 : EXPOSE 80
---> Using cache
---> 4dc7970daa32
Step 10/10 : ENTRYPOINT ["/docker-entrypoint.sh"]
---> Using cache
---> 9a6fea0eaeaa
Successfully built 9a6fea0eaeaa
Successfully tagged naive-honix:latest