# How to set up and use proxy in your server | Docker | Docker-compose | Proxy.py

I wanted to run a proxy in my server, I tried everything but still failed to set it up. I tried mitmproxy, squid, and some other proxies that I forgot, yet I somehow failed in all of them. 

After researching a little more I finally found a solution that works perfectly. [Proxy.py](https://github.com/abhinavsingh/proxy.py)

This awesome tool is written in python, has docker configurations, and HTTPS certificate generator.

So let's start.

I reckon that you are already on your server's terminal, you have `git` and `python3-virtualenv` installed.


```bash
git clone https://github.com/abhinavsingh/proxy.py.git
```

```bash
cd proxy.py
```

```bash
python3 -m venv env
. env/bin/activate
```

```bash
pip install -r requirements.txt
```

So if you run all commands so far, you should be good.

Now, this is where we do a little customization. In proxy.py docs, they are building and running containers through docker engine, but we will create our own docker-compose file for more *comfort*.

if you don't have already installed docker and docker-compose please follow these links:

for docker: [docker/download](https://docs.docker.com/get-docker/)

for docker-compose (ubuntu): [linuxize/docker-compose](https://linuxize.com/post/how-to-install-and-use-docker-compose-on-ubuntu-20-04/#installing-docker-compose-on-ubuntu) 

Let's dig in.

create a file called `docker-compose.yml` in our cloned directory (proxy.py)

```bash
nano docker-compose.yml
```

paste this context

```yml
version: "3"
   
services:
  proxy:
    build: .
    container_name: custom-proxy
    ports:
      - "8899:8899"
    restart: always
```

if you have an entry for `hosts` file you can use `extra-hosts`

here is sample
```yml
services:
  proxy:
    build: .
    container_name: custom-proxy
    ports:
      - "8899:8899"
    restart: always
    extra_hosts:
      - "example.com:1.2.3.4"
```

Let me break these lines for you

**proxy** is just the name of the service

**build** is where our Dockerfile is located (yes it's in the current directory!)

**container_name** is not necessary, I just did it because I like it that way

**ports**: we expose the ports to the public (proxy.py default is 8899)

**restart: always** is simply saying start running container again if an error occurs

That's that.

Now that we have our docker-compose file we can update and run our containers more easily! Run the following command to build and get your container up.

```bash
sudo docker-compose build
```

when this command finishes you'll have something like this

```docker
...
Step 14/15 : ENTRYPOINT [ "proxy" ]
 ---> Using cache
 ---> a18dd7a78abb
Step 15/15 : CMD [ "--hostname=0.0.0.0" ]
 ---> Using cache
 ---> 012db77b2da2
Successfully built 012db77b2da2
Successfully tagged proxypy_proxy:latest
```
to get you container up and running

```bash
sudo docker-compose up
```
<span style="color: grey;">note: add -d parameter at the end to run in the background</span>


```bash
Creating exbir-proxy ... done
Attaching to exbir-proxy
exbir-proxy | 2020-12-04 18:43:35,267 - pid:1 [I] load_plugins:334 - Loaded plugin proxy.http.proxy.HttpProxyPlugin
exbir-proxy | 2020-12-04 18:43:35,268 - pid:1 [I] listen:113 - Listening on 0.0.0.0:8899
exbir-proxy | 2020-12-04 18:43:35,277 - pid:1 [I] start_workers:136 - Started 2 workers
```

CONGRATULATIONS! your proxy works in your server on port 8899!


At this point, I have a little bonus for you. If you have your own customizations too, to make docker-compose controls easier, go ahead and make *temporary* alias for yourself. 

```bash
alias dd="sudo docker-compose down -v && sudo docker-compose build && sudo docker-compose up -d"
```

You can add it to your .bashrc (or other shells) file if you want to make it permanent.

Please ask me any questions related to this post!
