The Dockerfile
We'll cover the following
In this lesson, we are going to discuss the Dockerfile that we uploaded in the previous lesson. This lesson will show you how to fulfill all the requirements and dependencies of your application if you will run it in a Docker container.
Dockerfile
So here goes the Dockerfile:
FROM gcr.io/educative-exec-env/educative:latestRUN cd usr/local/educative && mkdir Basic-MERN-Stack-App## Copying the contents of Basic-MERN-Stack-App to the folder we created in the above commandCOPY Basic-MERN-Stack-App usr/local/educative/Basic-MERN-Stack-AppRUN cd usr/local/educative/Basic-MERN-Stack-App && \npm install && \cd client && \npm install## Installing YarnRUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.listRUN apt-get update && apt-get install yarn## Fixing etc/hostsRUN echo "127.0.0.1 localhost" >> /etc/hosts && \echo "::1 localhost ip6-localhost ip6-loopback" >> /etc/hosts && \echo "fe00::0 ip6-localnet" >> /etc/hosts && \echo "ff00::0 ip6-mcastprefix" >> /etc/hosts && \echo "ff02::1 ip6-allnodes" >> /etc/hosts && \echo "ff02::2 ip6-allrouters" >> /etc/hosts && \echo "172.17.0.3 e2d7ddabb2c5" >> /etc/hosts## Installing TmuxRUN apt-get -y install tmux## Installing MongoDBRUN apt-get update && rm -rf /var/lib/apt/lists/*RUN apt-get update && apt-get install -y gnupg2RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4RUN bash -c 'echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list'RUN apt-get updateRUN apt-get install -y mongodb-org=4.0.10 mongodb-org-server=4.0.10 mongodb-org-shell=4.0.10 mongodb-org-mongos=4.0.10 mongodb-org-tools=4.0.10 --allow-unauthenticatedRUN mkdir -p /data/dbRUN mongod --smallfiles > /dev/null &
Base Image
FROM gcr.io/educative-exec-env/educative:latest
This is an official image made by Educative. It comes with an operating system of Ubuntu:16.04
with Python 2.7
and Node.js 8.x
already installed in the container. So now we have Node.js
of version 8
built into the container.
Copying Files into Docker environment
Next, we will transfer files from outside of the Docker environment into the Docker environment. So first of all, we create a folder named Basic-MERN-Stack-App in the directory (usr/local/educative
):
RUN cd usr/local/educative && mkdir Basic-MERN-Stack-App
Your code can reside anywhere else as well but as a convention, we place the folders in this directory (usr/local/educative
).
By using the COPY
command, we copy the contents of Basic-MERN-Stack-App
into usr/local/educative/Basic-MERN-Stack-App
(the folder that we created in the previous step):
## Copying the contents of Basic-MERN-Stack-App to the folder we created in the above command
COPY Basic-MERN-Stack-App usr/local/educative/Basic-MERN-Stack-App
Now that the files are present in the Docker environment, we install the node_modules
by using npm install
in the root and the client folder:
RUN cd usr/local/educative/Basic-MERN-Stack-App && \
npm install && \
cd client && \
npm install
The above portion is how you copy your code files into the Docker Environment. So far, we have Ubuntu
as our OS with Python and Node.js installed on it. We have also transferred our application code into the Docker environment in the following directory:
usr/local/educative/Basic-MERN-Stack-App
.
Installing yarn
Let’s look at the requirements left now.
As this particular project requires yarn
, we install it through the following instructions:
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn
Fixing etc/hosts
On Educative, the etc/hosts
files need to be fixed as it becomes empty in the OS of the Docker container. The /etc/hosts
is an operating system file that translates hostnames or domain names to IP addresses Therefore, we append the lines specified using the echo
command:
RUN echo "127.0.0.1 localhost" >> /etc/hosts && \
echo "::1 localhost ip6-localhost ip6-loopback" >> /etc/hosts && \
echo "fe00::0 ip6-localnet" >> /etc/hosts && \
echo "ff00::0 ip6-mcastprefix" >> /etc/hosts && \
echo "ff02::1 ip6-allnodes" >> /etc/hosts && \
echo "ff02::2 ip6-allrouters" >> /etc/hosts && \
echo "172.17.0.3 e2d7ddabb2c5" >> /etc/hosts
Tmux
tmux
is not part of the requirement but it will used by us to view both the client and server sessions at once in the same terminal.
RUN apt-get -y install tmux
MongoDB
Finally, MongoDB
is installed in the following way:
RUN apt-get update && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y gnupg2
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
RUN bash -c 'echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list'
RUN apt-get update
RUN apt-get install -y mongodb-org=4.0.10 mongodb-org-server=4.0.10 mongodb-org-shell=4.0.10 mongodb-org-mongos=4.0.10 mongodb-org-tools=4.0.10 --allow-unauthenticated
RUN mkdir -p /data/db
RUN mongod --smallfiles > /dev/null &
You can modify the versions in the command according to the requirements of your application.
Options to Copy Code
There are two options to copy code files into the container:
- Cloning from GitHub
- Copying Files via Dockerfile when the image is built
To clone from GitHub, you can use the git clone <your-repository-link>
command. If we didn’t make modifications to the code on GitHub, we would have used the following commands:
RUN cd usr/local/educative && \
git clone https://github.com/hemakshis/Basic-MERN-Stack-App.git && \
cd Basic-MERN-Stack-App && \
npm install && \
cd client && \
npm install