Installation

Install Docker Engine on Ubuntu | Docker Documentation

Ubuntu - Docker — 从入门到实践 (gitbook.io)

安装与卸载 - Docker — 从入门到实践 (gitbook.io)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo apt-get remove docker \
docker-engine \
docker.io

sudo apt-get update

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

# Install Docker Compose
sudo apt-get install docker-compose-plugin

apt 换源 (可选)

ubuntu | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

修改/etc/apt/sources.list为以上源

Docker Compose

Overview of Docker Compose | Docker Documentation

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.

Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.

用Compose运行多个容器,服务的运行通过YAML文件配置,

1
2
3
4
5
6
7
8
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"

不同的服务就是不同的镜像的容器,运行compose,会根据services创建相关的镜像,运行后,每个镜像的实例开启,构成一个project的运行环境。

  • 每个容器间如何访问呢?project开始执行后,每个容器都产生一个containerID,这个ID会在每个容器的/etc/hosts中记录下该ID号对应的docker虚拟网络中IP地址

commands

  1. docker compose down/up/stop/start 等命令执行时会通过本地路径查找docker-compose.yml配置文件
  2. compose 创建容器的命令规则:
    • 如果service的image来自远程的镜像源,名称为image:label
    • 如果通过本地Dockerfile构建,则名称来格式project_name:service_name

Uninstall Docker Engine

Install Docker Engine on Ubuntu | Docker Documentation

  1. Uninstall the Docker Engine, CLI, Containerd, and Docker Compose packages:

    1
    $ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
  2. Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:

    1
    2
    $ sudo rm -rf /var/lib/docker
    $ sudo rm -rf /var/lib/containerd

You must delete any edited configuration files manually.

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
WORKDIR /app

EXPOSE 8080

COPY . .

RUN pip install -r requirements.txt


# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "app.py"]

From: 基于某一镜像创建镜像

WORKDIR:project在微系统中的工作目录,不指定则为“/”目录

EXPOSE:表示允许暴露微系统中的指定端口号

COPY:因为服务是微系统中运行,因此要将工作目录的文件复制到微服务

RUN:构建镜像时,执行的命令,命令能否执行取决于基于的系统是否支持该命令

CMD:镜像构建完后,需要在容器中执行的命令。

RUN和CMD的区别在于后者是在镜像构建后执行的命令,此时镜像已经运行了,前者相当于在构建镜像。

Issue

1. docker容器中执行命令不能联网,通过重启docker服务解决

通过重启docker服务解决

1
sudo systemctl restart docker.service

RUN pip install -r requirements.txt 不成功可能由于镜像源

2. Docker 安装错误

可能原因是apt错误

3. Docker error: HTTP 408 response body: invalid character ‘<’ looking for beginning of value

Docker error: HTTP 408 response body: invalid character ‘<’ looking for beginning of value - IT开发屋 (itclubx.com)

  1. Docker源的问题
  2. 镜像或标签错误

4. OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: “bash”: executable file not found in $PATH: unknown

在不同的发行版中支持的shell不同,如果bash不能执行,可能可以切换成sh