use docker-compose
Simple docker step by step how to use docker-compose with explanation
Docker-compose is a handy tool for those who wish to define and run multi-container Docker applications.
This tutorial will guide you step by step on how to utilize docker-compose effectively.
Step 1: Install Docker and Docker-Compose
Before we start dabbling in Docker-compose, make sure both Docker and Docker-compose are installed on your machine.
The official Docker documentation can guide you on how to install Docker on Linux, Windows, and macOS.
Similarly, Docker-compose can be installed by following the instructions on the official Docker-compose documentation.
Step 2: Create Your Dockerfile
Assuming the Docker and Docker-compose are installed, the next step is to create a Dockerfile.
The Dockerfile specifies what goes on in the environment inside your container.
For example:
# syntax=docker/dockerfile:1
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
This simple example is for a node application, but yours might be different based on the application you’re running.
Step 3: Create the Docker-Compose File
Next, create a docker-compose.yml file, which will define the services that your application is made up of.
In a basic setup, you might only have one service: your application. However, if you have a database for example, you should start that as another service.
Here’s an example docker-compose file for an application using a database:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Step 4: Build and Run your App with Docker-Compose
Once your Dockerfile and docker-compose file are set up, you can use Docker-compose to build your app by typing in the terminal:
docker-compose up
If everything is set up correctly, your application should be running smoothly. You can close it at any time by hitting CTRL+C in your terminal.
Step 5: Check Your Running Services
To check your running services, you can type the following in your terminal:
docker-compose ps
This will give you an overview of the services that you have running.
Remember, Docker-compose is an immensely useful tool for managing multi-container Docker applications.
Like any tool, it requires a little practice to use effectively. This guide serves as a basic introduction to Docker-compose, but there are many more features and functionalities to explore as you become more comfortable with it.