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
How To Install and Use Docker on Ubuntu 22.04
Docker is a platform used by developers to automate the deployment of applications within containers. The containers include all the necessary components for the application to run, ensuring consistent behavior across various environments. In this guide, we'll walk through the process of installing
4 min read
How to Install and Configure Docker in Ubuntu?
Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install and use PHP Composer on Linux?
A composer is a tool that is used for dependency management in PHP. But Composer is not a package manager. Composer is an application-level manager that is completely used for PHP programming language. A package manager is used to import codebases into the project and make it up to date. Composer he
3 min read
How to install Cockpit on Ubuntu 22.04 for better server
A reliable and efficient server architecture depends on server administration. To make managing your Ubuntu 22.04 server easier, you can use Cockpit, a straightforward web-based interface that provides an intuitive dashboard for system operations and server administration. In this article, we'll lea
4 min read
How to Install NVM on Ubuntu 22.04
Whether you are developing a web application, working with a Node.js-based project, or learning Node.js in general, NVM makes it easier to work with your Node.js environments in Ubuntu. In essence, NVM (Node Version Manager) is a useful tool for working with multiple versions of Node.js on your comp
5 min read
How to Install Docker on CentOS?
Quick Preview to Install Docker on CentOS:Installation of Docker on CentOS:Open CentOS Terminal.Execute the command yum update in the Root Menu.Run the command yum install -y yum-utils device-mapper-persistent-data lvm2.Execute the command yum-config-manager --add-repo https://download.docker.com/li
4 min read
How to Install Docker using Chocolatey on Windows?
Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install Docker on Debian?
Docker Service Product is the essential tool used for the development purpose of any software where the said software needs to be passed through different development phases. The Installed Docker Service makes Operating System-Level Virtualization to create Docker Containers. Docker can easily be in
4 min read
How To Install Docker On AWS EC2 ?
You can use the following instructions to install Docker on an AWS EC2 instance. Note that depending on the Linux distribution your EC2 instance is running, there may be differences in certain commands or processes. What is Docker?Docker is an OS-level virtualization that provides tools for building
3 min read
How to Use Docker Compose With Jenkins
In the fast-paced world of software development, it is mainly ensured by automation of repetition and task persistence. This is where Docker Compose and Jenkins come into play. Docker Compose is a tool for defining and running multi-container Docker applications. On the other hand, Jenkins is a wide
8 min read