How to use docker compose unpause command to resume services

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose unpause command to resume paused services. We will begin by creating a simple docker-compose.yml file to define a basic web service using the Nginx image.

Following the setup, you will start the services using docker compose up, then pause them using docker compose pause. Finally, you will use docker compose unpause to resume the services and verify that they are running correctly. This hands-on exercise will demonstrate the practical application of pausing and unpausing Docker Compose services.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555095{{"How to use docker compose unpause command to resume services"}} docker/ps -.-> lab-555095{{"How to use docker compose unpause command to resume services"}} docker/pull -.-> lab-555095{{"How to use docker compose unpause command to resume services"}} end

Create a simple docker-compose.yml file

In this step, we will create a basic docker-compose.yml file. This file is the core of Docker Compose, defining the services, networks, and volumes for your application. Before we start, we need to install Docker Compose as it is not pre-installed in this environment.

First, let's install Docker Compose. We will download the latest stable release and make it executable.

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

Now, let's verify the installation by checking the version.

docker-compose --version

You should see the version information printed to the console, confirming that Docker Compose is installed correctly.

Next, navigate to the ~/project directory, which is your working directory for this lab.

cd ~/project

Now, we will create a file named docker-compose.yml in this directory. This file will define a simple service using the nginx image. We will use the nano editor to create and edit the file.

nano docker-compose.yml

Inside the nano editor, paste the following content:

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

Let's break down this docker-compose.yml file:

  • version: '3.8' specifies the Docker Compose file format version.
  • services: defines the different services that make up your application.
  • web: is the name of our service. You can name your services anything you like.
  • image: nginx:latest specifies the Docker image to use for this service. In this case, we are using the latest version of the official Nginx image. Since this image might not be present locally, Docker Compose will automatically pull it from Docker Hub when you start the service.
  • ports: maps ports between the host machine and the container. "80:80" maps port 80 on the host to port 80 on the container. This means you will be able to access the Nginx web server running inside the container by visiting http://localhost (or the VM's IP address) on your host machine.

After pasting the content, save the file by pressing Ctrl + O, then press Enter to confirm the filename, and finally press Ctrl + X to exit the nano editor.

You have now successfully created your first docker-compose.yml file. In the next step, we will use this file to start the Nginx service.

Start services using docker compose up

In this step, we will use the docker-compose up command to start the services defined in our docker-compose.yml file. This command builds, (re)creates, starts, and attaches to containers for a service.

Make sure you are in the ~/project directory where you created the docker-compose.yml file in the previous step.

cd ~/project

Now, run the following command to start the Nginx service:

docker-compose up -d

Let's understand the command:

  • docker-compose up starts the services defined in the docker-compose.yml file.
  • -d runs the containers in detached mode, meaning they will run in the background and not block your terminal.

When you run this command for the first time, Docker Compose will pull the nginx:latest image from Docker Hub if it's not already present on your system. You will see output indicating the image being pulled and the container being created and started.

To verify that the container is running, you can use the docker ps command.

docker ps

You should see an entry for the web service (or a similar name based on your directory and service name) with the status Up. This indicates that the Nginx container is running.

You can also verify that the Nginx web server is accessible by using curl to access the exposed port 80.

curl http://localhost:80

You should see the default Nginx welcome page HTML output in your terminal. This confirms that the Nginx server is running inside the container and is accessible from your host machine through the mapped port.

Pause services using docker compose pause

In this step, we will learn how to pause the running services using the docker-compose pause command. Pausing a container temporarily suspends all its processes. This is different from stopping a container, which shuts down the processes.

Make sure you are in the ~/project directory where your docker-compose.yml file is located and your services are running.

cd ~/project

Now, execute the following command to pause the web service:

docker-compose pause web

You should see output indicating that the web service is being paused.

To verify that the container is paused, you can use the docker ps command.

docker ps

Look at the STATUS column for the web service container. It should now show a status like Up ... (Paused). This confirms that the container's processes are suspended.

While the container is paused, the Nginx web server inside it is not actively processing requests. If you try to access it using curl, the request will likely hang or time out because the server is not responding.

curl http://localhost:80

This command will not immediately return the Nginx welcome page as it did when the container was running. You might need to press Ctrl + C to interrupt the command after a while.

Pausing is useful when you need to temporarily halt a service without losing its current state.

Unpause services using docker compose unpause

In this step, we will resume the execution of the paused service using the docker-compose unpause command. This command unpauses containers that have been paused.

Ensure you are in the ~/project directory where your docker-compose.yml file is located.

cd ~/project

Now, execute the following command to unpause the web service:

docker-compose unpause web

You should see output indicating that the web service is being unpaused.

To verify that the container is no longer paused and is running normally, you can use the docker ps command again.

docker ps

Observe the STATUS column for the web service container. It should now show a status like Up ... without the (Paused) indicator. This means the container's processes have resumed.

After unpausing, the Nginx web server inside the container should be actively processing requests again. You can confirm this by using curl to access the exposed port 80.

curl http://localhost:80

This time, you should immediately receive the HTML output of the default Nginx welcome page, just like you did after initially starting the container. This confirms that the service is fully operational again.

Unpausing is the counterpart to pausing, allowing you to quickly resume a service from its suspended state.

Verify services are running after unpausing

In this step, we will perform a final verification to ensure that the Nginx service is fully operational after being unpaused. While we checked the container status in the previous step, it's good practice to confirm that the application running inside the container is also responding as expected.

Ensure you are in the ~/project directory.

cd ~/project

We will use the curl command again to access the Nginx web server running on port 80.

curl http://localhost:80

You should see the HTML content of the default Nginx welcome page printed to your terminal. This confirms that the Nginx process inside the container is running and serving content.

This step reinforces the understanding that unpausing a container allows the application within it to resume its normal operations.

Finally, to clean up the running container and network created by Docker Compose, you can use the docker-compose down command. This command stops and removes containers, networks, and volumes created by up.

docker-compose down

You will see output indicating that the service and network are being stopped and removed.

To confirm that the container is no longer running, you can use docker ps.

docker ps

The output should show no running containers, indicating that the Nginx service has been successfully stopped and removed.

Summary

In this lab, we learned how to use Docker Compose to manage the lifecycle of services. We started by installing Docker Compose and creating a docker-compose.yml file to define a simple Nginx service.

We then used docker compose up to start the defined service, docker compose pause to temporarily suspend the service's processes, and finally docker compose unpause to resume the paused service. We verified the service's status after each step, demonstrating the practical application of these Docker Compose commands for controlling service execution.