Docker 使用笔记

in Technique with 0 comment and 121 read

安装

Ubuntu

依赖

apt update
apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y

仓库

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

主体

apt update 
apt install docker-ce docker-ce-cli containerd.io -y

卸载

apt purge docker-ce
apt autoremove

CentOS

依赖

yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

仓库

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

主体

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

卸载

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

rm -rf /etc/systemd/system/docker.service.d
rm -rf /var/lib/docker
rm -rf /var/run/docker

启动及开机启动

systemctl start docker
systemctl enable docker

以非 Root 用户身份执行

sudo usermod -aG docker $USER

验证

docker run hello-world

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

赋权

chmod +x /usr/local/bin/docker-compose

版本

docker-compose --version

常用命令

容器

# 启动容器
docker start Name/ID

# 停止容器
docker stop Name/ID

# 杀死容器
docker kill Name/ID

# 删除容器
docker rm Name/ID

# 显示正在运行的容器:
docker ps

# 显示全部容器:
docker ps -a

# 进入容器
docker exec -it NAME /bin/sh
or
docker exec -it ID /bin/sh

镜像

# 检索 image
docker search image-name

# 下载 image
docker pull image-name

# 显示全部镜像:
docker images

# 删除镜像
docker rmi ID

保存和加载镜像

# 保存镜像到一个 tar 包
docker save image-name -o file-path

# 加载一个 tar 包格式的镜像
docker load -i file-path