[TOC]
<br>
## 容器介紹
容器是Docker的另一個核心組件。
簡單的說,容器是鏡像的一個運行實例。如果認為虛擬機是模擬運行的一整套操作系統(提供了運行態環境和其他系統環境)和跑在上面的應用。那么Docker容器就是獨立運行的一個或一組應用,以及它們的必需運行環境。
<br>
## 創建容器
通過鏡像,創建容器,命令格式: `docker create [OPTIONS] IMAGE [COMMAND] [ARG...] `
**關鍵Options**
--name string Assign a name to the container
-p, --publish list Publish a container's port(s) to the host
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
### 案例:創建 redis 的容器
```
root@ubuntu:/home/guanfuchang# docker create -p 16379:6379 --name redis redis:5.0
07bf73ec9f73d6d7a2c4bad79813bf1ae63b853e2b3a22a92d58d7ecfe24ca2e
```
創建成功后,會返回容器ID
>[info]Tips:命令中的端口 `16379:6379`,冒號前面的端口16379是虛擬機的端口,6379是容器內的端口,通過該設置,是將操作系統的16379映射到容器內的6379,我們后面使用redis客戶端連接redis時,連接的是16379哦。
<br>
## 查看容器列表
查看正在運行的容器列表,命令 `docker ps`
查看所有的本地容器,命令 `docker ps -a`
```
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 4 minutes ago Created redis
```
<br>
## 啟動容器
啟動容器,命令格式:`docker start 容器名或容器ID`,其中容器的id,只需要輸入前幾位即可。
### 案例:啟動redis容器
```
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 4 minutes ago Created redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker start 07bf73ec9f73
07bf73ec9f73
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 8 minutes ago Up 42 seconds 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#
```
通過上面創建后,主機的16379端口上已經運行了一個redis服務。
### 案例:通過redis客戶端進行測試
Redis可視化管理工具(Redis Desktop Manager])
鏈接:https://pan.baidu.com/s/1sOiOm7bEALZKA0-GpkZ3_Q 密碼:ruxk
下載安裝后,連接redis服務器,配置如下:

連接成功如下:

## 創建并運行容器
上面通過docker create 創建了容器,然后通過docker start 來啟動容器。
由于創建容器并且啟動容器的操作非常頻繁,docker client 提供了更加便捷的命令 `docker run` 一步創建并且啟動容器。
命令格式:`docker run [OPTIONS] IMAGE [COMMAND] [ARG...]`
### 案例:創建并運行一個redis容器
```
root@ubuntu:/home/guanfuchang# docker run -p 16380:6379 --name redis2 redis:5.0
1:C 15 Nov 2018 07:01:49.153 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Nov 2018 07:01:49.154 # Redis version=5.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Nov 2018 07:01:49.154 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 15 Nov 2018 07:01:49.155 * Running mode=standalone, port=6379.
1:M 15 Nov 2018 07:01:49.155 # 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 15 Nov 2018 07:01:49.156 # Server initialized
1:M 15 Nov 2018 07:01:49.156 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 15 Nov 2018 07:01:49.156 # 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 15 Nov 2018 07:01:49.156 * Ready to accept connections
```

上面例子,啟動redis容器后,一直在前臺運行,如果想要讓容器后臺運行,加入參數-d,如:
```
root@ubuntu:/home/guanfuchang# docker run -p 16380:6379 -d --name redis3 redis:5.0
e4a4aae7860f9f5a2bca62cb806e33ef356939708f9655c95af122565d0db3c2
```
## 停止容器
停止容器有2種方式
* `docker stop 容器名或容器id`
* `docker kill 容器名或容器id`
### 案例:停止容器redis3
```
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a4aae7860f redis:5.0 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:16380->6379/tcp redis3
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang# docker stop redis3
redis3
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
```
## 刪除容器
刪除容器,命令格式:`docker rm [OPTIONS] CONTAINER [CONTAINER...]`
刪除正在運行的容器,添加`-f`參數
### 案例:刪除容器redis3
```
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a4aae7860f redis:5.0 "docker-entrypoint.s…" 7 minutes ago Exited (0) 3 minutes ago redis3
4c371862865e redis:5.0 "docker-entrypoint.s…" 12 minutes ago Exited (0) 10 minutes ago redis2
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker rm redis3
redis3
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c371862865e redis:5.0 "docker-entrypoint.s…" 12 minutes ago Exited (0) 10 minutes ago redis2
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
```
## 進入容器
有些時候,我們需要進入容器內,做一些操作,比如修改配置文件等
(**不推薦修改容器,后面會介紹如何掛載外部文件**)
進入容器,命令格式:`docker exec [OPTIONS] CONTAINER COMMAND [ARG...]`
### 案例:進入容器redis
```
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker exec -it redis /bin/bash
root@07bf73ec9f73:/data#
root@07bf73ec9f73:/data# cd /
root@07bf73ec9f73:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@07bf73ec9f73:/# ls /usr/local/bin/
docker-entrypoint.sh gosu redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
```
通過上面例子,我們可以得知,容器里面,包含了一個小型的linux系統,在linux系統上安裝了redis服務。
>[info] control+d 退出容器
## 查看日志
命令格式: `docker logs [OPTIONS] CONTAINER`
### 案例:查看redis容器日志
```
root@ubuntu:/home/guanfuchang# docker logs -f redis
1:C 15 Nov 2018 06:21:27.687 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Nov 2018 06:21:27.689 # Redis version=5.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Nov 2018 06:21:27.689 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 15 Nov 2018 06:21:27.690 * Running mode=standalone, port=6379.
1:M 15 Nov 2018 06:21:27.690 # 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 15 Nov 2018 06:21:27.690 # Server initialized
1:M 15 Nov 2018 06:21:27.690 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 15 Nov 2018 06:21:27.690 # 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 15 Nov 2018 06:21:27.690 * Ready to accept connections
```
---
:-: 
<span style="color: #993366;"><em>***<span style="text-decoration: underline;"><span style="text-decoration: underline;">微信掃一掃,關注“python測試開發圈”,了解更多測試教程!!</span></span>***</em></span>