Docker-compose basic tutorial
Tags: docker, dockercompose
Published: 2022-02-07 12:18:51

# Docker Compose Tutorial

Docker simplifies the process of managing application processes in containers. While containers are similar to virtual machines in certain ways, they are more lightweight and resource-friendly. This allows developers to break down an application environment into multiple isolated services.

## Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Compose works in all environments: production, staging, development, testing, as well as CI workflow etc

Setting Up a docker-compose.yml File

mkdir ~/compose-demo

cd ~/compose-demo

mkdir app

nano app/index.html

```
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Docker Compose Demo</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>

	<h1>This is a Docker Compose Demo Page.</h1>
	<p>This content is being served by an Nginx container.</p>

</body>
</html>
```

nano docker-compose.yml

```
version: '3.7'
services:
  web:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./app:/usr/share/nginx/html
```

We then have the services block, where we set up the services that are part of this environment. In our case, we have a single service called web. This service uses the nginx:alpine image and sets up a port redirection with the ports directive. All requests on port 8000 of the host machine (the system from where you’re running Docker Compose) will be redirected to the web container on port 80, where Nginx will be running. 

The volumes directive will create a shared volume between the host machine and the container. This will share the local app folder with the container, and the volume will be located at /usr/share/nginx/html inside the container, which will then overwrite the default document root for Nginx.

docker-compose up -d

This command will show you information about the running containers and their state, as well as any port redirections currently in place:

docker-compose ps

To check the logs produced by your Nginx container, you can use the logs command:

docker-compose logs

If you want to pause the environment execution without changing the current state of your containers, you can use:

docker-compose pause

To resume execution after issuing a pause:

docker-compose unpause

The stop command will terminate the container execution, but it won’t destroy any data associated with your containers:

docker-compose stop

If you want to remove the containers, networks, and volumes associated with this containerized environment, use the down command:

Notice that this won’t remove the base image used by Docker Compose to spin up your environment (in our case, nginx:alpine). This way, whenever you bring your environment up again with a docker-compose up, the process will be much faster since the image is already on your system.

docker-compose down

In case you want to also remove the base image from your system, you can use:

docker image rm nginx:alpine