Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker Compose Key Commands dockercompose up down logs exec build pull restart
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-compose-key-commands-dockercompose-up-down-logs-exec-build-pull-restart

Docker Docker Docker Compose Key Commands dockercompose up down logs exec build pull restart

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~2 min read

1. Core Command / Concept

Start and manage a Docker Compose application with ease. docker-compose up.

2. Step-by-Step

  1. Open a terminal and navigate to your project directory.
  2. Run docker-compose up -d to start the containers in detached mode.
  3. Check the containers are running with docker-compose ps.
  4. View the logs with docker-compose logs -f.
  5. ⚠️ Stop and remove all containers with docker-compose down --rmi all.
  6. Clean up by removing the stopped containers with docker rm $(docker ps -aq -f status=exited).

3. Real Commands with Examples


Example 1: Start and view logs

  • Purpose: Start a container and view its logs.
  • Command: docker-compose up -d && docker-compose logs -f
  • Brief explanation of flags: -d runs containers in detached mode, -f follows the log output.

Example 2: Restart a container

  • Purpose: Restart a container.
  • Command: docker-compose restart web
  • Brief explanation of flags: web is the service name.

Example 3: Build an image

  • Purpose: Build a Docker image.
  • Command: docker-compose build
  • Brief explanation of flags: None.

4. Common Errors


Error 1: Image not found

  • Error message or symptom: ERROR: Service 'web' uses an image, but 'docker-compose config' indicates that the image does not exist: ...
  • Why it happens: The image is not built or pulled.
  • One-line fix: Run docker-compose build or docker-compose pull.

Error 2: Port conflict

  • Error message or symptom: ERROR: for web *:80/tcp is already allocated
  • Why it happens: Another container is using the same port.
  • One-line fix: Change the port in the docker-compose.yml file.

Error 3: Service not found

  • Error message or symptom: ERROR: Service 'non-existent-service' not found
  • Why it happens: The service does not exist in the docker-compose.yml file.
  • One-line fix: Check the docker-compose.yml file for the correct service name.

5. Quick Checks


Scenario 1: Check if a container is running

  • Situation: You want to check if a container is running.
  • Command: docker-compose ps
  • What a correct response looks like: The container is listed with a Status of Up.

Scenario 2: Check container logs

  • Situation: You want to check the logs of a container.
  • Command: docker-compose logs -f
  • What a correct response looks like: The logs are displayed in real-time.

Scenario 3: Check if a service is running

  • Situation: You want to check if a service is running.
  • Command: docker-compose exec web /bin/bash
  • What a correct response looks like: You are logged into the container.

6. Last-Minute Reference

docker-compose up: Starts and runs containers in detached mode.
docker-compose down: Stops and removes containers.
docker-compose logs: Displays container logs.
docker-compose exec: Executes a command inside a container.
docker-compose build: Builds a Docker image.
docker-compose pull: Pulls the latest image from the registry.
docker-compose restart: Restarts a container.
docker-compose ps: Lists running containers.
docker-compose config: Displays the configuration of the project.
• ⚠️ docker-compose down --rmi all: Deletes all images and containers.




ADVERTISEMENT