What is the best way to Install docker compose

Docker compose is a service within docker that help us to launch multiple container at same time.

Previous Tutorial – Docker Network

Install Docker Compose

install Docker Compose is on multiple platforms

Check Official Website for Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now check if docker compose installed or not

docker-compose --version

How to use Docker Compose

Navigate a to folder where you want to keep your project

Create a Dockerfile

Create a simple docker file with name ubuntu.dockerfile and paste the bellow code in it.

FROM ubuntu

Now lets create the image using above dockerfile.

docker build -t ubuntu2 -f ubuntu.dockerfile .

Create a Docker Compose YAML file – docker-compose.yml

Create a file with name docker-compose.yml and paste the bellow code in it

version: "3"

networks:
  network1:
    driver: bridge

services:
  web:
    image: "nginx:latest"
    ports:
      - "5000:5000"
    networks:
      - network1
  database:
    image: "mysql"
    networks:
      - network1
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: passwd
      MYSQL_DATABASE: db
      MYSQL_USER: user-db
      MYSQL_PASSWORD: passwd-bdd

volumes:
  db_data: {}

Run docker-compose file – UP and DOWN

Once your have create docker–compose file, now make the docker compose up

docker-compose up

Now your compose file is running, you can go to new terminal and check your containers with bellow command

docker ps

You can shut down the compose by using docker-compose down

docker-compose down

Docker Compose for WordPress

Navigate or create a folder, for this example we are creating a folder wordpress. Inside the folder create a docker-compose.yml file and bellow code

version: '3.3'

services:
  db:
    image: mysql:5.7
    volumes: 
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password123
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}

Now just we need to start our compose file

docker-compose up

You can open new terminal and check your container working or not

docker ps

Now lets access it using browser, open any browser and type http://localhost:8000/ or ip_address:8000.

Now you can step up your wordpress.

Useful Command Used with Docker Compose

Down the compose

docker-compose down

Up the compose in detach mode

docker-compose up -d

Check all running compose

docker-compose ps

Check environment variable of service in docker compose

docker-compose run <service_name> env
docker-compose run db env
docker-compose run wordpress env

Stop Docker Compose Services

docker-compose stop