开始卷docker啦

docker 是什么?docker 为什么受推崇?docker 怎么卷?

Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用,然后发布到任何linux主机。容器采用沙箱机制,相互不存在任何接口,而且性能开销非常低。

1.准备一台Linux服务器或电脑。

2.使用官方安装脚本自动安装。

> curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

3.开始使用 docker。

3.1.启动 docker
> sudo systemctl start docker
3.2.重启 docker
> sudo systemctl restart docker
3.3.停止 docker
> sudo systemctl stop docker
3.4.设置开机自启动
> sudo systemctl enable docker
3.5.查看 docker 状态
> sudo systemctl status docker

4.运行 hello-world 判断 docker 是否安装成功。

> sudo docker run hello-world


Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

5.运行 docker 容器。如果没有该镜像,那么 docker 将自动下载镜像。

> docker run -i -t ubuntu:15.10 /bin/bash
/#>

参数解析:
-t: 在新容器内指定一个伪终端或终端。
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
注意第二行 /#>,说明已进入一个 ubuntu15.10 系统的容器

5.1.通过 cat /proc/version 和 ll/dir 命令分别查看容器的操作系统版本和目录结构
/#> cat /proc/version
Linux version 3.10.0-1160.105.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu Dec 7 15:39:45 UTC 2023
/#> ll
total 72
drwxr-xr-x   1 root root 4096 Feb  3 02:48 ./
drwxr-xr-x   1 root root 4096 Feb  3 02:48 ../
-rwxr-xr-x   1 root root    0 Feb  3 02:48 .dockerenv*
drwxr-xr-x   2 root root 4096 Jul  6  2016 bin/
drwxr-xr-x   2 root root 4096 Oct 19  2015 boot/
drwxr-xr-x   5 root root  360 Feb  3 02:48 dev/
drwxr-xr-x   1 root root 4096 Feb  3 02:48 etc/
drwxr-xr-x   2 root root 4096 Oct 19  2015 home/
drwxr-xr-x   8 root root 4096 Sep 13  2015 lib/
drwxr-xr-x   2 root root 4096 Jul  6  2016 lib64/
drwxr-xr-x   2 root root 4096 Jul  6  2016 media/
drwxr-xr-x   2 root root 4096 Oct 19  2015 mnt/
drwxr-xr-x   2 root root 4096 Jul  6  2016 opt/
dr-xr-xr-x 114 root root    0 Feb  3 02:48 proc/
drwx------   2 root root 4096 Jul  6  2016 root/
drwxr-xr-x   5 root root 4096 Jul  6  2016 run/
drwxr-xr-x   1 root root 4096 Jul 22  2016 sbin/
drwxr-xr-x   2 root root 4096 Jul  6  2016 srv/
dr-xr-xr-x  13 root root    0 Feb  3 02:25 sys/
drwxrwxrwt   2 root root 4096 Jul  6  2016 tmp/
drwxr-xr-x   1 root root 4096 Jul 22  2016 usr/
drwxr-xr-x   1 root root 4096 Jul 22  2016 var/

6.退出容器

/#> exit
exit
>

7.通过 docker ps  查看容器运行状态

我们这时候发现容器没有运行,原因是 exit 指令在退出的同时也将容器停止掉。不过,可以通过 -d 创建一个后台运行的容器。

7.1.创建一个后台运行的容器
> docker run -d ubuntu:15.10 /bin/bash -c "while true;do echo hello world; sleep 1; done"
4a07e4711f538e4cec5696afef0dbc9632e7ab402a99d7a91080b391ae4744f7
7.2. 查询容器运行状态
> docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
4a07e4711f53   ubuntu:15.10   "/bin/bash -c 'while…"   5 seconds ago   Up 4 seconds             stoic_banach

参数解析:
CONTAINER ID: 容器 ID。
IMAGE: 使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。

状态有7种:
created(已创建)
restarting(重启中)
running 或 Up(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。
NAMES: 自动分配的容器名称。

7.3.在宿主机使用 docker logs  {容器ID} 查看输出内容:
> docker logs 4a07e4711f53
hello world
hello world
hello world
hello world
hello world
hello world

8.停止容器

> docker stop 4a07e4711f53
4a07e4711f53

再次使用 docker ps 查看发现已经没有运行的容器

> docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
>

9.在 docker 中使用 nginx 容器。

9.1.下载 nginx 镜像
> docker pull nginx:latest
9.2.运行 nginx 容器
> docker run --name root-nginx -p 8080:80 -d nginx

参数解析: 

 --name root-nginx:容器名称。

-p 8080:80:端口映射,将宿主机 8080 端口映射到容器内部的 80 端口。

-d nginx:设置容器在后台运行。

 最后在浏览器访问http://localhost:8080能够打开页面(如下图)说明配置成功!

10.在 docker 中使用 mysql

10.1.下载 mysql 镜像
> docker pull mysql
10.2.运行 mysql 容器
> docker run -itd --name root-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

参数解析: 

 --name root-mysql:容器名称。

-p 3306:3306:端口映射,将宿主机 3306 端口映射到容器内部的 3306 端口。

MYSQL_ROOT_PASSWORD=123456:设置 root 账号的密码

 

 

 

 

posted @ 2024-02-03 12:02  七月的枫丶  阅读(7)  评论(0编辑  收藏  举报