notes/Ubuntu/Docker.md
2024-04-29 02:07:50 +08:00

157 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### Ubuntu Docker 安装及使用
- [安装](#安装)
- [创建镜像](#通过dockerfile文件创建镜像)
- [通过dockerfile文件创建镜像](#通过dockerfile文件创建镜像)
- [使用官网镜像](使用官网镜像)
- [常用命令](#常用命令)
- [查看镜像列表](#查看镜像列表)
- [删除镜像](#删除镜像)
- [创建容器](#创建容器)
- [查看所有容器](#查看所有容器)
- [镜像重命名](#修改重命名)
- [导入容器](#导入容器)
- [删除容器](#删除容器)
- [容器重命名](#容器重命名)
- [启动容器](#启动容器)
- [连接容器](#连接容器)
### 安装
##### 直接使用官方安装脚本自动安装即可
```
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
```
##### 测试一下docker是否安装成功
```
sudo docker help
```
##### 为 hehaoyang 用户添加权限
```
sudo usermod -aG docker hehaoyang
sudo reboot
```
### 创建镜像
#### 通过dockerfile文件创建镜像
##### 创建dockerfile文件
```
mkdir docker && cd docker
vim dockerfile
```
* **dockerfile 文件内容**
```
FROM ubuntu:18.04
MAINTAINER hehaoyang "1109196436@qq.com"
ENV DEBIAN_FRONTEND=noninteractive
RUN cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
RUN sed -i 's@http://.*ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list
RUN apt update
RUN apt install -y bash-completion vim sudo locales time rsync bc python
RUN apt update && apt install -y -f
# language support
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
# switch to a no-root user
RUN useradd -m -d /home/hehaoyang -s /bin/bash hehaoyang
RUN sed -i -e '/\%sudo/ c \%sudo ALL=(ALL) NOPASSWD: ALL' /etc/sudoers
RUN usermod -a -G sudo hehaoyang
USER hehaoyang
WORKDIR /home/hehaoyang
```
##### 创建镜像
```
docker build -t ubuntu_18_04 .
# ubuntu_18_04 是镜像名称,可随意更改,注意命令最后有一个’.
```
##### 使用官网镜像
```
docker pull ubuntu:18.04
```
### 常用命令
##### 查看镜像列表
```
docker images
```
##### 删除镜像
```
docker rmi ubuntu:18.04
```
##### 创建容器
```
# --privileged 使用该参数, 容器能够获取到主机的完整权限
# --name 设置容器名
# --mount type=bind,source=/root/target,target=/app # source= 填 SDK 所在目录target= 填容器内的一个目录,必须是空目录
# ubuntu 是上一步的镜像名
docker run --privileged --mount type=bind,source=/home/hehaoyang/works/gogs,target=/home/hehaoyang/workspace --name="gogs" -d -i -t -p 3000:3000 ubuntu_18_04
```
##### 查看所有容器
```
docker ps -a
```
##### 修改重命名
```
docker tag gogs:laster test:laster
docker rmi gogs:laster
```
#### 导入容器
```
docker load -i sg368z_ubuntu-20.04-v1.2.tar.gz
```
##### 删除容器
```
docker rm -f gogs
```
##### 容器重命名
```
docker rename mystifying_wilbur quectel-rk3568-ubuntu
```
##### 启动容器
```
docker start gogs
```
##### 连接容器
```
docker attach gogs
```
#### 导出镜像为容器
* 导出容器为镜像
```
docker commit quectel_rk3568 quectel_rk3568_ubuntu:laster
```
* 导出为压缩包
```
docker save -o docker.tar.gz quectel_rk3568_ubuntu:laster
```