How To: Run An OSS Wordle Clone With Docker Compose
Tags: devops, docker, tutorial, beginners
Published: 2023-07-27 19:31:28

<a href="https://www.nytimes.com/games/wordle/index.html" target="_blank">Wordle</a> took the internet by storm after its release in late 2021. For many, it’s still a morning ritual that pairs seamlessly with a cup of coffee and the start of a work day. As a DevOps engineer, is there a single better way to warm up your mind other than puzzling out a <a href="https://docs.docker.com/compose/" target="_blank">Docker Compose</a> file and then indulging in the world’s favorite word game? Well, the jury’s still out on that one, but this tutorial can let you see for yourself.

### Why Write a Docker Compose File?

Even in an application with a single Dockerfile, a Docker Compose file can be a useful asset. Working from a Dockerfile often requires lengthy `build` and `run` commands, which can be migrated into a Compose file. This way, you aren't copying and pasting complex commands on every new build. Instead, your entire application builds and runs with just `docker compose up`. This is even more valuable when using an application with multiple Dockerfiles: you no longer need to individually build and run each Dockerfile.

There are still countless apps that are Dockerized but lack Compose files. When working on Shipyard's <a href="https://shipyard.build/blog/shipyard-presents-docker-compose-community-spotlight/" target="_blank">Docker Compose Community Spotlight Series</a>, I specifically looked for apps that came pre-packaged with Compose files. This was to emphasize some of the cool things you can do with Docker Compose, as well as to show how easy Compose makes app development. However, when it comes to writing a Compose file from scratch, it's easy to get intimidated by aspects such as container networking, volume mounts, or getting a service definition correct. If you're new to Docker Compose, there is nothing to worry about: most of your first Compose file will resemble your Docker `build` and `run` commands.

## Using Compose To Run A Dockerized Wordle Clone

There’s an excellent open source React-based Wordle clone on GitHub. It has approximately one hundred contributors, and over two thousand users have forked it to put their own spin on the modern classic web game. This repo comes equipped with a Dockerfile, allowing you to run it in a container on your local machine.

![React Wordle in action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8u7f7z6n8fusds52hv7.png)

<a href="https://reactle.vercel.app/" target="_blank">Try out the demo here.</a>

It'll take us a matter of minutes to get it up and running with Docker Compose.

### Step 1: Fork React-Wordle from GitHub
Start out by forking the <a href="https://github.com/cwackerfuss/react-wordle" target="_blank">react-wordle repo</a> from GitHub to your local machine. I created a branch based on **main** called **add-docker-compose** so I can make multiple commits without cluttering my main branch's git log.

The repo provides the following Docker commands to build and run the image:

```zsh
docker build -t reactle:dev -f docker/Dockerfile .
docker run -d -p 3000:3000 --name reactle-dev reactle:dev
```
We'll use these commands to populate our Docker Compose file in the next step.

### Step 2: Craft A Compose File
We can get this repo deploying with the addition of a simple, single-service Docker Compose file. Open your text editor or IDE of choice and create a `docker-compose.yaml` file in your forked app’s root directory.

First, let’s set the Compose version and define a service based off of our single Dockerfile, which we’ll call **reactle**:

```yaml
version: '3.8'
services:
    reactle: 
```

Now we’ll want to build from the existing Dockerfile. In this repo, it's stored in the **docker** directory, so we’ll include this path in our Compose definition. Since all files required for this app are stored immediately in the root directory, we'll set our **build context** to the app's root.

I set the container’s port to `3000`, which is standard for development.
```yaml
version: '3.8'
services:
    reactle:
        build:
            context: .
            dockerfile: docker/Dockerfile
        ports:
            - '3000:3000'
```

This app’s resources live in a couple of directories and files, as specified by the Dockerfile. We can list their paths under the **volumes** label so the container can access them. Each volume is formatted as the path within the repo (`./src`) followed by a colon, and then the corresponding mount point within the container (`/app/src`).

```yaml
version: '3.8'
services:
    reactle:
        build:
            context: .
            dockerfile: docker/Dockerfile
        ports:
            - '3000:3000'
        volumes:
            - './src:/app/src'
            - './public:/app/public'
            - './package-lock.json:/app/package-lock.json'
            - './package.json:/app/package.json'
```

If we want to make this app <a href="https://shipyard.build" target="_blank">Shipyard</a>-compatible, we just need to add one more label to the Compose file:
```yaml
version: '3.8'
services:
    reactle:
        build:
            context: .
            dockerfile: docker/Dockerfile
        labels:
            shipyard.route: `/`
        ports:
            - '3000:3000'
        volumes:
            - './src:/app/src'
            - './public:/app/public'
            - './package-lock.json:/app/package-lock.json'
            - './package.json:/app/package.json'
```

And there we have it: a complete Docker Compose file equipped to run our Wordle clone! I'll open a PR on the react-wordle repo with this new file.

### Step 3: Running Our App
Now that we’ve done all the hard work, we can head on over to the terminal, navigate to the app’s root directory, and run the `docker compose up` command. Compose will provide a link to the running application, which we can access from the browser.

![Using Docker Compose in a terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jshs7xtsj87e51hrwycd.gif)

If you would like to run Wordle in a cloud-hosted [ephemeral environment](https://ephemeralenvironments.io), you can <a href="https://docs.shipyard.build/docs/quickstart" target="_blank">create a new application on Shipyard</a> for free. This is especially useful if you’re building and testing your own Wordle spinoff. You and your team can watch your commits in real time in a fully-interactive preview environment.
## …And Enjoy!
Now you can harness the power of Docker Compose to manage your fully-functional Wordle clone! 


![React Wordle deployed and running!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfqu7bg90d5xmfdlhuh6.gif)

The possibilities are endless — you can customize Wordle to your liking, contribute to the react-wordle repo, host your own Wordle variant online, and share links to your creations with friends and colleagues. For now, you can maybe just sit back, relax, and solve today’s Wordle.

*If you're interested in exploring another great OSS repo that can be run with Docker Compose, <a href="https://shipyard.build/blog/docker-compose-community-spotlight-taskcafe/" target="_blank">check out my article on Taskcafé</a>. It's a self-hosted Kanban board built with React and Golang.*