i2tutorials

Docker – Compose

Docker Compose

 

Step-1:

To get the latest stable version of Docker Compose, download it from the official Github repository.

$ 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

Next, set permissions so that docker-compose can run:

$ sudo chmod +x /usr/local/bin/docker-compose

Verify the installation by running the following commands:

$ docker-compose --version

Output

Here’s how to set up a docker-compose.yml file and get a containerized environment running with Docker Compose.

Step-2: Creating a docker-compose.yml file

You can use Docker Compose by setting up docker-compose.yml. With the official Nginx image from Docker Hub, a static HTML file will be served.

Make a new directory in your home folder, then move into it:

$ mkdir ~/compose-demo
$ cd ~/compose-demo

Set up an application folder in this directory to act as the document root for Nginx:

$ mkdir app

Create an index.html file in the app folder using your favorite text editor:

$ vi app/index.html

This file needs the following content:

s2

If you’re using vi, you can save and close the file by typing CTRL+X, then Y and ENTER.

Step-3: Create docker-compose.yml:

$ vi docker-compose.yml

In docker-compose.yml, add the following content:

Docker Compose knows what configuration version we’re using from the docker-compose.yml file.

After that, you’ve got the services block, where you set up the services. In your case, you have one service called web, which uses the nginx:alpine image and sets up port redirection. All requests will be redirected to the web container on port 80, where Nginx runs, on the host machine (where Docker Compose is running).

In the next step, you’ll set up Docker Compose to bring up the containerized web server environment that will serve your demo page.

Step-4:  Running Docker Compose

This command will download the Docker images, create a container for the web service, and run the containerized environment:

$ docker-compose up -d

If Docker Compose can’t find the image on your local system, it will download it from Docker Hub. You’ll see output like this:

Your environment is now running in the background. To make sure it’s running, run:

$ docker-compose ps

You can use this command to see if there are any running containers and their states:

If you’re running this demo on your local machine, point your browser to localhost:8000, or your_server_domain_or_IP:8000 if you’re running it remotely.

Here’s what you’ll see:

Your app folder files are kept in sync with the container’s document root thanks to the shared volume you set up in docker-compose.yml. When you reload the page, any changes you make to index.html will be picked up by the container automatically.

We’ll show you how to manage your containerized environment with Docker Compose next.

Docker Compose Commands: A Quick Guide

You’ve seen how to set up docker-compose.yml and run docker-compose up. Let’s see how to use Docker Compose commands to manage your containerized environment.

You can check your Nginx container’s logs with the logs  command:

$ docker-compose logs

Output

You can pause environment execution without changing the current state of your containers by using:

$ docker-compose pause

Output

After a pause, resume execution:

$ docker-compose unpause

Output

You can use the stop command to end container execution, but it won’t destroy your containers’ data:

$ docker-compose stop

Output

Use the down command to remove the containers, networks, and volumes associated with this containerized environment:

$ docker-compose down

Output

This won’t remove Docker Compose’s base image for spinning up your environment (in your case, nginx:alpine). Because the image is already on your system, bringing your environment up again with docker-compose up will be much faster.

If you want to remove the base image too, you can do this:

$ docker image rm nginx:alpine

Output

Conclusion

We’ve seen how to install Docker Compose and set up a containerized environment using Nginx.

 

Exit mobile version