如何用Docker容器部署应用
自从用了Docker之后,部署应用真的是简单很多,再也不用单独安装很多依赖。
近期部署大模型的应用如Dify AI,Flowise等,发现很多应用是用Docker部署的,相比源码部署要简单很多。Dify的安装只需要4步:
# Clone the Dify source code to your local machine:
git clone https://github.com/langgenius/dify.git
# Navigate to the Docker directory in the Dify source code
cd dify/docker
# Copy the environment configuration file
cp .env.example .env
# Start the Docker containers
docker compose up -d
同样Flowise的安装也只需要4 步:
# Clone the Flowise source code to your local machine:
git clone https://github.com/FlowiseAI/Flowise.git
# Navigate to the Docker directory in the Dify source code
cd Flowise/docker
# Copy the environment configuration file
cp .env.example .env
# Start the Docker containers
docker compose up -d
什么是Docker?
Docker is a tool that is used to automate the deployment of applications in lightweight containers so that applications can work efficiently in different environments in isolation.
Docker本质是上一种Container Orchestration System,能够把每个软件独立安装与运行在一个容器里面。类似的产品还有Kubernetes,Openshift。使用Docker不仅从能够解决部署应用的兼容性问题,而且还能提升部署的效率。比如在Mac上安装一个PostgreSQL的数据库,常规的安装步骤如下:
# Install PostgreSQL
brew install postgresql
# Start PostgreSQL as a background service
brew services start postgresql
# Access the PostgreSQL shell
psql postgres
# Create a new user
CREATE USER myuser WITH PASSWORD 'mypassword'
# Give the user superuser privileges
ALTER USER myuser WITH SUPERUSER;
# Exit the shell
\q
如果使用Docker,安装步骤可以简化为一个指令:
docker run --name my-postgres \
-e POSTGRES_USER=myuser \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=mydatabase \
-p 5432:5432 \
-d postgres
Docker常用的命令有哪些?
1. Running a Container
Docker最常用的命令是运行一个容器。根据官方文档的描述:The docker run
command runs a command in a new container, pulling the image if needed and starting the container. 命令格式如下:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
• IMAGE
specifies the Docker image to use, optionally with a tag or digest to specify the version.
• COMMAND
and ARG...
are optional commands and arguments to run inside the container, overriding the default image command.
完全了解这个命令会非常复杂,建议运行具体的容器时,通过ChatGPT查找相应的命令。例如以下回复:
docker run command
Here’s a quick reference to the docker run command — with examples for different common use cases:
1. Run a container interactively
docker run -it ubuntu bash
- -i: Interactive
- -t: Allocate a pseudo-TTY
- ubuntu: Image name
- bash: Command to run inside
2. Run a container in the background (detached)
docker run -d nginx
- -d: Detached mode (runs in background)
3. Map ports
docker run -p 8080:80 nginx
- Maps port 80 inside the container to port 8080 on your host
4. Mount a volume
docker run -v /host/path:/container/path ubuntu
- Mounts a host directory into the container
5. Assign a name
docker run --name my-container nginx
6. Auto-remove container on exit
docker run --rm ubuntu
运行了容器之后,可以通过docker ps
来查看运行的容器。In docker ps
, the ps
stands for “process status” — just like the traditional Linux/Unix ps command that shows running processes.
2. Managing Docker Images
Docker运行一个容器的时候经常要拉取镜像。如果你需要管理Docker本地的镜像,可以通过docker image
命令:
Common docker image
Subcommands and Their Functions
docker image ls
(ordocker images
): Lists all Docker images available locally, showing details like repository, tag, image ID, creation date, and size.docker image pull
: Downloads an image from a Docker registry to the local system.docker image push
: Uploads an image from the local system to a Docker registry.docker image rm
: Removes one or more images from the local system.docker image inspect
: Displays detailed metadata and configuration information about one or more images.docker image build
: Builds a new image from a Dockerfile.docker image save
: Saves one or more images to a tar archive.docker image load
: Loads an image from a tar archive or STDIN.docker image tag
: Creates a new tag for an existing image.docker image prune
: Removes unused images to free up disk space.docker image history
: Shows the history of an image’s layers.docker image import
: Imports an image from a tarball to create a filesystem image.