2

【Docker 系列】docker 学习 三

 2 years ago
source link: https://segmentfault.com/a/1190000040921192
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【Docker 系列】docker 学习 三

使用 Dcoker 部署 nginx

搜索 nginx 镜像

  • 使用 docker search nginx
# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15246     [OK]
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2053                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   815                  [OK]
...
  • 或者在 dockerhub 上搜索 nginx,具体的版本和详细信息会更加全面,一般使用官方的

拉取 nginx 镜像

拉取 nginx 镜像,我们这里就拉取最新版本的 nginx

# docker pull nginx
Using default tag: latest                # 最新版本
latest: Pulling from library/nginx        # nginx 库
33847f680f63: Pull complete                #分层下载,后续会详细学习分层的原理
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90                                        # 签名
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest            # nginx真实下载路径

创建并运行容器

  • 新建一个容器命名为 nginx1
  • nginx 默认端口是 80,将 docker 容器中的 80 端口映射程 主机中的 8888 端口
  • 设置后台运行 nginx 容器
# docker run -d --name nginx1 -p 8888:80 nginx
2772a40501571630fb6fc2305f41f7a409299c4d15595ba3dd654d73f2a5e7b6

# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
2772a4050157   nginx     "/docker-entrypoint.…"   2 seconds ago   Up 2 seconds   0.0.0.0:8888->80/tcp   nginx1

使用 curl 命令,访问一下 主机的 8888 端口,查看是否访问 OK

# curl localhost:8888
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

咱们也可以进入到 nginx docker 容器中,直接访问 80 端口

# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
2772a4050157   nginx     "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   0.0.0.0:8888->80/tcp   nginx1

进入 nginx1 的终端
# docker exec -it nginx1 /bin/bash

访问 80 端口
# curl localhost:80

因为我们在创建 nginx1 容器的时候,将主机的 8888 端口,映射到了 容器 nginx1 的 80 端口,因此可以访问主机的 8888 端口来访问到 nginx1 容器中的 80 端口

此时,可以访问我的阿里云服务器的 8888 端口,实际是可以访问到我的 nginx1 容器中的 nginx 服务器

尝试使用和部署 可视化 Docker 页面 portainer

portainer 是 Docker 图形化页面管理工具,他提供了一个后台面板供我们操作和管理。

创建和启动 portainer

docker run -d -p 8888:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

可选参数说明:

  • --restart

当容器退出的时候,重启策略是什么样的,这里使用 always ,默认值是 “no”

绑定挂载卷

  • --privileged

给予这个容器扩展权限

  • -p 8888:9000

将容器里面的 9000 端口,映射到主机的 8888 端口,便于我们访问主机 8888 端口的时候,可以访问到 portainer 容器的 9000 端口

访问和设置 portainer 用户

浏览器访问:IP:8888

设置密码,点击 Create users 即可看到如下页面

解释一下上述画红线的地方:

  • 0 stacks

    Stacks就是一组一致运行的、相互关联的services

  • 1 container
  • 1 volume

1 个挂载卷

  • 3 images

我们到主机上面查看 docker 的系统信息

docker info

进入到我们自己的 docker 服务,可以看到上述解释的每一个项

咱们点进 images 看看效果:

我们可以在这个 web 管理页面看到我们 docker 服务中 3 个镜像的详情,也可以对镜像进行删除,新建,导入和导出

感兴趣的话,可以自己多熟悉和尝试一下 portainer 的使用,以后我们做 CI/CD 的时候,会使用 Rancher

大家学习的时候,可以多多交流,多多练习,多多查看帮助文档,或者在命令行里面使用 –help 来查看都有哪些参数,例如:

docker run --help

用法: docker run [参数] 镜像 [命令] [命令的参数列表...]

Run a command in a new container

-a, --attach list Attach to STDIN, STDOUT or STDERR

-c, --cpu-shares int CPU shares (relative weight)

-d, --detach 后台运行容器

-e, --env list 设置环境变量

-h, --hostname string Container host name

-i, --interactive Keep STDIN open even if not attached

-l, --label list Set meta data on a container

-m, --memory bytes 内存限制

-p, --publish list Publish a container's port(s) to the host

-P, --publish-all Publish all exposed ports to random ports

-t, --tty Allocate a pseudo-TTY

-u, --user string 用户名或者uid

-v, --volume list 挂载卷

-w, --workdir string 设置容器中的工作目录

参考资料:

docker docs

欢迎点赞,关注,收藏

朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力

好了,本次就到这里

技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。

我是小魔童哪吒,欢迎点赞关注收藏,下次见~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK