Open In App

How To Install and Use Docker Compose on Ubuntu 22.04

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Docker is a platform that provides virtual containers on which an application can be deployed independent of the underlying OS of the server. Further, the container can be created from a replica called docker image which contains all the dependencies and can run on any OS that has a docker engine, with similar results.

What is docker-compose?

Docker Compose is a tool by which we can manage multi-container docker applications. It simplifies the process of running multiple containers, their configurations, and their interdependencies. Compose uses a YAML file to define the services, networks, and volumes required for your application.

  • Service: Defines containers and their configurations, allowing multiple services to interact within a defined network.
  • Networks and Volumes: Networks enable communication between services, while volumes provide persistent storage shared among containers, ensuring data persistence beyond the container lifecycle.

Prerequisites

Docker Engine: Docker Compose relies on Docker Engine to build and manage containers. Ensure Docker Engine is installed and running on your Ubuntu system.

Note: Make sure you have the latest and stable version of docker.

How to check:

$ docker --version
Command to check docker-engine

Step 1: Download Docker Compose

Use the command to download:

$ mkdir -p ~/.docker/cli-plugins/
$ curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
Download Docker Compose

Step 2: Apply Executable Permissions

The flag settings of the docker-compose file under the ~/.docker/cli-plugins folder are set to Executable by the command chmod +x ~/.docker/cli-plugins/docker-compose, allowing one to be able to execute the Docker Compose command directly on the command line, which aims to help in easier management of multi-container Docker applications. Said differently, this actionable setting of a file makes it runnable with no specification of an interpreter.

$ chmod +x ~/.docker/cli-plugins/docker-compose

Step 3: Verify Installation

The Docker Compose version command verifies the installation of Docker Compose. Running this command lets one know which version of Docker Compose is installed; in addition, it helps in determining whether everything is correctly set up and reachable. This step helps to ensure that everything went well with the installation, making you good to go for using Docker Compose in managing your containerized apps.

$  docker compose version
Apply Executable Permissions

Step 4: Using docker compose to run nodejs application in containers

Creating nodejs application:

Prerequisite:

Your device must have nodejs if not install from this command

$ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
$ sudo apt install -y nodejs

Create a directory :

$ mkdir node-app

Enter into the directory :

$ cd node-app

Create a package.json file and copy the following code in it:

{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}

Create the node js application by writing the following into a new file named index.js :

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
res.send('Hello, Docker!');
});

app.listen(port, () => {
console.log(App running at http://localhost:${port});
});

Create a file named Dockerfile in the current folder and write the following command in it.

# Use the official Node.js image from the Docker Hub
FROM node:14

# Create and change to the app directory
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the application port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

Create a file docker-compose.yml in your directory and write the following commands in it

version: '3'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: npm start

Now run the command to build your application:

$ docker compose build --no-cache
docker compose build --no-cache

Command to run your application :

$ docker compose up
docker compose up command output

Go to the URL http://Ip:3000 to test your application is running or not :

Application is running on 3000 port

Shutting down application

Run the following command to shut down your application-

$ docker compose down

Basic commands of docker compose

  • docker compose build: This command is used to build the images specified in the docker-compose.yml file using the Dockerfiles provided. It is useful when you have made change in the application code or Dockerfile.
  • docker compose up: This command is used to build the images, create the containers from the images, and starts the services as mentioned into the docker-compose.yml file.
  • docker compose down: This command clears up the running containers. Its stops the running containers and remove containers, volumes, networks and images defined in your docker-compose.yml file.

Next Article
Article Tags :

Similar Reads