docker基础学习(一)-LMLPHP

docker基础学习(一)-LMLPHP

操作演示:

1、查看一个容器的版本

[root@ELK-chaofeng08 ~]# docker version
Client:
Version: 18.09.
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb ::
OS/Arch: linux/amd64
Experimental: false Server: Docker Engine - Community
Engine:
Version: 18.09.
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb ::
OS/Arch: linux/amd64
Experimental: false
[root@ELK-chaofeng08 ~]# docker -v
Docker version 18.09., build 774a1f4

2、查找指定的镜像

docker基础学习(一)-LMLPHP

3、查看已安装的镜像

[root@ELK-chaofeng08 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 4.0-alpine 83944736833a weeks ago .5MB
busybox latest d8233ab899d4 weeks ago .2MB
nginx 1.14-alpine 66952fd0a8ef weeks ago 16MB

4、从hub.docker.com网站拉取镜像

[root@ELK-chaofeng08 ~]# docker image pull nginx:1.14-alpine

5、拉取busybox

[root@ELK-chaofeng08 ~]# docker image pull busybox
Using default tag: latest
latest: Pulling from library/busybox
Digest: sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Status: Image is up to date for busybox:latest

6、删除一个镜像

[root@ELK-chaofeng08 ~]# docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Deleted: sha256:d8233ab899d419c58cf3634c0df54ff5d8acc28f8173f09c21df4a07229e1205
Deleted: sha256:adab5d09ba79ecf30d3a5af58394b23a447eda7ffffe16c500ddc5ccb4c0222f

7、查看已安装镜像的完整的IMAGE ID

[root@ELK-chaofeng08 ~]# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 4.0-alpine sha256:83944736833a71a490d842a93ec192d78fbe61063ca8b38d5a2b786f477f20ca weeks ago .5MB
nginx 1.14-alpine sha256:66952fd0a8efa0598626fad89d3a0827bc24fc92c3adb576adbc9fd58606e1af weeks ago 16MB

8、查看容器中运行了哪些服务(包含启动和停止的)

[root@ELK-chaofeng08 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdbb9c2cc7c8 redis:4.0-alpine "docker-entrypoint.s…" minutes ago Up minutes /tcp redis1
77f4d1bb8da9 nginx:1.14-alpine "nginx -g 'daemon of…" minutes ago Up minutes /tcp web1

还要关注STATUS的这个值,这个值显示为Up,表示服务处于启动状态。默认情况下不能看到停止状态的容器,所以需要加上-a参数,即“docker ps -a”来查看STATUS这个值为Exit,表示为关闭状态。

9、启动一个容器

一个容器中只能运行一个镜像,但是可以运行多个容器。

[root@ELK-chaofeng08 ~]# docker container run --name redis2 -d redis:4.0-alpine
68d5b4f366afe5e731112dfb809c8669dd186dbbd6259b59056811345ab1cc4c

在docker run这个过程中,首先是去拉去本地的redis:4.0-alpine这个镜像image,我们刚刚是已经从互联网上拉取过并放在了本地,因此可以直接启动。但是如果docker没有找到本地的镜像image,则会去互联网上下载,然后再运行。其中--name是指定容器的名字,-d是指定运行的镜像,在运行容器之前还有创建容器这个过程。这就是他们启动的过程

10、查看此时运行的容器

[root@ELK-chaofeng08 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68d5b4f366af redis:4.0-alpine "docker-entrypoint.s…" minutes ago Up minutes /tcp redis2
77f4d1bb8da9 nginx:1.14-alpine "nginx -g 'daemon of…" minutes ago Up minutes /tcp web1

11、查看已运行的容器的详细信息

[root@ELK-chaofeng08 ~]# docker inspect redis2

12、交互式向容器发送指令

[root@ELK-chaofeng08 ~]# docker exec -it redis2 /bin/ls /etc
TZ init.d network securetty
alpine-release inittab opt services
apk issue os-release shadow

-i表示交互式输入指令。

-t表示分配一个伪终端。

13、查看容器的日志

[root@ELK-chaofeng08 ~]# docker logs web1
172.17.0.1 - - [/Mar/::: +] "GET / HTTP/1.1" "-" "curl/7.29.0" "-"

此容器中运行了一个web服务器,我们可以查看日志

镜像的介绍:

docker基础学习(一)-LMLPHP

docker基础学习(一)-LMLPHP

docker基础学习(一)-LMLPHP

docker基础学习(一)-LMLPHP

docker基础学习(一)-LMLPHP

也就是说一个镜像可以有多个tag,但是一个tag只能对应一个镜像。

docker基础学习(一)-LMLPHP

14、删除镜像

如果要删除镜像,此镜像必须是停止状态,然后需要先删除容器,再删除镜像,如下所示:

[root@ELK-chaofeng08 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8c95fc5ba8 chen/busybox/httpd:v0. "httpd -f -h /data/h…" minutes ago Up minutes jolly_poitras
[root@ELK-chaofeng08 ~]# docker stop db8c95fc5ba8
db8c95fc5ba8

删除容器

[root@ELK-chaofeng08 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
db8c95fc5ba8 chen/busybox/httpd:v0. "httpd -f -h /data/h…" minutes ago Exited () minutes ago
9fdca18c27b4 chen/busybox/httpd:v0. "httpd -f -h /data/h…" minutes ago Exited () minutes ago
68d5b4f366af redis:4.0-alpine "docker-entrypoint.s…" hours ago Exited () About an hour
fdbb9c2cc7c8 redis:4.0-alpine "docker-entrypoint.s…" hours ago Exited () hours ago
77f4d1bb8da9 nginx:1.14-alpine "nginx -g 'daemon of…" hours ago Exited () About an hour
[root@ELK-chaofeng08 ~]# docker container rm 9fdca18c27b4
9fdca18c27b4
[root@ELK-chaofeng08 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chen/busybox/httpd v0. 2a906703e3da minutes ago .2MB
chen/busybox/httpd v0. b17250b566ae minutes ago .2MB
redis 4.0-alpine 83944736833a weeks ago .5MB
busybox latest d8233ab899d4 weeks ago .2MB
nginx 1.14-alpine 66952fd0a8ef weeks ago 16MB
quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 months ago .6MB
[root@ELK-chaofeng08 ~]# docker image rm b17250b566ae
Untagged: chen/busybox/httpd:v0.
Deleted: sha256:b17250b566aeeaa99a1e0850717d56b018d846cf6f27253a72ced90eb60912c3
Deleted: sha256:2b0eba4397e6216f884be40579b35e0e950211bf8a751552f889a6a3200b5ed4
[root@ELK-chaofeng08 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chen/busybox/httpd v0. 2a906703e3da minutes ago .2MB
redis 4.0-alpine 83944736833a weeks ago .5MB
busybox latest d8233ab899d4 weeks ago .2MB
nginx 1.14-alpine 66952fd0a8ef weeks ago 16MB
quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 months ago .6MB

删除镜像成功。

04-05 02:17