最近入门了一下docker,主要是因为交互题/环境题越来越多,赛后复现不得不用docker搭环境(不想学也得学了
试了一些指令和题目,也踩过一些坑
之后学习过程中发现新的东西也会记录下来吧
感谢指点我的生蚝和haxo还有空白
安装Docker
apt-get install docker.io
启动docker
systemctl start docker
关闭docker
systemctl stop docker
重启docker
systemctl restart docker
自动启动docker
systemctl enable docker
查看docker运行状态
systemctl status docker
docker开启时
输入ifconfig会发现新增了名为docker0的设备
nc docker0Ip 端口 即可链接题目
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-35.png)
Docker常用命令
查看docker配置和信息
docker info
查看docker版本
docker version
列出所有容器(正在运行)
docker ps
#CONTAINER ID: 容器 ID
#IMAGE: 使用的镜像
#COMMAND: 启动容器时运行的命令
#CREATED: 容器的创建时间
#STATUS: 容器状态(7种)
#PORTS: 容器的端口信息和使用的连接类型(tcp\udp)
#NAMES: 自动分配的容器名称
#STATUS:
created(已创建)
restarting(重启中)
running(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-40.png)
查看所有运行中的容器状态
docker stats
进入容器
docker exec imagename/imageID
列出所有镜像
docker images
#REPOSITORY:表示镜像的仓库源
#TAG: 镜像的标签版本号
#IMAGE ID: 镜像ID
#CREATED: 镜像创建时间
#SIZE: 镜像大小
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-39.png)
搜索镜像
docker search imagename
拉取镜像
docker pull imagename
#后面加:tag 是拉取指定版本的镜像
运行容器
docker run -it -d --imagename
# -it 表示 与容器进行交互式启动
#-d 表示可后台运行容器
运行已存在的容器
docker start imagename/imageID
停止容器运行
docker stop imagename/imageID
强制停止容器运行
docker kill imagename/imageID
重启容器
docker restart imagename/imageID
删除容器
docker rm -f imagename/imageID
查看容器运行日志
docker logs -f --tail=查看末尾多少行 iamgeID
#默认是查看全部
dockerfile
从dockerfile中生成镜像
docker build . -t 'imagesID'
“.”表示当前目录下
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-36.png)
运行镜像
docker run -d -p host port:docker port images
docker run -it -p 1229:9999 crazy
#-e 设置环境变量
#-d 后台运行,返回容器ID
#-P(大写) 随机端口映射,Docker将随机分配映射的端口
#-p(小写) 将容器的端口映射到主机的端口
#-it 为容器分配一个交互式伪终端
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-34.png)
dockfile命令
FROM 构建新镜像基于的基础镜像
LABEL 标签
RUN 构建镜像时运行的Shell命令
COPY 拷贝文件或目录到镜像中
ADD 解压压缩包并拷贝
ENV 设置环境变量
USER 为RUN、CMD和ENTRYPOINT执行命令指定运行用户
EXPOSE 声明容器运行的服务端口
WORKDIR 为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录
CMD 运行容器时默认执行,如果有多个CMD指令,最后一个生效
FROM ubuntu:20.04
#FROM指令必须是dockerfile中非注释行的第一个指令
#比如这个指定了基于ubuntu20.04的镜像
# FROM php:7.4-apache
#这个镜像基于PHP7.4和Apache服务器,可以用于运行PHP应用程序,并提供Apache作为Web服务器。
ENV DEBIAN_FRONTEND noninteractive
#设置环境变量 DEBIAN_FRONTEND,值设为 noninteractive。这样设置的目的是使 Debian 包管理器(如 apt-get)以非交互模式运行
#这样在安装软件包时将不会提示用户进行交互式选择,而是使用默认选项。
(待续)
遇到的坑点:
(1)执行
docker build . -t 'imagename'
出现:unable to prepare context: unable to evaluate symlinks in dockerfile path: FindFirstFile \path\dockerfile: The system cannot find the file specified.
需要将.txt后缀去掉
![](https://kkkkkkkotori.top/wp-content/uploads/2024/02/image-33.png)
(2)非root用户执行docker命令时
出现:Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/images/json: dial unix /var/run/docker.sock: connect: permission denied
需要命令前加上 sudo
或者换个方法,创建一个docker用户组,把使用的用户加入进去
sudo cat /etc/group #查看当前系统有哪些用户组
sudo groupadd docker #添加docker用户组
sudo gpasswd -a $ID docker #检测当前用户是否已经在docker用户组中,ID是用户名。
sudo gpasswd -a $USER docker #将当前用户添加至docker用户组
newgrp docker #更新docker用户组
dockerHUB
待续
Comments | NOTHING