Deploy IPFS with Nginx in Docker

2024-03-19 / IPFS

This post is mainly to help reader to deploy an IPFS service with nginx proxy which provides basic authorizations.

A quick start is the following steps.

Some basic requirements:

  • Docker should already be installed on your computer.
  • Basic Linux terminal skills.

Create files

  1. Create folder

    $ mkdir ipfs
    $ cd ipfs
    
  2. Create docker-compose.yaml

    version: '3'
    
    services:
      ipfs:
        container_name: ipfs
        image: ipfs/kubo:latest
        volumes:
          - ./data/:/data/ipfs/
        ports:
          - '8080:8080'
        restart: always
      nginx:
        container_name: nginx
        image: nginx:latest
        ports:
          - '5001:5001'
        volumes:
          - './default.conf:/etc/nginx/conf.d/default.conf:ro'
          - './htpasswd:/htpasswd:ro'
        restart: always
    
  3. Create htpasswd

    ljiang:$apr1$nShUpvO7$hizri44cnQwto/zQ0y3Ka0
    
  4. Create default.conf

    server {
        listen 5001;
        server_name _;
        client_max_body_size 2048000m;
        location / {
            proxy_pass http://ipfs:5001;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            auth_basic           "Administrator’s Area";
            auth_basic_user_file /htpasswd;
        }
    }
    

Init IPFS Data

  1. Make data folder

    $ mkdir -p ./data/
    
  2. Init data

    $ IPFS_PATH="$(pwd)/data/" ipfs init
    

Set IPFS config

  1. Set Addresses

    $ IPFS_PATH="$(pwd)/data/" ipfs config --json Addresses.API '"/ip4/0.0.0.0/tcp/5001"'
    $ IPFS_PATH="$(pwd)/data/" ipfs config --json Addresses.Gateway '"/ip4/0.0.0.0/tcp/8080"'
    
  2. Set API

    $ IPFS_PATH="$(pwd)/data/" ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
    $ IPFS_PATH="$(pwd)/data/" ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "PUT", "POST"]'
    

Start IPFS

$ docker-compose up -d