# Minecraft

# Install Minecraft on Docker Compose

## Install Mincraft using Docker Compose

The Plugins are important. like "ViaVersion" as it it be what lets you cross platforms to switch and other consoles.

```yaml
services:
  minecraft:
    image: itzg/minecraft-server:java21
    dns:
      - 8.8.8.8
      - 8.8.4.4
    environment:
      UID: "1000"
      GID: "1000"
      EULA: "TRUE"
      TYPE: "PAPER"
      VERSION: "1.21.1"
      PAPER_CHANNEL: "default"
      PLUGINS: |
        https://download.geysermc.org/v2/projects/geyser/versions/latest/builds/latest/downloads/spigot
        https://download.geysermc.org/v2/projects/floodgate/versions/latest/builds/latest/downloads/spigot
        https://github.com/ViaVersion/ViaVersion/releases/download/5.6.0/ViaVersion-5.6.0.jar
    ports:
      - "25565:25565" # Java Edition
      - "19132:19132/udp" # Bedrock Edition (Switch, consoles, mobile)
    volumes:
      - ./data:/data
    restart: unless-stopped
```

Got the container running? Good. Next step...

### Test it

```bash
sudo docker logs <container_name>
```

You can also use

```bash
sudo docker logs minecraft-minecraft-1 | grep "logged in with entity id"
```

Check for errors and see what it says.

### Using hose / .service to open and close ports AND start and stock docker.

This is going to allow you to open up the ports to your sever using UFW only when you start it up. Then it will close them, when you stop the service.

Go to:

```
sudo vim /etc/systemd/system/minecraft.service
```

You may not have that file yet. if not, just create it new and paste in the below.

You will need to change these directories to match where your data is stored.   
WorkingDirectory=/opt/minecraft  
ExecStart=/usr/bin/docker compose up -d  
ExecStop=/usr/bin/docker compose down

```bash
[Unit]
Description=Minecraft Server (Docker)
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/minecraft

ExecStartPre=/usr/sbin/ufw allow 25565/tcp
ExecStartPre=/usr/sbin/ufw allow 19132/udp
ExecStart=/usr/bin/docker compose up -d

ExecStop=/usr/bin/docker compose down
ExecStopPost=/usr/sbin/ufw delete allow 25565/tcp
ExecStopPost=/usr/sbin/ufw delete allow 19132/udp

[Install]
WantedBy=multi-user.target

```

### To start and stock your docker container

You can use the below to start and stop your docker container, instead of going through your docker-compose.yml file.

sudo systemctl start minecraft  
sudo systemctl stop minecraft  
sudo systemctl status minecraft