CentOS 7 Docker的安装与使用

作者: 陈俊飞 分类: Linux 发布时间: 2017-07-26 16:44

yum -y install docker-io                      # 仅此一条命令就可以搞定;

配置 Docker 加速器

Docker 版本在 1.12 或更高

创建或修改 /etc/docker/daemon.json 文件,修改为如下形式 (请将 加速地址 替换为在加速器页面获取的专属地址)

{“registry-mirrors”: [“https://xyrif030.mirror.aliyuncs.com”,”http://8c3d2dea.m.daocoud.io”]}

sudo systemctl daemon-reload
sudo systemctl restart docker

service docker start                          # 启动docker

chkconfig docker on                            # 加入开机启动

docker pull centos:latest                      #从docker.io中下载centos镜像到本地 /var/lib/docker/graph

docker images                                  #查看已下载的镜像

docker run -i -t centos /bin/bash              #启动一个容器

我们可以看到,CentOS 容器已经被启动,并且我们得到了 bash 提示符。在 docker 命令中我们使用了 “-i 捕获标准输入输出”和 “-t 分配一个终端或控制台”选项。若要断开与容器的连接,输入 exit。

  基本信息查看

    docker version:查看docker的版本号,包括客户端、服务端、依赖的Go等
[root@localhost /]# docker version
Client version: 1.0.0
Client API version: 1.12
Go version (client): go1.2.2
Git commit (client): 63fe64c/1.0.0
Server version: 1.0.0
Server API version: 1.12
Go version (server): go1.2.2
Git commit (server): 63fe64c/1.0.0

    docker info :查看系统(docker)层面信息,包括管理的images, containers数等
[root@localhost /]# docker info
Containers: 16
Images: 40
Storage Driver: devicemapper
Pool Name: docker-253:0-1183580-pool
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 2180.4 Mb
Data Space Total: 102400.0 Mb
Metadata Space Used: 3.4 Mb
Metadata Space Total: 2048.0 Mb
Execution Driver: lxc-0.9.0
Kernel Version: 2.6.32-431.el6.x86_64

 

5 镜像的获取与容器的使用

镜像可以看作是包含有某些软件的容器系统,比如ubuntu就是一个官方的基础镜像,很多镜像都是基于这个镜像“衍生”,该镜像包含基本的ubuntu系统。再比如,hipache是一个官方的镜像容器,运行后可以支持http和websocket的代理服务,而这个镜像本身又基于ubuntu。

    搜索镜像
docker search <image>:在docker index中搜索image
[root@localhost /]# docker search ubuntu12.10
NAME                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mirolin/ubuntu12.10                                                         0
marcgibbons/ubuntu12.10                                                     0
mirolin/ubuntu12.10_redis                                                   0
chug/ubuntu12.10×32         Ubuntu Quantal Quetzal 12.10 32bit  base i…   0
chug/ubuntu12.10×64         Ubuntu Quantal Quetzal 12.10 64bit  base i…   0

    下载镜像

docker pull <image> :从docker registry server 中下拉image
[root@localhost /]# docker pull chug/ubuntu12.10×64

    查看镜像

docker images: 列出images

docker images -a :列出所有的images(包含历史)

docker images –tree :显示镜像的所有层(layer)

docker rmi  <image ID>: 删除一个或多个image

 

 使用镜像创建容器
[root@localhost /]# docker run chug/ubuntu12.10×64  /bin/echo hello world
hello world

交互式运行
[root@localhost /]# docker run -i -t chug/ubuntu12.10×64  /bin/bash
root@2161509ff65e:/#

    查看容器

docker ps :列出当前所有正在运行的container
docker ps -l :列出最近一次启动的container
docker ps -a :列出所有的container(包含历史,即运行过的container)
docker ps -q :列出最近一次运行的container ID

 

  再次启动容器

docker start/stop/restart <container> :开启/停止/重启container
docker start [container_id] :再次运行某个container (包括历史container)
docker attach [container_id] :连接一个正在运行的container实例(即实例必须为start状态,可以多个窗口同时attach 一个container实例)
docker start -i <container> :启动一个container并进入交互模式(相当于先start,在attach)

ctrl+d 退出容器且关闭, docker ps 查看无

ctrl+p+q 退出容器但不关闭, docker ps 查看有

docker run -i -t <image> /bin/bash :使用image创建container并进入交互模式, login shell是/bin/bash
docker run -i -t -p <host_port:contain_port> :映射 HOST 端口到容器,方便外部访问容器内服务,host_port 可以省略,省略表示把 container_port 映射到一个动态端口。
注:使用start是启动已经创建过得container,使用run则通过image开启一个新的container。

    删除容器

docker rm <container…> :删除一个或多个container
docker rm `docker ps -a -q` :删除所有的container
docker ps -a -q | xargs docker rm :同上, 删除所有的container

Docker attach

Docker attach可以attach到一个已经运行的容器的stdin,然后进行命令执行的动作。
但是需要注意的是,如果从这个stdin中exit,会导致容器的停止。


[root@localhost temp]# docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED              STATUS              PORTS               NAMES
2327e7eab0ed        busybox:buildroot-2014.02   "/bin/sh"           About a minute ago   Up About a minute                       bb2
[root@localhost temp]# docker attach bb2
/ # ls
bin      dev      etc      home     lib      lib64    linuxrc  media    mnt      opt      proc     root     run      sbin     sys      tmp      usr      var
/ # pwd
/
/ #

Docker exec

关于-i、-t参数

可以看出只用-i时,由于没有分配伪终端,看起来像pipe执行一样。但是执行结果、命令
返回值都可以正确获取。


[root@localhost temp]# docker exec -i bb2 /bin/sh
date
Tue Jul 14 04:01:11 UTC 2015
echo $?
0
dir
/bin/sh: dir: not found
echo $?
127

使用-it时,则和我们平常操作console界面类似。而且也不会像attach方式因为退出,导致
整个容器退出。
这种方式可以替代ssh或者nsenter、nsinit方式,在容器内进行操作。


[root@localhost temp]# docker exec -it bb2 /bin/sh
/ # pwd
/
/ # echo $?
0
/ # dir
/bin/sh: dir: not found
/ # echo $?
127

如果只使用-t参数,则可以看到一个console窗口,但是执行命令会发现由于没有获得stdin
的输出,无法看到命令执行情况。


[root@localhost temp]# docker exec -t bb2 /bin/sh
/ # pwd

hanging....
[root@localhost temp]# docker exec -t bb2 pwd
/
[root@localhost temp]# echo $?
0
[root@localhost temp]# docker exec -t bb2 dir
2015/07/14 04:03:57 docker-exec: failed to exec: exec: "dir": executable file not found in $PATH
[root@localhost temp]# echo $?
0

docker exec执行后,会命令执行返回值。(备注Docker1.3似乎有Bug,不能正确返回命令执行结果)


[root@localhost temp]# docker exec -it bb cat /a.sh
echo "running a.sh"
exit 10
[root@localhost temp]# docker exec -t bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
[root@localhost temp]# docker exec -it bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
[root@localhost temp]# docker exec -i bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
关于-d参数

在后台执行一个进程。可以看出,如果一个命令需要长时间进程,使用-d参数会很快返回。
程序在后台运行。


[root@localhost temp]# docker exec -d bb2 /a.sh
[root@localhost temp]# echo $?
0

如果不使用-d参数,由于命令需要长时间执行,docker exec会卡住,一直等命令执行完成
才返回。


[root@localhost temp]# docker exec  bb2 /a.sh
^C[root@localhost temp]#
[root@localhost temp]#
[root@localhost temp]# docker exec -it  bb2 /a.sh
^C[root@localhost temp]#
[root@localhost temp]# docker exec -i  bb2 /a.sh
^C[root@localhost temp]# docker exec -t  bb2 /a.sh
^C[root@localhost temp]#

查看容器运行日志

循环取最后100行日志

docker logs --tail 100 -ft  containerID
ctrl+c退出
[root@jdu4e00u53f7 sysconfig]# docker logs --tail 100 -ft  7ef4bba57f49
2017-07-27T01:37:58.587345000Z root@7ef4bba57f49:/usr/lib/apt# ls /usr/bin/apt
2017-07-27T01:37:58.587614000Z /usr/bin/apt
2017-07-27T01:38:02.795557000Z root@7ef4bba57f49:/usr/lib/apt# ls /usr/bin/apt -al
2017-07-27T01:38:02.795840000Z -rwxr-xr-x 1 root root 14376 Mar  8 13:48 /usr/bin/apt
2017-07-27T01:38:14.135456000Z root@7ef4bba57f49:/usr/lib/apt# cd /etc/apt/
2017-07-27T01:38:14.135617000Z apt.conf.d/     preferences.d/  sources.list    sources.list.d/ trusted.gpg     trusted.gpg.d/  
2017-07-27T01:38:22.727239000Z root@7ef4bba57f49:/usr/lib/apt# cd /etc/apt/
2017-07-27T01:38:26.128830000Z root@7ef4bba57f49:/etc/apt# cat sources.list
2017-07-27T01:38:26.129146000Z # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
2017-07-27T01:38:26.129405000Z # newer versions of the distribution.
2017-07-27T01:38:26.129637000Z deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
2017-07-27T01:38:26.129869000Z # deb-src http://archive.ubuntu.com/ubuntu/ xenial main restricted
2017-07-27T01:38:26.130119000Z 
2017-07-27T01:38:26.130360000Z ## Major bug fix updates produced after the final release of the
2017-07-27T01:38:26.130584000Z ## distribution.
2017-07-27T01:38:26.130803000Z deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
2017-07-27T01:38:26.132012000Z # deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
2017-07-27T01:38:26.132346000Z 
2017-07-27T01:38:26.133143000Z ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
2017-07-27T01:38:26.133410000Z ## team. Also, please note that software in universe WILL NOT receive any
2017-07-27T01:38:26.133642000Z ## review or updates from the Ubuntu security team.
2017-07-27T01:38:26.133864000Z deb http://archive.ubuntu.com/ubuntu/ xenial universe
2017-07-27T01:38:26.134115000Z deb-src http://archive.ubuntu.com/ubuntu/ xenial universe
2017-07-27T01:38:26.134355000Z deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
2017-07-27T01:38:26.134577000Z deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates universe
2017-07-27T01:38:26.134799000Z 
2017-07-27T01:38:26.135034000Z ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
2017-07-27T01:38:26.135265000Z ## team, and may not be under a free licence. Please satisfy yourself as to
2017-07-27T01:38:26.135504000Z ## your rights to use the software. Also, please note that software in
2017-07-27T01:38:26.136740000Z ## multiverse WILL NOT receive any review or updates from the Ubuntu
2017-07-27T01:38:26.136988000Z ## security team.
2017-07-27T01:38:26.137232000Z deb http://archive.ubuntu.com/ubuntu/ xenial multiverse

查看容器中的进程情况

docker top containerId
[root@jdu4e00u53f7 sysconfig]# docker top  7ef4bba57f49
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                29688               29674               0                   09:53               pts/1               00:00:00            /bin/bash

检查容器的所有信息

 

docker inspect containerId

 

[root@jdu4e00u53f7 sysconfig]# docker inspect 7ef4bba57f49
[
 {
 "Id": "7ef4bba57f499b056be1ae1c0b03ba34c06315733d572f40449ca8d16c469f1c",
 "Created": "2017-07-27T01:17:51.17074445Z",
 "Path": "/bin/bash",
 "Args": [],
 "State": {
 "Status": "running",
 "Running": true,
 "Paused": false,
 "Restarting": false,
 "OOMKilled": false,
 "Dead": false,
 "Pid": 29688,
 "ExitCode": 0,
 "Error": "",
 "StartedAt": "2017-07-27T01:53:58.801406665Z",
 "FinishedAt": "2017-07-27T01:53:16.090752206Z"
 },
 "Image": "sha256:14f60031763da664d5d65f428afbc2e6f9778dfdde17effca7704a138225c4a6",
 "ResolvConfPath": "/var/lib/docker/containers/7ef4bba57f499b056be1ae1c0b03ba34c06315733d572f40449ca8d16c469f1c/resolv.conf",
 "HostnamePath": "/var/lib/docker/containers/7ef4bba57f499b056be1ae1c0b03ba34c06315733d572f40449ca8d16c469f1c/hostname",
 "HostsPath": "/var/lib/docker/containers/7ef4bba57f499b056be1ae1c0b03ba34c06315733d572f40449ca8d16c469f1c/hosts",
 "LogPath": "",
 "Name": "/cocky_mclean",
 "RestartCount": 0,
 "Driver": "devicemapper",
 "MountLabel": "",
 "ProcessLabel": "",
 "AppArmorProfile": "",
 "ExecIDs": null,
 "HostConfig": {
 "Binds": null,
 "ContainerIDFile": "",
 "LogConfig": {
 "Type": "journald",
 "Config": {}
 },
 "NetworkMode": "default",
 "PortBindings": {},
 "RestartPolicy": {
 "Name": "no",
 "MaximumRetryCount": 0
 },
 "AutoRemove": false,
 "VolumeDriver": "",
 "VolumesFrom": null,
 "CapAdd": null,
 "CapDrop": null,
 "Dns": [],
 "DnsOptions": [],
 "DnsSearch": [],
 "ExtraHosts": null,
 "GroupAdd": null,
 "IpcMode": "",
 "Cgroup": "",
 "Links": null,
 "OomScoreAdj": 0,
 "PidMode": "",
 "Privileged": false,
 "PublishAllPorts": false,
 "ReadonlyRootfs": false,
 "SecurityOpt": null,
 "UTSMode": "",
 "UsernsMode": "",
 "ShmSize": 67108864,
 "Runtime": "docker-runc",
 "ConsoleSize": [
 0,
 0
 ],
 "Isolation": "",
 "CpuShares": 0,
 "Memory": 0,
 "CgroupParent": "",
 "BlkioWeight": 0,
 "BlkioWeightDevice": null,
 "BlkioDeviceReadBps": null,
 "BlkioDeviceWriteBps": null,
 "BlkioDeviceReadIOps": null,
 "BlkioDeviceWriteIOps": null,
 "CpuPeriod": 0,
 "CpuQuota": 0,
 "CpusetCpus": "",
 "CpusetMems": "",
 "Devices": [],
 "DiskQuota": 0,
 "KernelMemory": 0,
 "MemoryReservation": 0,
 "MemorySwap": 0,
 "MemorySwappiness": -1,
 "OomKillDisable": false,
 "PidsLimit": 0,
 "Ulimits": null,
 "CpuCount": 0,
 "CpuPercent": 0,
 "IOMaximumIOps": 0,
 "IOMaximumBandwidth": 0
 },
 "GraphDriver": {
 "Name": "devicemapper",
 "Data": {
 "DeviceId": "19",
 "DeviceName": "docker-253:1-25300355-6e53b52a7e200e1bd050ba9ab17817d8dfeca4d2078ae742df02feb84a10fc5b",
 "DeviceSize": "10737418240"
 }
 },
 "Mounts": [],
 "Config": {
 "Hostname": "7ef4bba57f49",
 "Domainname": "",
 "User": "",
 "AttachStdin": true,
 "AttachStdout": true,
 "AttachStderr": true,
 "Tty": true,
 "OpenStdin": true,
 "StdinOnce": true,
 "Env": [
 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
 ],
 "Cmd": [
 "/bin/bash"
 ],
 "Image": "ubuntu",
 "Volumes": null,
 "WorkingDir": "",
 "Entrypoint": null,
 "OnBuild": null,
 "Labels": {}
 },
 "NetworkSettings": {
 "Bridge": "",
 "SandboxID": "aea719b68d5e803780cb8a374d62296cce46704e6eb56fbf7333c4d296b41896",
 "HairpinMode": false,
 "LinkLocalIPv6Address": "",
 "LinkLocalIPv6PrefixLen": 0,
 "Ports": {},
 "SandboxKey": "/var/run/docker/netns/aea719b68d5e",
 "SecondaryIPAddresses": null,
 "SecondaryIPv6Addresses": null,
 "EndpointID": "342f4f0f65bcfa105d5490b8fb238125e8fd3bbe008deacb6fee0465f718f8f7",
 "Gateway": "172.17.0.1",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "IPAddress": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "MacAddress": "02:42:ac:11:00:02",
 "Networks": {
 "bridge": {
 "IPAMConfig": null,
 "Links": null,
 "Aliases": null,
 "NetworkID": "cf7fb0172a019ac3fbddc27a473fa7f9d7f581c0984b07ee26e66bcaf215aa50",
 "EndpointID": "342f4f0f65bcfa105d5490b8fb238125e8fd3bbe008deacb6fee0465f718f8f7",
 "Gateway": "172.17.0.1",
 "IPAddress": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "MacAddress": "02:42:ac:11:00:02"
 }
 }
 }
 }
]

登录docker HUB

[root@jdu4e00u53f7 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: aaaaaaa 
Password: 
Login Succeeded
[root@jdu4e00u53f7 ~]# 
登录信息保存在
[root@jdu4e00u53f7 .docker]# cat ~/.docker/config.json 
{
 "auths": {
 "https://index.docker.io/v1/": {
 "auth": "asdfafadfasdfadf"
 }
 }
}[root@jdu4e00u53f7 .docker]# 

 

 

Docker中images无法使用apt-get update解决方案

ubuntu的docker配置位置

sudo vim /etc/default/docker 

centos的docker配置位置

sudo vim /etc/sysconfig/docker

DOCKER_OPTS="--dns 8.8.8.8"

#将8.8.8.8设置为自己的DNS,save

sudo service docker restart

6 持久化容器与镜像

    6.1 通过容器生成新的镜像

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

[root@jdu4e00u53f7 /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              36540f359ca3        2 weeks ago         192.5 MB
[root@jdu4e00u53f7 /]# docker commit 6e68a68e1e6a docker.io/centos_chenjunfei
sha256:b7930ab7ec9a896528f7aa5f341c79faf937d9c41b167974c8b669ef79ce6826
[root@jdu4e00u53f7 /]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos_chenjunfei   latest              b7930ab7ec9a        10 seconds ago      194.2 MB
docker.io/centos              latest              36540f359ca3        2 weeks ago         192.5 MB
[root@jdu4e00u53f7 /]# 

 

    6.2 持久化容器

export命令用于持久化容器

docker export <CONTAINER ID> > /tmp/export.tar

[root@jdu4e00u53f7 /]# docker export 6e68a68e1e6a > /opt/docker_6e68a68e1e6a.tar
[root@jdu4e00u53f7 /]# ls -alh /opt/docker_6e68a68e1e6a.tar 
-rw-r--r-- 1 root root 191M Jul 26 16:22 /opt/docker_6e68a68e1e6a.tar

import命令用于导入容器,export导出是CONTAINER,import 变成了image了。。。。

[root@jdu4e00u53f7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e68a68e1e6a centos "/bin/bash" About an hour ago Exited (137) 12 minutes ago tender_swanson
[root@jdu4e00u53f7 /]# docker rm 6e68a68e1e6a
6e68a68e1e6a
[root@jdu4e00u53f7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@jdu4e00u53f7 /]# cat /opt/docker_6e68a68e1e6a.tar |docker import - export:latest
sha256:b8e20c341cff26073e199657d752e8d36b8c6ca94386b537ae508ac0fb9ca82e
[root@jdu4e00u53f7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@jdu4e00u53f7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@jdu4e00u53f7 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
export latest b8e20c341cff 47 seconds ago 192.5 MB
docker.io/centos_chenjunfei latest b7930ab7ec9a 8 minutes ago 194.2 MB
docker.io/centos latest 36540f359ca3 2 weeks ago 192.5 MB
[root@jdu4e00u53f7 /]# 

 

     6.3 持久化镜像

Save命令用于持久化镜像

docker save 镜像ID > /tmp/save.tar

      6.4 导入持久化container

删除container 2161509ff65e

导入export.tar文件

[root@localhost /]# cat /tmp/export.tar | docker import - export:latest
af19a55ff0745fb0a68655392d6d7653c29460d22d916814208bbb9626183aaa
[root@localhost /]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
export                  latest              af19a55ff074        34 seconds ago      270.3 MB
chug/ubuntu12.10x64_2   latest              daa11948e23d        20 minutes ago      270.3 MB
chug/ubuntu12.10x64     latest              0b96c14dafcd        4 months ago        270.3 MB

 

    6.5 导入持久化image

删除image daa11948e23d

导入save.tar文件
[root@localhost /]# docker load < /tmp/save.tar
对image打tag
[root@localhost /]# docker tag daa11948e23d load:tag

 

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

 

6.7 一些其它命令

docker logs $CONTAINER_ID #查看docker实例运行日志,确保正常运行
docker inspect $CONTAINER_ID #docker inspect <image|container> 查看image或container的底层信息

docker build <path> 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
docker build -t repo[:tag] 同上,可以指定repo和可选的tag
docker build – < <dockerfile> 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
docker port <container> <container port> 查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到

7 一些使用技巧

    7.1 docker文件存放目录

Docker实际上把所有东西都放到/var/lib/docker路径下了。
[root@localhost docker]# ls -F
containers/  devicemapper/  execdriver/  graph/  init/  linkgraph.db  repositories-devicemapper  volumes/

containers目录当然就是存放容器(container)了,graph目录存放镜像,文件层(file system layer)存放在graph/imageid/layer路径下,这样我们就可以看看文件层里到底有哪些东西,利用这种层级结构可以清楚的看到文件层是如何一层一层叠加起来的。

    7.2  查看root密码

docker容器启动时的root用户的密码是随机分配的。所以,通过这种方式就可以得到容器的root用户的密码了。
docker logs 5817938c3f6e 2>&1 | grep ‘User: ‘ | tail -n1

  7.3 docker 容器部署tomcat后,日志文件中出现中文乱码,很多问号,中文的文件夹也是问好。先看看容器的locale:

[root@docker1 ~]# docker exec -it 41de9a0b6045 locale
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

在Dockerfile中添加环境变量: ENV   LC_ALL en_US.UTF-8