How To Install and Use Docker Compose on Ubuntu 22.04
Last Updated :
08 Jul, 2024
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
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
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
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
Command to run your application :
$ docker compose up
Go to the URL http://Ip:3000 to test your application is running or not :
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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read