Skip to content

Instantly share code, notes, and snippets.

@andimiya
Last active January 6, 2020 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andimiya/058ba5b2fcb6e045b066913c12cdaab0 to your computer and use it in GitHub Desktop.
Save andimiya/058ba5b2fcb6e045b066913c12cdaab0 to your computer and use it in GitHub Desktop.

Notes for Dockerizing an Express Server then Hosting on an EC2

Locally...

touch DockerFile

touch .dockerignore

  • node_modules
  • npm-debug.log

Build the Image:
Updated command: docker image build -t nameofimage:1.0 . docker build -t <your username>/crypto-api .

Run the Image:
docker run --env-file .env -p 8080:8080 -d andimiya/crypto-api

Get the Container ID:
docker ps

Print app output:
docker logs <container id>

Should see something like:
Running on http://localhost:8080

Look inside the container:
docker exec -it <container id> /bin/bash

NOTES:

  • bash isn't installed on any alpine images
FROM node:7.10

# Create app directory
WORKDIR /usr/src/app


# Install app dependencies
COPY package.json .
# For npm@5 or later, copy package-lock.json as well
# COPY package.json package-lock.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

Set up an EC2 Instance on AWS

Set up security groups, open necessary ports
ssh into the EC2
ssh -i "AndreaKeyPair.pem" ec2-user@ec2-34-238-43-66.compute-1.amazonaws.com
sudo yum install vim
touch .env
vim .env
sudo yum install -y docker - Unless EC2 is pre-loaded with Docker

Populate the .env file with environment variables, don't put export in front of the variables!
docker pull andimiya/crypto-api
docker run --env-file .env -p 8080:8080 -d andimiya/crypto-api

curl localhost:8080 from inside the ec2
curl an endpoint from inside the ec2

After Things are Running on the EC2, Deployment Steps for the API

  • Build from local
    • docker build --no-cache -t andimiya/crypto-api .
  • Push from local to Docker
    • docker push andimiya/crypto-api
  • ssh into EC2 instance
    • ssh ec2-user@34.212.0.41 -i ~/.ssh/AndreaWestKeyPair.pem
  • Stop the running docker container
    • Pull down the latest docker image (docker pull andimiya/crypto-api)
    • Run docker run --env-file .env -p 8080:8080 -d andimiya/crypto-api

Set up the S3

Static site hosting
Set error file to: index.html
Make a deploy.sh file:

npm run build

aws s3 sync build/ s3://andimiya-crypto-app --delete --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment