Tags: #docker #dockercompose #cheatsheet
Last Reviewed: 16/08/2024
Usefull commands for docker and docker-compose
Command | Purpose |
---|---|
docker-compose up -d | Starts the Container and dettach (so you have your bash prompt back) |
docker-compose down | Stops the Container |
docker-compose ps | Checks the status of the Container |
docker-exec -ti <container-name> /bin/bash | Opens a Shell Prompt inside a Container (so you can troubleshoot) |
docker save <image> > file.tar | Saves an Image to a Tar file Eg.: docker save nginx:latest > nginx_backup.tar |
docker load < file.tar | Loads a Tar file as an Image |
docker stop $(docker ps -a -q) | Stops all Containers. You can use “rm” instead of “stop” to remove them! |
docker build --tag <tag> . | To be run from the directory where “dockerfile” lives. Will create an Image with the tag provided. Eg.: docker build –tag tornscrapper:0.1 |
docker images -a | grep none | awk '{ print $3; }' | xargs docker rmi | Delete dangling images and / or images that have a “none” tag. Might be helpful if “docker image prune” doesn't remove those. |
docker compose version docker compose config docker compose start docker compose stop docker compose restart docker compose run docker compose create docker compose attach docker compose pause docker compose unpause docker compose wait docker compose up docker compose down docker compose ps docker compose top docker compose events docker compose logs docker compose images docker compose build docker compose push docker compose cp docker compose exec
web: # build from Dockerfile build: . args: # Add build arguments APP_HOME: app # build from custom Dockerfile build: context: ./dir dockerfile: Dockerfile.dev # build from image image: ubuntu image: ubuntu:14.04 image: tutum/influxdb image: example-registry:4000/postgresql image: a4bc65fd
ports: - "3000" - "8000:80" # host:container # expose ports to linked services (not to host) expose: ["3000"]
# environment vars environment: RACK_ENV: development environment: - RACK_ENV=development # environment vars from file env_file: .env env_file: [.env, .development.env]
# makes the `db` service available as the hostname `database` # (implies depends_on) links: - db:database - redis # make sure `db` is alive before starting depends_on: - db # make sure `db` is healthy before starting # and db-init completed without failure depends_on: db: condition: service_healthy db-init: condition: service_completed_successfully
# make this service extend another extends: file: common.yml # optional service: webapp volumes: - /var/lib/mysql - ./_data:/var/lib/mysql # automatically restart container restart: unless-stopped # always, on-failure, no (default)
services: web: labels: com.example.description: "Accounting web app"
services: web: dns: 8.8.8.8 dns: - 8.8.8.8 - 8.8.4.4
services: web: devices: - "/dev/ttyUSB0:/dev/ttyUSB0"
# declare service healthy when `test` command succeeds healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 1m30s timeout: 10s retries: 3 start_period: 40s
# specifying user user: root # specifying both user and group with ids user: 0:0