Skip to content

Instantly share code, notes, and snippets.

@serjee
Last active May 5, 2024 07:58
Show Gist options
  • Save serjee/d811942ca38f11a3daa1805ba0941737 to your computer and use it in GitHub Desktop.
Save serjee/d811942ca38f11a3daa1805ba0941737 to your computer and use it in GitHub Desktop.
#docker

Docker CheatSheet 🐋

List commands, version and info

docker list docker commands

docker container --help list container commands

docker --version short info about version

docker version full info about version

docker info full info about docker

View images & containers & networks & volume

docker image ls view images

docker container ls view runned containers (depreciated docker ps)

docker container ls -a view all containers (depreciated docker ps -a)

docker container ls -q view only id of containers

docker network ls view networks

docker volume ls view volume

Connect

sudo docker exec -it your-name-container bash connect to your-name-container

Pull & Build & Logs

docker pull [name] download image from Docker register

docker build -t [name] build image from dockerfile and set name [name]

docker logs -follow [name] realtime monitoring for container

Run & Stop

docker run [name] run image

docker run -it [name] sh run with interaction mode (shell)

docker run -d [name] run with detached mode (unbind terminal from runed container)

docker run -p 8080:80 [name] run with map container port (80) to public port (8080)

docker run -d --restart always --name your-container-name -p 8080:80 -v /var/www/html:/usr/share/nginx/html [name] run with map container port (80) to public port (8080) and with auto restart after down and with bind container directory /usr/share/nginx/html to public directory /var/www/html

docker stop [name] stop container

docker stop $(docker ps -a -q) stop all containers

Remove container

docker rm [ID] remove container with [ID]

docker rm $(docker ps -a -q -f status=exited) remove all container with status exited

Docker Compose

Example docker-compose.yml for postgree:

version: "3.9"
services:
  postgres:
    image: postgres:14.1
    environment:
      POSTGRES_DB: "userdb"
      POSTGRES_USER: "userdb"
      POSTGRES_PASSWORD: "userdb"
      PGDATA: "/var/lib/postgresql/data/pgdata"
    volumes:
      - ../2. Init Database:/docker-entrypoint-initdb.d
      - .:/var/lib/postgresql/data
    ports:
      - "5432:5432"

Example docker-compose.yml for python scripts run:

version: "3.9"
services:
  app1:
    image: my-python
    command: /usr/local/bin/python3 /dist/app.py
    restart: always
    volumes:
      - "./app1/:/dist"

  app2:
    image: my-python
    command: /usr/local/bin/python3 /dist/app.py
    restart: always
    volumes:
      - "./app2/:/dist"

Commands:

docker-compose version view version

docker-compose up start project

docker-compose down stop project

docker-compose logs -f view logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment