Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker常用命令集合 #15

Open
dalaolala opened this issue Feb 5, 2020 · 12 comments
Open

docker常用命令集合 #15

dalaolala opened this issue Feb 5, 2020 · 12 comments

Comments

@dalaolala
Copy link
Owner

dalaolala commented Feb 5, 2020

#安装以及启动docker


curl -sSL https://get.docker.com/ | sh
service docker restart

国内机器安装docker

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

#容器镜像删除
必须先停止再删除


docker stop $(docker ps -a -q) 
docker rm $(docker ps -a -q)
docker images
docker rmi <image id>

#上传镜像到hub docker
点这里申请一个docker hub 帐号,
1、登录到dockehub点击一下按钮:create —> create repository

2、为存在于本地的镜像打标签,命令如下:
docker tag /[:]
这里的tag不指定就是latest,例如:


docker tag aaa/imagesa bbb/imagesb:V1.0

3、在本地登录docker hub 帐号,命令如下:


root@master:~# docker  login
Username: lidnyun
Password:
Email: 邮箱地址
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded

4、push镜像,命令如下:

docker push <hub-user>/<repo-name>:<tag>
例如:
docker push bbb/imagesb:V1.0

5、下载镜像

docker pull bbb/imagesb:V1.0

#打包以及使用本地镜像


打包
docker save -o /root/demo.tar bbb/imagesb:V1.0
使用
docker load -i /root/demo.tar

#创建TG MTProto
创建后找 MTProto Admin Bot注册


docker run -d -p 443:443 --name=mtproto --ulimit nofile=98304:98304 --restart=always -v proxy-config:/data -e TAG=fa030b8ba9a6a099c45328d67889c76a  -e SECRET=fa030b8ba9a6a027c45328d67889c76a telegrammessenger/proxy:latest

#安装Docker-Compose


curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod a+x /usr/local/bin/docker-compose
# 创建个软链接,以后用 dc 命令来代替 docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/dc

国内镜像安装参考如下

sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

#重新编译镜像文件

进入到镜像源码目录,然后执行
docker build -t bbb/gmirror:v1 .

#生成某搜索镜像


docker run -p 80:80 -d bohan/onemirror

另外附上一个常见命令集合点这里

#进入到docker里面的命令

sudo docker ps
sudo docker exec -it 775c7c9ee1e1 /bin/bash 

#进入docker后无法使用vim命令

apt-get update
apt-get install vim

#docker进入容器内部

docker exec -it alade /bin/bash
@dalaolala
Copy link
Owner Author

dalaolala commented Oct 30, 2021

解决国内安装docker太慢

1.卸载旧版本

sudo yum remove docker \
          docker-client \
          docker-client-latest \
          docker-common \
          docker-latest \
          docker-latest-logrotate \
          docker-logrotate \
          docker-engine

2.安装需要的软件包

sudo yum install -y yum-utils

3、添加镜像信息

#官方给的镜像
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#建议使用阿里云的镜像进行加速,要不太慢了
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

4、安装DOCKER CE

yum install docker-ce docker-ce-cli containerd.io

5、配置国内源

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
  "https://kfwkfulq.mirror.aliyuncs.com",
  "https://2lqq34jg.mirror.aliyuncs.com",
  "https://pee6w651.mirror.aliyuncs.com",
  "https://registry.docker-cn.com",
  "http://hub-mirror.c.163.com"
  ],
  "dns": ["8.8.8.8","8.8.4.4"]
}
EOF
systemctl daemon-reload
systemctl restart docker

执行完上述几步,Docker就安装完成了

6、另外一种安装docker的方法

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

20230703更新镜像地址

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://dockerproxy.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://docker.nju.edu.cn"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

@dalaolala
Copy link
Owner Author

dalaolala commented Apr 19, 2022

修改docker存放目录

默认存放位置

docker info | grep "Docker Root Dir"

停掉Docker服务

service docker stop

移动原有的内容

mv /var/lib/docker /data/docker

进行链接

ln -sf /data/docker /var/lib/docker

指定容器启动参数

# CentOS6
# 因为Ubuntu默认开启了selinux机制
OPTIONS=--graph="/data/docker" --selinux-enabled -H fd://

# CentOS7
# 修改docker.service文件,使用-g参数指定存储位置
$ vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --graph /new-path/docker
# Ubuntu
# 因为Ubuntu默认没开启selinux机制
OPTIONS=--graph="/data/docker" -H fd://

指定容器启动参数 2023年更新,上面如法启动增加下列即可

ExecStartPost=/bin/mkdir -p /data/docker

重新reload配置文件

 systemctl daemon-reload

重启docker服务

service docker start

方法二

直接更改配置文件,例如

vi /etc/docker/daemon.json

{
  "data-root": "/data/docker-data",
  "insecure-registries": ["127.0.0.1:5000"]
}

@dalaolala
Copy link
Owner Author

配置国内加速源 2022年更新

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://t4d1ci2w.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

@dalaolala
Copy link
Owner Author

dalaolala commented Nov 12, 2022

一键搭建zookeeper

docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /root/docker/zookeeper:/data --name zookeeper --restart always zookeeper

配置说明

 -e TZ="Asia/Shanghai" # 指定上海时区 
 -d # 表示在一直在后台运行容器
 -p 2181:2181 # 对端口进行映射,将本地2181端口映射到容器内部的2181端口
 --name # 设置创建的容器名称
 -v # 将本地目录(文件)挂载到容器指定目录;
 --restart always #始终重新启动zookeeper

参考:https://www.shouxicto.com/article/5905.html

@dalaolala
Copy link
Owner Author

dalaolala commented Nov 23, 2022

docker ps相关

只显示部分字段名称
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}\\t{{.Status}}"

docker image相关

删除所有没使用的image
docker image prune -a

@dalaolala
Copy link
Owner Author

dalaolala commented Apr 9, 2023

docker安装脚本版

#!/bin/bash
# remove old version
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

# remove all docker data 
sudo rm -rf /var/lib/docker

#  preinstall utils 
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

# add repository
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

# make cache
sudo yum makecache fast

# install the latest stable version of docker
sudo yum install -y docker-ce

# start deamon and enable auto start when power on
sudo systemctl start docker
sudo systemctl enable docker

# add current user 
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker

国内网络

#!/bin/bash
# 移除掉旧的版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

# 删除所有旧的数据
sudo rm -rf /var/lib/docker

#  安装依赖包
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

# 添加源,使用了阿里云镜像
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 配置缓存
sudo yum makecache fast

# 安装最新稳定版本的docker
sudo yum install -y docker-ce

# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF

# 启动docker引擎并设置开机启动
sudo systemctl start docker
sudo systemctl enable docker

# 配置当前用户对docker的执行权限
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker

@dalaolala
Copy link
Owner Author

dalaolala commented Apr 9, 2023

安装最新版docker-compose

curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose

一键安装docker-compose

curl -L "https://github.com/docker/compose/releases/download/1.26.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

@dalaolala
Copy link
Owner Author

dalaolala commented May 9, 2023

安装稳定版
curl -sSL https://get.docker.com/ | CHANNEL=stable bash

安装docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

加速安装

sudo curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

@dalaolala
Copy link
Owner Author

docker常见的工具集合

https://github.com/einverne/dockerfile

@dalaolala
Copy link
Owner Author

通过守护镜像自动更新镜像脚本
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower -cR

@dalaolala
Copy link
Owner Author

docker-compose 下载最新版
curl -L "https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod a+x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

@dalaolala
Copy link
Owner Author

dalaolala commented Jul 8, 2024

配置镜像加速,解决屏蔽问题

vi /etc/docker/daemon.json

配置以下内容

{ 
  "registry-mirrors" : 
    [ 
      "https://docker.1panel.live", 
      "https://noohub.ru", 
      "https://docker.zhai.cm",
      "https://dockerhub.timeweb.cloud" 
    ] 
}

国内的机器安装问题解决

curl -fsSL https://gitee.com/tech-shrimp/docker_installer/releases/download/latest/linux.sh| bash -s docker --mirror Aliyun

参考 https://github.com/tech-shrimp/docker_installer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant