LinuxCommandLibrary

docker-compose-up

Run Docker Compose defined application

TLDR

Start all services defined in the docker-compose file

$ docker compose up
copy

Start services in the background (detached mode)
$ docker compose up [[-d|--detach]]
copy

Start services and rebuild images before starting
$ docker compose up --build
copy

Start specific services only
$ docker compose up [service1 service2 ...]
copy

Start services with custom compose file
$ docker compose [[-f|--file]] [path/to/config] up
copy

Start services and remove orphaned containers
$ docker compose up --remove-orphans
copy

Start services with scaled instances
$ docker compose up --scale [service]=[count]
copy

Start services and show logs with timestamps
$ docker compose up --timestamps
copy

SYNOPSIS

docker-compose [GLOBAL_OPTIONS] up [COMMAND_OPTIONS] [SERVICE...]

PARAMETERS

-f, --file
    Specify one or more Compose files to use. Can be specified multiple times for multiple files.

-p, --project-name
    Specify an alternate project name (default is the directory name).

--env-file
    Specify an alternate environment file.

-d, --detach
    Detached mode: Run containers in the background.

--no-deps
    Don't start linked services (dependencies).

--always-recreate-deps
    Recreate dependent containers, even if they're not explicitly specified.

--build
    Build images before starting containers. Useful for applying local changes.

--no-build
    Don't build images, even if they are missing or outdated.

--no-start
    Don't start the services after creating them. Useful for just creating resources.

--force-recreate
    Recreate containers even if their configuration hasn't changed. Implies --renew-anon-volumes.

--no-recreate
    Don't recreate containers if they already exist, just start them.

--remove-orphans
    Remove containers for services not defined in the Compose file.

--abort-on-container-exit
    Stop all containers if any container exits.

-V, --renew-anon-volumes
    Recreate anonymous volumes instead of retrieving data from the previous containers.

--scale =
    Scale a specific SERVICE to NUM instances. Overrides the `scale` setting in the Compose file.

-t, --timeout
    Specify a shutdown timeout in seconds (default: 10) for stopping containers.

--no-attach
    Don't attach to stdout/stderr of services. Useful in combination with -d.

DESCRIPTION

docker-compose up is a fundamental command for managing multi-container Docker applications defined in a docker-compose.yml file. It builds, creates, (re)creates, attaches to, and starts all services within your application stack.

When executed, it first reads the service definitions. If `build` instructions are present, it constructs the necessary Docker images; otherwise, it pulls specified images from a registry. Next, it creates or recreates containers for the defined services, ensuring their configurations are up-to-date. `docker-compose up` then establishes a default network, allowing services to communicate seamlessly using their service names.

By default, the command attaches to the output of all services, streaming their logs directly to your terminal, which is ideal for development and debugging. For background execution, the -d (detached) flag can be used. This command significantly simplifies the deployment and management of complex application environments, enabling developers to bring an entire application up with a single, concise command.

CAVEATS

Resource Consumption: Running many services, especially with builds, can consume significant CPU, memory, and disk resources.
Debugging Detached Containers: Logs for containers running in detached mode (`-d`) are not immediately visible, requiring `docker-compose logs` for inspection.
Data Persistence: Anonymous volumes are recreated by default when containers are recreated, leading to data loss. Named volumes are generally preferred for persistent data.
Networking Complexity: While Compose simplifies networking, understanding how services communicate and expose ports is crucial for troubleshooting.
Version Compatibility: Ensure your `docker-compose.yml` version matches your Docker Compose CLI version for optimal compatibility.

THE <I>DOCKER-COMPOSE.YML</I> FILE

The docker-compose.yml file is the heart of `docker-compose up`. This YAML configuration defines your application's services, networks, and volumes. Each service entry specifies crucial details like the Docker image or `build` context, port mappings, mounted volumes, environment variables, and inter-service dependencies. A thorough understanding of this file's structure and options is critical for effectively deploying and managing your application with Docker Compose.

TYPICAL WORKFLOW

A common workflow involves creating a `docker-compose.yml` and then executing `docker-compose up -d --build` to deploy your application in the background, ensuring images are freshly built. For iterative development, `docker-compose up --build` can refresh services without full teardown. Upon completion, `docker-compose down` efficiently removes all created resources, including containers, networks, and volumes, leaving a clean environment.

HISTORY

Docker Compose evolved from a tool called Fig, developed by Orchard Laboratories. Docker acquired Orchard Labs in 2014, subsequently rebranding Fig as Docker Compose. Its core purpose was to simplify the management of multi-service applications by consolidating their definitions into a single YAML file, providing an easier alternative to orchestrating multiple `docker run` commands. Since its inception, Docker Compose has become an essential tool in the Docker ecosystem, continuously evolving to meet the demands of modern microservices architectures.

SEE ALSO

Copied to clipboard