Docker and Docker Compose
Edward Odhiambo
Full-Stack Engineer | JavaScript, React, NextJS, Redux, Rails & Databases | Docker, Kubernetes and AWS
Docker is a set of services that uses virtualisation technology and packages known as containers to manage package images that can be used in any environment. Docker packages an image containing all the dependencies that the package needs so that it can be used again in any other environment without facing issues with having to install correct package versions or whether it is compatible with your Operating System.
Docker has a platform whether images have already been packaged and can be downloaded by anyone for example MongoDB docker image. You can get them at Docker Hub. In this guide, we will look at how to install and set up Docker, how to pull already existing Docker images and also how to build your own images. For users running Windows, it is preferred that you install Docker Destop instead. You can also install the Desktop version on Mac as well.
For this guide, I will focus on installing it on Linux since the Desktop version is not fully supported in all Linux distributions. However, since some distros support the Desktop version, you can install the Desktop version. I will focus on installing it on Arch Linux which is much simpler and because the desktop version is not fully supported yet.
For this case, we will use the Arch package manager pacman.
$ sudo pacman -S docker docker-compose
This will download and install Docker and docker compose which are the tools that we will be working with. In this example we will set up a Node backend project that relies on MongoDB as its database. We will pull down the official MongoDB image from Docker Hub and then connect to it from our backend.
We will then build a docker image for our Node project as well.
$ sudo docker pull mongo
This will now pull the MongoDB image to our local system and we can now connect to it from our backend app.
Assuming that you created your Node backend code already, we can proceed to set up the Docker part of it that involves packaging the backend itself as well as setting up the connections for the Mongo database.
We will need to have three files, a Dockerfile, docker-compose.yaml and .dockerignore
Dockerfile
This will contain a set of instructions on how we want Docker to set up and package our Node backend.
FROM node:16
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
CMD ['npm', 'start']
.dockerignore
This is kind of like gitignore where we define a set of files that we want to be ignored by Docker.
./node_modules
Dockerfile
.dockerignore
docker-compose.yaml
If you don't want to go through this process of defining the files to be ignored. You can simply initiate this via a command.
First, install the npm package.
npm i -g dockerignore
Now, just simply type this in the terminal in your project directory.
领英推荐
dockerignore
This will create a .dockerignore file and add all the files that Docker is supposed to ignore.
docker-compose.yaml
This is now the file that will contain the configuration for the Node backend as well the MongoDB image. It will also have environment variables that will enable us to connect to the database.
This file requires a couple of parameters. You will need to specify a version which should always be 3 or greater. Under services, you will specify the images that you are working with in your project. In this case, our Node backend will be api and the MongoDB will be mongo.
You can specify environment variables in the environment parameter.
version: "3"
services:
api:
build: .
depends_on:
- mongo
environment:
PORT: 9239
MONGO_URL: mongodb://root:example@mongo:27017/
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
In this file, under the api service, we specify build . to tell Docker to build the image from the Dockerfile.
depends_on is to tell Docker that the api service will require MongoDb to be running first before connecting to it later on.
For MongoDb, we specify the image to tell Docker to use the mongo image that we have pulled from Docker Hub. If you have not pulled the image yet, Docker will simply pull when we compose the file.
We also specify the restart: always to ensure that MongoDb will always run if we restart the machine. Now that we have set up everything, we can now compose the file for Docker to build and set up the images correctly and serve our api.
sudo docker compose up -d
This will package everything up and serve the app and if the configuration was done correctly, the backend should run and be able to connect to MongoDB as well.
We use the -d flag to keep the service running.
Now, you can test your api and should be working correctly.
Let’s Connect
References
CTO och Tech Lead
2 年Great article Edward.
Ruby Developer | Results-oriented software engineer, passionate about building impactful products
2 年Nice article Edward. Great primer for someone who wants a quick intro to Docker.