Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.4 KiB

  1. Close your eyes for one moment and imagine that everything you host runs in docker containers.
  2. You no longer need to battle system dependencies, and configurations are more manageable; it is now easier to backup and transfer your applications.
  3. In my quest to dockerize everything, I am now dockerizing my Minecraft server.
  4. Minecraft is a relatively simple application to host since it is just a single Java application that you need to run.
  5. To put this in Docker, we need to declare a Java Docker image that launches our Minecraft server.
  6. ```bash
  7. FROM openjdk:8u232
  8. WORKDIR /root/minecraft
  9. CMD java -Xmx2048M -jar spigot-1.10.jar -o true
  10. ```
  11. To make this Docker container work, we need to mount the volume containing our Minecraft files to the path "/root/minecraft" in our Docker container.
  12. By mounting the volume, it enables us the persist the data between consecutive runs.
  13. To orchestrate all of my Docker containers, I am using Docker-compose.
  14. Although there is only one container for this instance, I can add additional services to this file; for example, If I had a website that went along with my server, I could use docker-compose to launch both the Minecraft server and the server's website.
  15. ```yaml
  16. version: "2.2"
  17. networks:
  18. bridge-network:
  19. external:
  20. name: bridge-network
  21. services:
  22. minecraft:
  23. restart: always
  24. image: minecraft
  25. build: ./minecraft-docker
  26. networks:
  27. - bridge-network
  28. volumes:
  29. - "./minecraft:/root/minecraft"
  30. ports:
  31. - "8123:8123"
  32. - "25565:25565"
  33. ```
  34. To expose the Minecraft server to the internet, we need to tell docker-compose the applications ports.
  35. I am using a bridged docker network along with appending the ports in the docker-compose configuration file.
  36. Port 25565 is the default Minecraft server port, and port 8123 gets used by Dynmap.
  37. To create the network bridge, you need to run this command once.
  38. ```bash
  39. docker network create -d bridge bridge-network
  40. ```
  41. We need to execute the build command to generate our new Minecraft Docker container:
  42. ```bash
  43. docker-compose build
  44. ```
  45. To launch the server, we just run the compose-up command.
  46. ```bash
  47. docker-compose up
  48. ```
  49. One quick note on the file structure that I am employing for this project:
  50. I like to keep the Dockerfiles in a directory separate from any volumes that I am mounting within those containers.
  51. Additionally, it is a good practice to have your docker-compose file at the root directory.
  52. ```bash
  53. ╭─jeff@dangerous ~/Docker
  54. ╰─$ tree -L 2
  55. .
  56. ├── docker-compose.yml
  57. ├── minecraft
  58. │ ├── banned-ips.json
  59. │ ├── banned-players.json
  60. │ ├── bukkit.yml
  61. │ ├── commands.yml
  62. │ ├── crash-reports
  63. │ ├── eula.txt
  64. │ ├── logs
  65. │ ├── ops.json
  66. │ ├── permissions.yml
  67. │ ├── plugins
  68. │ ├── run.bat
  69. │ ├── server-icon.png
  70. │ ├── server.properties
  71. │ ├── spigot-1.10.jar
  72. │ ├── spigot.yml
  73. │ ├── survival
  74. ├── minecraft-docker
  75. │ └── Dockerfile
  76. └── README.md
  77. ```
  78. Docker will scan the entire director before building a project-- Docker does this so it knows what caches it can use during an incremental build.
  79. To improve the build time, I add the Minecraft server folder to the docker-ignore file to prevent Docker from scanning all the world artifacts.
  80. ```bash
  81. # .dockerignore file
  82. minecraft
  83. ```