SoFunction
Updated on 2025-04-11

Two methods of installing Dify in Docker containers

If Docker is installed on Windows, you can use the Docker container to install Dify:

1. Method 1

1. Pull Dify image

Open PowerShell or Command Prompt (CMD) and run the following command to pull the Dify image from Docker Hub (the command line is found in Docker Hub):

docker pull designthru2019/dify:56c6d1af0944dbdb5e0115cb623ff0e118a4ac62

This command downloads the latest image of Dify from Docker Hub, and the download time depends on the network status.

2. Configure environment variables

Before running Dify, you need to configure some necessary environment variables, such as database connection information, keys, etc. Can create a.envFiles to store these environment variables, the example content is as follows:

# Database configurationDATABASE_URL=postgresql://user:password@localhost:5432/dify
# Key ConfigurationSECRET_KEY=your_secret_key

You need to modify these values ​​according to actual conditions, such as setting the database username, password, port and other information. Save the above as.envand place the file in a suitable directory, and will be used when starting the container later.

3. Prepare the database

Dify requires a database to store data. Taking PostgreSQL as an example, you can run a PostgreSQL container through Docker:

docker run -d \
  --name postgres-dify \
  -e POSTGRES_USER=user \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DB=dify \
  -p 5432:5432 \
  postgres

This command creates and launches a namedpostgres-difyPostgreSQL container, ensure.envIn the fileDATABASE_URLConsistent with the configuration here.

4. Run the Dify container

In PowerShell or command prompt, start the Dify container with the following command:

docker run -d \
  -p 3000:3000 \
  --env-file .env \
  difyhub/dify

Parameter description:

  • -d: means running the container in daemon mode, that is, the container is running in the background.
  • -p 3000:3000: Map the 3000 port inside the container to the 3000 port of the host, so that the Dify application can be accessed through the host's browser.
  • --env-file .env: Specify the previously created.envfile, passing environment variables into the container.

5. Access the Dify app

Open the browser and accesshttp://localhost:3000, If everything is configured correctly, you should be able to see the login or registration page of Dify. Follow the page prompts to start using Dify.

  • Database initialization: When using PostgreSQL for the first time, some initialization operations may be required, such as creating table structures.
  • Port conflict: Ensure that the host's port 3000 and port 5432 are not occupied by other applications, otherwise the port mapping parameters can be modified.
  • Network issues: If you encounter network problems when pulling images or accessing applications, you need to check the network settings or configure the proxy.

2. Method 2

1. Install the necessary tools

Before you begin, make sure your system has the following tools installed:

  • Git: Used to clone the Dify source code repository. CanGit official websiteDownload and install the version that suits your system. After the installation is complete, enter the command linegit --versionVerify that the installation is successful.
  • Docker: Used to create and manage containers. Available fromDocker official websiteDownload and install Docker Desktop (for Windows and macOS), or install it according to the official documentation for the corresponding Linux distribution. After the installation is complete, enterdocker --versionCheck whether the installation is successful.
  • Docker Compose: Used to define and run multi-container Docker applications. Can be passeddocker compose versionCheck whether it has been installed. If it is not installed, please refer toDocker Compose official installation documentationPerform installation.

2.Clone the Dify source code to the local environment

git clone /langgenius/

- Make sure your network has normal access to GitHub. If there are restrictions on the network, you may need to configure a proxy. For example, the command to configure an HTTP proxy in Git is:

git config --global  :portgit config --global  :port

- The cloning process may take some time, depending on your network speed and repository size.

3. Enter the diify source code docker directory

cd dify/docker

- Confirm that your current working directory is correctly switched to the `dify/docker` directory. You can use the `pwd` command to view the current working directory.

4. Copy and rename the configuration file

cp . .env

- The `.env` file contains various environment variables required for Dify to run, such as database connection information, keys, etc. After the copy is completed, the configuration items in the `.env` file need to be modified according to the actual situation. Here are some common configuration items and their descriptions:

# Database connection informationDATABASE_URL=postgresql://user:password@localhost:5432/dify
# Modify to your own database username, password, host address, port and database name# Key ConfigurationSECRET_KEY=your_secret_key
# Generate a secure random key, which can be generated using Python command: python -c 'import secrets; print(secrets.token_hex(32))'# Other configuration items are modified as needed

5. Start Docker Compose

Depending on the Docker Compose version on your system, select the appropriate command to start the container.

docker compose up -d
- **Version compatibility**:If yours Docker Compose Older version,May be required `docker-compose up -d` Order。Can be passed `docker compose version` View version information。
- **Container startup order**:Docker Compose Will follow `` Start each container in the order defined in the file。If there is a dependency between containers,like Dify Rely on database services,Make sure the database container is up and running normally first。
- **Log viewing**:like果容器启动失败,Available `docker compose logs` Order查看容器的日志信息,To troubleshoot problems。例like,View logs for all containers:
docker compose logs -f

- **Resource Occupancy**: Starting multiple containers may take up more system resources, ensuring that your system has enough memory and CPU resources to run these containers.

6. Verify the installation

Open the browser and accesshttp://localhost:3000, if everything is configured correctly, you should be able to see the Dify login or registration page.

7. Follow-up maintenance and updates

Stop the container: If you need to stop the Dify container, you candify/dockerRun the following command in the directory:

docker compose down

Update code: If you need to update Dify to the latest version, you candifyRun the following command in the directory to pull the latest code and restart the container:

git pulldocker compose up -d --build

Now you should be able to install and run Dify locally using Method 2.

Comparison of the two

Both methods are feasible, but they are different in terms of operation and application scenarios.

Method 1

Use Docker directly to pull the official Dify image and run the container. This method is relatively simple and fast, suitable for users who want to quickly experience the Dify function and do not make in-depth modifications or customizations to the source code.

Method 2

Use Docker Compose to start the container by cloning the Dify source code. This method is more suitable for developers, who may need to modify, debug the source code of Dify, or customize the development according to their own needs.

Things to note

Method 1

  • Environment variable configuration: The configuration information in the .env file must be accurate, especially the database connection information. If the database is configured incorrectly, the Dify container will not be able to connect to the database normally, resulting in a failed startup.
  • Database dependencies: You need to ensure that the database service is running normally, and that the permissions and configuration of the database are consistent with the settings in the .env file. For example, after the PostgreSQL container is started, you want to confirm that the database username, password, and database name matches the DATABASE_URL in the configuration file.
  • Port conflict: Ensure that port 3000 on the host and the port used by the database (such as 5432) are not occupied by other applications, otherwise the Dify container or database container will not start normally.

Method 2

  • Git environment: You need to make sure that Git is installed on the system and that you can access the GitHub website normally. If there are restrictions on the network, you may need to configure a proxy to clone the source code.
  • Docker Compose version: There may be some subtle differences in different versions of Docker Compose, and you need to select the appropriate command based on the version installed on your system. For example, an older version might use the docker-compose command, while a newer version might use the docker compose command.
  • Configuration file modification: After copying and renaming the . file to .env, the configuration information in it needs to be modified according to the actual situation, such as database connection information, keys, etc., otherwise the container startup may fail.

Which method is better

  • If you just want to quickly experience the features of Dify: It is recommended to use it. This method is simple to operate, does not require cloning the source code, it only needs to pull the official image and run the container, which allows you to use Dify in a short time.
  • If you are a developer, you need to customize Dify or debug it: this is recommended to use method 2. By cloning the source code, you can modify and debug the code, and using Docker Compose to manage dependencies and configurations between multiple containers more easily.

This is the end of this article about the two methods of Docker container installation Dify. For more related Docker container installation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!