ComposeMentis: Dancing with Docker Compose
~A Playful Journey into Container Symphony :)
Whatsup people! It's been real long since i've written something. So here i am with a small docker-compose based project that would teach you how we can orchestrate docker containers.
What is Docker Compose???
It's not a big deal, docker compose just helps you run multiple containers in one go using a simple yaml manifest. Let's see how we can implement in a real life project.
Prerequisites!
Docker (of-course :p)
Preferably ubuntu (the commands and files used in the blog are written based on ubuntu)
Git
Some enthusiasm and you're good to go.
The Goal:
is to deploy a reddit clone application on a container using dicker-compose.
Build a docker image
Push it to docker container registry, dockerhub
Make a docker-compose.yaml file
Use it to orchestrate multiple services, set up networks, and volumes.
Let's Get Started!
Clone into the repo!!
- clone the repo into your local machine. In this case we are using a reddit-clone application.:github.com/g4ze/reddit-clone-deploy
$ git clone https://github.com/g4ze/reddit-clone-deploy
$ cd reddit-clone-deploy
Let's build and deploy image!
Since our goal is to deploy the app's image on dockerhub as well, it's necessary to build a good lightweight image out of it.
The repository we just cloned into already contains a ready-to-build dockerfile.
FROM node:19-alpine3.15 WORKDIR /reddit-clone COPY . /reddit-clone RUN npm install EXPOSE 3000 CMD ["npm","run","dev"]
Pretty basic stuff here, copying the directory, installing npm, exposing port 300 and running.
Build it from the terminal, make sure you're in the right directory!
$ docker build -t reddit .
A simple
docker images
would show something like:$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE reddit latest e032a908867c 13 minutes ago 760MB
Great! Now that we've built our image, let's just simply deploy it on docker hub:
$ sudo docker push g4ze/reddit:latest
Writing the docker-compose file:
This is how the docker compose fill should like for the app we a re using:
version: '3'
services:
reddit-clone:
image: your-image-name
ports:
- "3000:3000"
working_dir: /reddit-clone
environment:
NODE_ENV: development
command: npm run dev
version: '3'
means that the version of docker compose format we are using is 3services:
:- Defines a list of services. In this case, there is one service named
reddit-clone
.
- Defines a list of services. In this case, there is one service named
reddit-clone:
:- The name of the service. This name is used to reference the service within the Docker Compose file.
image: your-image-name
:- Specifies the Docker image to use for the service. Replace
your-image-name
with the actual name or ID of the Docker image you want to use. This image is the base for your service.
- Specifies the Docker image to use for the service. Replace
ports:
:- Defines port mappings between the host and the container. In this case, it maps port 3000 from the container to port 3000 on the host (
"3000:3000"
). This allows you to access your Node.js application running in the container onlocalhost:3000
on the host machine.
- Defines port mappings between the host and the container. In this case, it maps port 3000 from the container to port 3000 on the host (
working_dir: /reddit-clone
:- Sets the working directory inside the container to
/reddit-clone
. This is the directory where your application code is expected to be.
- Sets the working directory inside the container to
environment:
:- Allows you to set environment variables for the container. In this case, it sets
NODE_ENV
todevelopment
. This is commonly used to configure the runtime environment for your application.
- Allows you to set environment variables for the container. In this case, it sets
command: npm run dev
:- Specifies the command to run when the container starts. In this case, it runs the
npm run dev
command, which is typically used to start a Node.js development server.
- Specifies the command to run when the container starts. In this case, it runs the
In summary, this Docker Compose file defines a service named reddit-clone
based on a Docker image specified by your-image-name
. It maps port 3000 from the container to port 3000 on the host, sets the working directory to /reddit-clone
, sets the NODE_ENV
environment variable to development
, and runs the npm run dev
command when the container starts. This configuration is suitable for a Node.js application, especially during development.
Finale:
Now that we have out image built, docker compose file ready and our hopes high, let's dive right into our final command:
$ sudo docker-compose up
It it wroks, congrats, you can access the application on your localhost: http://localhost:3000/
That's it guys, you now have another small devops project on your github profile. Explore more to unlock more potential.
Have fun!
\>.<