前面了解了如何安装docker,今天继续docker基础的学习。
一、Docker命令导图
docker的命令主要分为两大块,一块是镜像相关的命令,一块是容器相关的命令。
二、Docker容器的运行
运行容器使用docker run命令,执行docker命令时默认使用root权限,可以通过sudo -i进行提权,docker run --help查看命令相关帮助。
###普通用户提权
demo@docker:~$ sudo -i
[sudo] password for demo:
###docker run命令帮助
root@docker:~# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
...
...
常用参数:
-d: 后台运行容器,并返回容器ID。
-i: 以交互模式运行容器,通常与 -t 同时使用。
-P: 随机端口映射,容器内部端口随机映射到主机的端口。
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口。
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用。
--name="容器名": 为容器指定一个名称。
--dns 8.8.8.8: 指定容器的DNS服务器,默认和宿主一致。
-h "主机名": 指定容器的主机名;
-e username="ritchie": 设置环境变量;
--env-file=[]: 从指定文件读入环境变量;
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值。
--net="bridge": 指定容器的网络连接类型,支持bridge/host/none/container四种类型。
bridge——使用docker daemon指定的网桥;【默认】
host——容器使用主机的网络;none——容器使用自己的网络(类似--net=bridge),但是不进行配置;
container:NAME_or_ID——使用其他容器的网路,共享IP和PORT等网络资源;--link=[]: 添加链接到另一个容器。
--expose=[]: 开放一个端口或一组端口。
--volume , -v: 绑定一个卷。
--privileged=false, 指定容器是否为特权容器,特权容器拥有所有的capabilities。
--restart="no", 指定容器停止后的重启策略。
no:容器退出时不重启;【默认】
on-failure:容器故障退出(返回值非零)时重启;
always:容器退出时总是重启;
docker run运行一个apache的容器,使用-d参数在后台运行,给容器起个名字叫apache,将容器的80端口映射给主机。如果本地没有相关镜像的话,docker会自动去定义好的镜像加速网站拉取镜像并运行。
###运行一个apache容器
root@docker:~# docker run -d --name apache -p 80:80 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
966cdeee86070b50683a2e0733d30ddc4db648b3dc28fbb12606ba1c7f556734
###查看本地镜像
root@docker:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest dabbfbe0c57b 10 months ago 144MB
###查看运行的容器
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
966cdeee8607 httpd "httpd-foreground" 3 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp apache
###查看apache是否启动
root@docker:~# curl http://127.0.0.1
<html><body><h1>It works!</h1></body></html>
###停止容器,docker stop 容器id或者容器名字
root@docker:~# docker stop apache
apache
###查看容器状态
root@docker:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
966cdeee8607 httpd "httpd-foreground" 8 minutes ago Exited (0) 7 seconds ago apache
###启动容器,docker start 容器id或者容器名字
root@docker:~# docker start apache
apache
root@docker:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
966cdeee8607 httpd "httpd-foreground" 11 minutes ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp apache
三、Docker进入容器
在企业中,运维工程师与开发工程师都可能会有进入容器内部的需求,一般建议使用以下两种Docker原生方式进入容器。
1、docker attach
docker attach 容器id或容器名字,直接进入容器正在执行某个命令的终端,不能在里面操作,不启动新的进程,退出时默认会关闭容器,不常用。
###运行一个容器循环打印hello,2s一次。
root@docker:~# docker run -itd ubuntu /bin/bash -c "while true; do echo hello; sleep 2; done"
b003c686e6d901fd83503e6bfcd511a6281ac9ff3d68a70daa613806aea7abb9
###查看容器状态,已运行。
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b003c686e6d9 ubuntu "/bin/bash -c 'while…" 5 seconds ago Up 4 seconds festive_chaplygin
###进入容器,可以看到每2s回显一次hello,Ctrl+c退出容器。
root@docker:~# docker attach b003c686e6d9
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
^Croot@docker:~#
###再查看容器,发现容器已停止。
root@docker:~#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@docker:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b003c686e6d9 ubuntu "/bin/bash -c 'while…" About a minute ago Exited (130) 22 seconds ago festive_chaplygin
root@docker:~#
###使用Ctrl+P+Q组合键退出,不会停止容器。
root@docker:~# docker attach b003c686e6d9
hello
hello
hello
read escape sequence
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b003c686e6d9 ubuntu "/bin/bash -c 'while…" 3 minutes ago Up About a minute festive_chaplygin
2、docker exec
docker exec:docker exec -it 容器id或容器名字 bash,开启一个新的终端,可以在里面进行操作,退出不会导致容器停止。如果容器的默认用户不是root,需要使用root用户登录容器时,可以使用-u参数,指定用户名为root。
###启动之前的apache容器。
root@docker:~# docker start apache
apache
###进入到容器,可以在容器中执行命令。
root@docker:~# docker exec -it -u root apache bash
root@966cdeee8607:/usr/local/apache2#
root@966cdeee8607:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@966cdeee8607:/usr/local/apache2# pwd
/usr/local/apache2
四、Docker日志
在 Docker 的整个生命周期中,超过70%的时间我们都是在做 Docker 的运维工作。查看 Docker 的日志,是运维工作中非常重要的一个环节。Docker 的日志分为两种:引擎的日志和应用的日志。
1、Docker引擎日志
Docker 引擎日志是指在 Docker 守护进程执行过程中产生的日志信息。如果 Docker 引擎出现了问题,则需要在宿主机上查看引擎日志。
###执行命令查看Docker引擎日志。
root@docker:~# journalctl -u docker | more
Jun 22 03:56:45 docker systemd[1]: Starting Docker Application Container Engine...
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.305750720Z" level=info msg="Starting up"
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.308003247Z" level=info msg="detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: /run/systemd/resolve/resolv.conf"
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.390323036Z" level=info msg="Loading containers: start."
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.825067799Z" level=info msg="Loading containers: done."
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.863884696Z" level=info msg="Docker daemon" commit=659604f graphdriver=overlay2 version=24.0.2
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.864183720Z" level=info msg="Daemon has completed initialization"
Jun 22 03:56:45 docker dockerd[2809]: time="2023-06-22T03:56:45.891228572Z" level=info msg="API listen on /run/docker.sock"
Jun 22 03:56:45 docker systemd[1]: Started Docker Application Container Engine.
Jun 22 07:58:29 docker systemd[1]: Stopping Docker Application Container Engine...
Jun 22 07:58:30 docker dockerd[2809]: time="2023-06-22T07:58:30.028381022Z" level=info msg="Processing signal 'terminated'"
Jun 22 07:58:30 docker dockerd[2809]: time="2023-06-22T07:58:30.459905535Z" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd namespace=moby
Jun 22 07:58:30 docker dockerd[2809]: time="2023-06-22T07:58:30.460298335Z" level=info msg="Daemon shutdown complete"
Jun 22 07:58:30 docker systemd[1]: docker.service: Deactivated successfully.
Jun 22 07:58:30 docker systemd[1]: Stopped Docker Application Container Engine.
Jun 22 07:58:30 docker systemd[1]: docker.service: Consumed 2.715s CPU time.
Jun 22 08:39:06 docker systemd[1]: Starting Docker Application Container Engine...
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.409251450Z" level=info msg="Starting up"
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.453079962Z" level=info msg="detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: /run/systemd/resolve/resolv.conf"
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.553516830Z" level=info msg="parsed scheme: \"unix\"" module=grpc
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.553556419Z" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.553576265Z" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock <nil> 0 <nil>}] <nil> <nil>}" modu
le=grpc
Jun 22 08:39:06 docker dockerd[3541]: time="2023-06-22T08:39:06.553587408Z" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.006283576Z" level=info msg="parsed scheme: \"unix\"" module=grpc
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.006326749Z" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.006355216Z" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock <nil> 0 <nil>}] <nil> <nil>}" modu
le=grpc
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.006364999Z" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.541435867Z" level=info msg="[graphdriver] using prior storage driver: overlay2"
Jun 22 08:39:07 docker dockerd[3541]: time="2023-06-22T08:39:07.632705458Z" level=info msg="Loading containers: start."
Jun 22 08:39:08 docker dockerd[3541]: time="2023-06-22T08:39:08.797403247Z" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a prefer
red IP address"
Jun 22 08:39:08 docker dockerd[3541]: time="2023-06-22T08:39:08.844426922Z" level=info msg="Loading containers: done."
Jun 22 08:39:09 docker dockerd[3541]: time="2023-06-22T08:39:09.084693661Z" level=info msg="Docker daemon" commit=03df974 graphdriver(s)=overlay2 version=20.10.20
Jun 22 08:39:09 docker dockerd[3541]: time="2023-06-22T08:39:09.084740253Z" level=info msg="Daemon has completed initialization"
Jun 22 08:39:09 docker systemd[1]: Started Docker Application Container Engine.
Jun 22 08:39:09 docker dockerd[3541]: time="2023-06-22T08:39:09.168688420Z" level=info msg="API listen on /var/run/docker.sock"
2、Docker应用日志
Docker 将应用运行在容器中,应用输出日志也就输出到容器中了,访问 Docker 应用的日志也就变成了访问 Docker 容器的日志。每个Docker守护进程都有一个默认的日志驱动程序,每个容器都使用该驱动程序,Docker 默认使用 json-file 日志驱动。
###查看Docker默认的日志驱动。
root@docker:~# docker info | grep Logging
Logging Driver: json-file
Docker 支持以下日志记录驱动程序:
3、查看容器日志
Docker 提供了 docker logs 命令来读取容器日志的信息。
用法:
docker logs 选项 容器ID或容器名
选项:
--details 显示提供给日志的额外详细信息。
--follow 或 -f 跟踪日志的输出。
--since 显示某个开始时间的所有日志。
--tail 或 -n 从日志末尾开始显示的行数。
--timestamps 或 -t 显示时间戳。
--until 显示某个时间之前的所有日志。
###运行一个nginx容器。
root@docker:~# docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
f575ef83a41bbbb3698f49045dccfe593f51bee9945147244394d0540ace819e
root@docker:~#
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f575ef83a41b nginx "/docker-entrypoint.…" 39 seconds ago Up 25 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp pedantic_hoover
###查看容器日志,-f参数跟踪日志输出。
root@docker:~# docker logs -f f575ef83a41b
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/06/22 15:46:43 [notice] 1#1: using the "epoll" event method
2023/06/22 15:46:43 [notice] 1#1: nginx/1.21.5
2023/06/22 15:46:43 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/06/22 15:46:43 [notice] 1#1: OS: Linux 5.15.0-73-generic
2023/06/22 15:46:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/06/22 15:46:43 [notice] 1#1: start worker processes
2023/06/22 15:46:43 [notice] 1#1: start worker process 31
2023/06/22 15:46:43 [notice] 1#1: start worker process 32
2023/06/22 15:46:43 [notice] 1#1: start worker process 33
2023/06/22 15:46:43 [notice] 1#1: start worker process 34
###查看容器日志,显示最后12行,并显示时间戳。
root@docker:~# docker logs -n 12 -t f575ef83a41b
2023-06-22T15:46:43.450743449Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
2023-06-22T15:46:43.452445183Z /docker-entrypoint.sh: Configuration complete; ready for start up
2023-06-22T15:46:43.623505983Z 2023/06/22 15:46:43 [notice] 1#1: using the "epoll" event method
2023-06-22T15:46:43.623527888Z 2023/06/22 15:46:43 [notice] 1#1: nginx/1.21.5
2023-06-22T15:46:43.623544018Z 2023/06/22 15:46:43 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023-06-22T15:46:43.623549744Z 2023/06/22 15:46:43 [notice] 1#1: OS: Linux 5.15.0-73-generic
2023-06-22T15:46:43.623552328Z 2023/06/22 15:46:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023-06-22T15:46:43.623562806Z 2023/06/22 15:46:43 [notice] 1#1: start worker processes
2023-06-22T15:46:43.623804592Z 2023/06/22 15:46:43 [notice] 1#1: start worker process 31
2023-06-22T15:46:43.623876133Z 2023/06/22 15:46:43 [notice] 1#1: start worker process 32
2023-06-22T15:46:43.624077454Z 2023/06/22 15:46:43 [notice] 1#1: start worker process 33
2023-06-22T15:46:43.632502784Z 2023/06/22 15:46:43 [notice] 1#1: start worker process 34
总结
以上就是今天学习的内容,如何运行一个容器,如何进入容器,Docker 的日志。