docker使用笔记

docker使用笔记

 

本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/14349287.html


本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/14349287.html

本文用到的符号说明:

1
2
3
4
5
<container>     容器名,形如 tiangolo/nginx-rtmp 或 nginx 等  
<img>           镜像名,形如 ubuntu:16.04 或 ubuntu 等  
<container_id>  容器 id,形如 8648e373168c  
<img_id>        镜像 id,形如 52b18ed2d4bf 
<tag>           镜像标签,形如 0.2, 0.3  

1. 容器

1.1 查看容器

查看当前处于运行状态的容器:

docker ps

查看所有容器:

docker ps -a

1.2 启动/停止容器

1
2
3
4
docker start <container>
docker stop <container>
docker start ffmpeg-centos-0.2
docker stop ffmpeg-centos-0.2

1.3 创建容器

语法:

1
2
3
4
5
6
7
8
docker run [OPTIONS] <img> [COMMAND] [ARG...]
    -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
    -d: 后台运行容器,并返回容器ID;
    -i: 以交互模式运行容器,通常与 -t 同时使用;
    -p: 端口映射,格式为:主机(宿主)端口:容器端口
    -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
    -h: 容器里的主机名(hostname)
    --name="<container>": 为容器指定一个名称;

实例:

基于镜像创建容器,容器在前台运行,在容器中执行 exit 后容器将变为停止状态:

1
2
3
docker run -it --name <container> <img> bash
docker run -it --name <container> <img>:<tag> bash
docker run -it --name ffmpeg-centos-0.2 ffmpeg-centos:0.2 bash

基于镜像创建容器,容器在后台运行,在容器中执行 exit 后容器仍处于运行状态:

docker run -itd --name ffmpeg-centos-0.2 ffmpeg-centos:0.2 bash

基于镜像创建容器,容器在前台运行,将宿主机上 /home/think/work 目录映射为容器中的 /work 目录:

docker run -it -v /home/frank/work:/work --name ffmpeg-centos-0.2 ffmpeg-centos:0.2 bash

基于镜像创建容器,容器在前台运行,将宿主机上 60084 端口映射为容器中的 80 端口:

docker run -it -p 60084:80 --name ffmpeg-centos-0.2 ffmpeg-centos:0.2 bash

1.4 进入容器

语法:

1
2
3
4
docker exec [OPTIONS] <container> COMMAND [ARG...]
    -d: 分离模式: 在后台运行
    -i: 即使没有附加也保持 STDIN 打开
    -t: 分配一个伪终端

实例:

进入容器并打开容器中的 bash:

1
2
docker exec -it <container> bash
docker exec -it ffmpeg-centos-0.2 bash

进入容器后,在容器中执行 exit 将退出容器进入主机环境,但容器并不会停止。

如果加 -d 参数,无法进入容器中操作 bash,此命令执行完,仍在宿主机 bash 中。

docker exec -itd <container> bash

所以 -d 参数目前看来没什么用,待研究。

1.5 查看容器信息

查看容器信息,诸如 IP、映射目录等

1
2
docker inspect <container>
docker inspect ffmpeg-centos-0.2

1.6 重命名容器

1
2
docker rename <container1> <container2>
docker rename ffmpeg-centos-debug ffmpeg-centos-0.2

1.7 删除容器

删除处于停止状态的容器

1
2
docker rm <container>
docker rm ffmpeg-centos-0.2

2. 镜像

2.1 查看镜像

docker images

2.2 拉取镜像

从 docker 的网络镜像仓库拉取镜像到本地:

1
2
3
docker pull <img>
docker pull <img>:<tag>
docker pull centos:7.2.1511

2.3 删除镜像

1
2
docker rmi <img>
docker rmi ffmpeg-centos:0.2

2.4 重命名镜像

1
2
docker tag <img_id> <img>:<tag>
docker tag 8648e373168c ffmpeg-centos:0.2

2.4 基于容器制作镜像

1
2
3
docker commit -m "comment" -a "author" <container> <img>:<tab>
docker commit -m "comment" -a "author" <container_id> <img>:<tab>
docker commit -m "2nd version" -a "frank" ffmpeg-centos-0.2 ffmpeg-centos:0.2

2.5 导出镜像

1
2
docker save <img>:<tag> | gzip > <xxx>.tar.gz
docker save ffmpeg-centos:0.2 | gzip > docker-img-ffmpeg-centos-0.2.tar.gz

2.6 导入镜像

1
2
gunzip -c <xxx>.tar.gz | docker load
gunzip -c docker-img-ffmpeg-centos-0.2.tar.gz | docker load

3. 设置

3.1 普通用户免 sudo 使用 docker

输入以下命令,将当前用户添加到 docker 组,即可免 sudo 使用 docker:

sudo gpasswd -a ${USER} docker

注意:上述命令行中的 ${USER} 将获取到当前用户名,当前用户是 frank,就是将 frank 添加到 docker 组,当前用户是 root,就是将 root 用户添加到 docker 组。当然也可手动指定用户名将其添加其到 docker 组,如当前用户是 root,运行 gpasswd -a frank docker 即可。

3.2 设置镜像加速

docker镜像源位于美国,拉取镜像非常缓慢。可配置国内镜像源,加快镜像拉取速度。

修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值:

1
2
3
4
5
6
7
8
9
{
    "registry-mirrors":
    [
        "https://registry.docker-cn.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}

上述配置文件添加了四个国内镜像源:docker 中国、清华、163 和腾讯。

修改上述配置文件后重启docker服务:

systemctl restart docker

4. 进阶用法

4.1 将主机上普通用户映射进容器

默认情况下,使用 docker run --privileged=true 命令以特权方式创建的容器,容器里的用户名是 root,具有完整的 root 权限;使用 docker run 命令以非特权方式创建的容器,容器里的用户名也是 root,但此 root 用户实际相当于主机上执行 docker run 命令的用户(例如普通用户 frank),容器中名为 root 的用户并不具备 root 权限,但是容器中新创建的文件属主却为 root,这样主机中的非 root 用户访问这些文件时常常权限不足,造成不便。

为解决此问题,需要将主机上的当前普通用户映射进容器中,且使容器中的普通用户同样具有执行 sudo 的权限,创建容器的命令如下:

1
2
3
4
5
6
7
docker run --user $(id -u ${USER}):$(id -g ${USER}) 
-v /etc/sudoers.centos8.2:/etc/sudoers:ro 
-v /etc/passwd:/etc/passwd:ro 
-v /etc/group:/etc/group:ro 
-v /etc/shadow:/etc/shadow:ro 
-v ${HOME}:${HOME} -w ${HOME} 
-it --name=ffmpeg-centos8.2-0.2-frank -h DK-FFMPEG-02-FRANK ffmpeg-centos8.2:0.2 bash

不同操作系统 sudoers 文件内容会有差别,例如主机系统为 openSUSE Leap 15.2,容器系统为 CentOS 8.2,那么是不能将主机系统中的 sudoers 文件映射进容器里的,起码会有警告,所以从 CentOS 8.2 系统里拷贝默认的 sudoers 文件放到主机中,重命名为 /etc/sudoers.centos8.2,此文件映射进 ffmpeg-centos8.2-0.2-frank 容器,即用此文件来设置此容器中用户的 sudo 权限。

4.2 清理磁盘空间

docker system prune

待补充

4.3 多阶段构建

利用 BuildKit 组件使用 docker 多阶段构建特性,要求 docker 版本不低于 v18.06

DOCKER_BUILDKIT=1 docker build --no-cache -f dockerfiles/dockerfile-ffmpeg-centos-multi -t ffmpeg:0.4 .

待补充

5. 修改记录

2020-07-09 初稿

Docker部署SayHello(FastAPI)

 

前言

昨天发了一个SayHello FastAPI版本,今天部署上自己的服务器了
体验地址: http://49.232.203.244:9001/message.html

服务部署

前置条件:以下在centos7.5 云服务器实验通过

yum -y install git	# 安装git
curl -sSL https://get.daocloud.io/docker | sh	# 安装docker

git clone https://gitee.com/zy7y/sayhello

git clone https://github.com/zy7y/sayhello

上面两个命令选一个执行就可了

部署后端

1. 进入到sayhello目录

cd sayhello

2. 编写API的Dockerfile(如果有请之直接构建镜像- 在下一步)

在sayhello目录下新建如下Dockerfile

FROM python:3.7
COPY . /app
WORKDIR ./app
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
EXPOSE 80
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

简单说下上面内容做了什么事情,不一定正确加了些个人理解

FROM python:3.7		# 拉取基础镜像python3.7,本机已有该镜像就会使用该镜像,没有就去远端仓库拉取,速度慢就需要换下源地址,百度即可(这里应该就是拉下镜像后弄成了个容器)
COPY . /app		# 将当前所在目录下所有文件 复制到 容器里面 /app 目录下
WORKDIR ./app	# 指定工作目录,我的理解是后面执行的命令 都相当于在这个目录下执行了,根目录的形式吧
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/	# 这步是在容器里面执行 pip 安装依赖
EXPOSE 80	# 将容器中80 端口开放
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]	# 容器运行时将执行 uvicorn main:app --host 0.0.0.0 --port 80 启动服务

3. 构建镜像

docker build -t sayhello .

4. 运行容器

会自动执行dockerfile里面的CMD命令

docker run -d --name sayhello-fastapi -p 8000:80 sayhello

5. 访问IP:8000/message,得到如下页面

部署前端

先确认message.html中的baseURL是不是后端服务的IP地址(127.0.0.1 不行)

1. 进入到sayhello/static目录

cd sayhello/static/

2. 编写Dockerfile文件(如果有请直接进入第三步)

FROM nginx:1.15.2-alpine
COPY . /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

3. 构建镜像

docker build -t sayhello-front .

4. 启动容器

docker run -d --name sayhello-front-9000 -p 9001:80 sayhello-front

5. 访问IP:9001/message.html

参考资料及感谢

感谢资料提供者/作者

  1. https://aqzt.com/bubble/6513.html
  2. https://www.cnblogs.com/tian874540961/p/11916832.html
  3. https://www.runoob.com/docker/docker-dockerfile.html
原文地址:https://www.cnblogs.com/xinxihua/p/14353016.html