御风灰灰
发布于 2024-07-11 / 18 阅读
0
0

ubuntu下docker安装

1. 配置源

配置源之前需要安装以下软件包

sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

1.1 docker官方源

  1. 添加apt源

    sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu  \
    $(lsb\_release -cs) \ 
    stable"
    
  2. 添加官方GPG密钥

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

1.2 清华源

apt源请选择当前芯片架构的

  1. 添加amd64的apt源

    sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    
  2. 添加arm64的apt源

    sudo add-apt-repository \
    "deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    
  3. 添加清华源GPG密钥

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

1.3 阿里云仓库

  1. 添加apt源

    sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    
  2. 添加阿里云GPG密钥

    
    

2. 安装docker环境

2.1 安装docker-ce

sudo apt install docker-ce

2.2 docker-compose安装

切换到sudo权限下运行

# 下载程序
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose


#增加执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 查看版本
docker-compose version

2.3 非root用户运行

# 增加到docker组
sudo usermod -aG docker $USER
# 刷新组
newgrp docker
# 重启docker
sudo systemctl restart docker

2.4 验证测试

  1. 启动测试程序
sudo docker run hello-world
  1. 运行日志如下
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

3. 常用命令

3.1 启动参数说明

命令 参数说明
-t 支持交互
-i t伪终端
--user 1000:1000 指定用户的gid和uid
-w /home/jello 指定容器内的工作空间
--privileged 指定特权模式

3.2 容器控制命令

  1. 重启所有容器
docker restart $(docker ps -a -q)
  1. 启动所有的容器命令
docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
  1. 关闭所有容器
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  1. 删除所有的容器
docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
  1. 查看容器
docker ps
  1. 启动单个容器

b82bacbff7c5是容器id

docker start b82bacbff7c5
  1. 停止单个容器

b82bacbff7c5是容器id

docker stop  b82bacbff7c5
  1. 重启单个容器

b82bacbff7c5是容器id

docker restart b82bacbff7c5

3.3 镜像类操作

  1. 删除所有的镜像
docker rmi $(docker p_w_picpaths | awk '{print $3}' |tail -n +2)
  1. 容器保存为镜像
docker commit -m "description" -a "author" <容器id> repository:tag
  • “description”:描述信息。
  • “author”:作者名。
  • <容器id>:可以在就终端那里看到,形如root@2ce6712ef339:/#。可以看到<容器id>是2ce6712ef339。
  • reposiory:镜像仓库名,任取即可。
  • tag:镜像标签名,任取即可。

3.4 镜像导出与导入

  1. 镜像加载与保存

若是只想备份images,使用save、load即可
若是在启动容器后,容器内容有变化,需要备份,则使用export、import

docker load -i nginx.tar
docker save -o nginx.tar nginx:latest

docker export -o nginx-test.tar nginx-test
docker import nginx-test.tar nginx:imp
  • export命令导出的tar文件略小于save命令导出的
  • export命令是从容器(container)中导出tar文件,而save命令则是从镜像(images)中导出
  • 基于第二点,export导出的文件再import回去时,无法保留镜像所有历史(即每一层layer信息,不熟悉的可以去看Dockerfile),不能进行回滚操作;而save是依据镜像来的,所以导入时可以完整保留下每一层layer信息。如下图所示,nginx:latest是save导出load导入的,nginx:imp是export导出import导入的

评论