0
0
Dockerdevops~5 mins

Defining services in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to run multiple containers together that work as parts of one app, you define services. Services let you describe each container's settings in one file so you can start them all easily.
When you want to run a web app and a database together on your computer.
When you need to start multiple containers with one command instead of many commands.
When you want to share network settings between containers automatically.
When you want to set environment variables or volumes for containers in one place.
When you want to stop or restart all parts of your app together.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: exampleuser
      POSTGRES_PASSWORD: examplepass
      POSTGRES_DB: exampledb
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

This file defines two services: web and db.

The web service uses the nginx image and maps port 8080 on your computer to port 80 in the container.

The db service uses the postgres image with environment variables to set up the database user, password, and database name. It also uses a volume to save data persistently.

The volumes section creates a named volume db-data to keep database files even if the container stops.

Commands
This command starts all services defined in the docker-compose.yml file in detached mode, so they run in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "db-data" with default driver Creating example_web_1 ... done Creating example_db_1 ... done
-d - Run containers in the background (detached mode)
This command lists all running services started by docker-compose, showing their status and ports.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- example_db_1 docker-entrypoint.sh postgres Up 5432/tcp example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command shows the logs of the web service to help you check if it started correctly or if there are errors.
Terminal
docker-compose logs web
Expected OutputExpected
example_web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration example_web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ example_web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh example_web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf example_web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf example_web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh example_web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
This command stops and removes all containers, networks, and volumes created by docker-compose for this project.
Terminal
docker-compose down
Expected OutputExpected
Stopping example_web_1 ... done Stopping example_db_1 ... done Removing example_web_1 ... done Removing example_db_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: defining services in a docker-compose.yml file lets you start and manage multiple containers as one app easily.

Common Mistakes
Not mapping ports in the service definition
Without port mapping, you cannot access the service from your computer's browser or other tools.
Always include the ports section with host:container port mapping for services you want to access externally.
Forgetting to create volumes for databases
Without volumes, database data is lost when the container stops or is removed.
Define volumes in the compose file and attach them to database services to keep data persistent.
Running docker-compose up without -d and then closing the terminal
The containers stop when the terminal session ends if not run in detached mode.
Use the -d flag to run containers in the background so they keep running after you close the terminal.
Summary
Define each container as a service in docker-compose.yml with image, ports, environment, and volumes.
Use 'docker-compose up -d' to start all services in the background.
Use 'docker-compose ps' to check running services and 'docker-compose logs' to see their output.
Use 'docker-compose down' to stop and clean up all services and networks.