Dockerize Zombie Frontend - cogeorg/teaching GitHub Wiki

We only need to containerize the front end since your contracts will be deployed to a test or the main network. To create a Docker image, an instance of which will be the container, we need to specify a Dockerfile.

Define the Docker file

Create a file called Dockerfile (yes, no ending) in the root of your cyptozombie repository (same level as truffle-config.js) and add the following lines:

# 1
FROM node:13.6.0-alpine

# 2
RUN mkdir -p /usr/app

# 3
WORKDIR /usr/app

# 4
COPY build/contracts/ build/contracts/
COPY src/ src/
COPY bs-config.json .
COPY package.json .

# 5
RUN npm install

# 6
EXPOSE 3000
EXPOSE 3001

# 7
CMD ["npm", "run", "dev"]
  1. defines the docker image that serves as a baseline for this container. It contains the computing environment necessary to run node.
  2. runs a bash command. Here, it creates a directory called /usr/app
  3. sets the working directory to /usr/app
  4. copies the code files into the docker container
  5. installs the dependencies for the code
  6. exposes the port to the host. If this is not set, the endpoint is only reachable from within the container but not from the host
  7. defines the command that is run when the container is started. Here, it is npm run dev.

Build and Run

This Dockerfile is used to build an image. Containers are running instances of an image.

$ docker build -t zombie-frontend:0.1.0 .
$ docker run -d --name zombies -p 3000:3000 -p 3001:3001 zombie-frontend:0.1.0

The first command builds the docker image, calling it zombie-frontend and tagging it with version number 0.1.0. The second command starts the container in detached mode (-d, optional), calling it zombies (--name, optional), exposing the ports (-p), and specifying the image and its version.

You should now be able to visit your zombie frontend on http://localhost:3000.

Other Docker Commands

All running docker containers and images are listed using

$ docker ps
$ docker images

respectively, and the running container is stopped and removed using

$ docker stop zombies
$ docker rm zombies

More docker commands can be found on this cheat sheet.