Make sure that your Docker image works
I recently finished a project at a company on Docker, so in the following articles, I will explain how to deploy a Ruby on Rails app on ECS.
The very first thing you have to do to Deploy an application on AWS is to check if your Dockerfile actually works properly. To create a container on ECS (Elastic Container Service), the Docker image needs to be registered in ECR(Elastic Container Registry). The easiest way to do so is to create a Dockerfile for the production (pro.Dockerfile or stg.Dockerfile) and make sure it runs solely with the Dockerfile. Often time, we use volumes (bind-mount) to shorten the build time, but to run on ECS, this is necessary. If the Docker image doesn't work on your local machine, the error must be taken care of before pushing it to ECR.
Creating only Dockerfile can be straightforward, but it is convenient to separate them into a base file and stage or production file for further work.
The base Dockerfile is this. Ruby and postgresql are used for this project.
FROM --platform=linux/amd64 ruby:3.1.2
ENV DEV_PACKAGES=""
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends \
postgresql-client ${DEV_PACKAGES} \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir /app
WORKDIR /app
ENV DOCKER=true
ENV LANG=C.UTF-8
ENV PATH=/app/bin:${PATH}
EXPOSE 80
--platform=linux/amd64 specifies that the image is designed to run on 64-bit x86 processor running a Linux operating system. This is necessary if you develop an application on M1 mac.
stg.Dockerfile is here.
FROM --platform=linux/amd64 assets-service:latest
ADD . /app
ARG GIT_HASH
ENV GIT_HASH=$GIT_HASH
RUN chmod +x ./scripts/start.sh
ENTRYPOINT ["./scripts/start.sh"]
ADD is used in the Dockerfile so all the files in the folder can be copied to the working directory in the container.
start.sh file is this.
echo "Starting rails server..."
rm -rf tmp/pids/server.pid
rails db:migrate
rails s -b 0.0.0.0 -p 80
This way rails server can be started.