Giter Club home page Giter Club logo

my-frequent-docker-commands's Introduction

My frequent Docker commands

πŸ“œ list containers/images

  • docker ps -a ← show even stopped containers
  • docker images -a ← show even unused images

βœ… build/run an image

  • docker build -t abc . ← build a container in a current directory
    • docker build -f docker/base/Dockerfile -t abc . ← build a Docker image from another path
    • docker build -f docker/def/Dockerfile --build-arg BASE_IMAGE=abc -t def ← pass a build argument
  • docker run -it abc bash ← run Docker image in an interactive bash shell
    • docker run --name xyz -it abc bash ← give it a name
  • docker exec -it xyz bash ← get into a running container (first make sure to docker start xyz if stopped)

πŸ’Ύ mount volume to a container (docs)

  • docker run --name xyz -v $(pwd):/app -it abc bash ← mount current directory to a container and run it
    • you'll be able able to access your local folder in the /app folder of the Docker image
    • ⚠ if you're using a terminal on Windows, make sure to replace $(pwd) (see more info in a section below)

πŸ’» develop through a container

  1. docker pull python:3.8 ← pull the latest Python image from DockerHub
  2. cd <project_directory> ← move into the chosen project directory
  3. docker run --name xyz -v $(pwd):/app -it python:3.8 bash ← run a container named "xyz" while mounting the current directory. NOTE: These steps were executed on a macOS device. Mounting files within a docker container on a Linux machine may modify the file owner. You could try mitigating it with the suggested workarounds here or here.

🌐 publish a container (e.g. to use REST/curl)

  • docker run -it --rm -p 8080:80 abc ← expose port 8080 (inside container) to port 80 (on the host), and automatically remove the container after exiting
  • docker run -d -p 8080:80 abc ← run in a detached mode (detach from the container and return to the terminal prompt)

πŸšΆβ€β™‚ exit a container

  • [CTRL] + [D] or exit ← exit and stop the container
  • [CTRL] + [P] + [Q] ← exit without stopping the container

❌ delete containers/images

  • docker system prune -a --volumes ← remove all (stopped containers, unused images, volumes)
  • docker stop $(docker ps -a -q) ← stop all running containers
    • docker stop <container_id> ← stop a running container
  • docker rm $(docker ps -a -q) ← remove all stopped containers
    • docker rm -f <id/name> ← remove a container forcefully
  • docker rmi $(docker images -q) ← remove all images
    • docker rmi -f <image_id> ← remove image by its ID forcefully
    • docker image rm -f <name> ← remove image by its name
  • docker volume rm $(docker volume ls -q) ← remove all volumes

⚠ running Docker commands on Windows

  • cmd.exe
    • replace $(pwd) with %cd%
    • replace any newline characters \ with ^. If you're using bash -c "…", then add ^ and make a newline before the first "
  • PowerShell
    • replace $(pwd) with ${pwd}
    • replace any newline characters \ with ` (but not the ones within bash -c "…")
  • Git Bash
    • replace $(pwd) with /$(pwd)
    • you may need to precede the command with winpty (at least not in the VS Code terminal)
    • it's safer to use bash instead of /bin/sh
    • if you're specifying a working directory, instead of -w /app use -w "//app"

You can find all the commands in the official Docker CLI Reference.

my-frequent-docker-commands's People

Contributors

pyxelr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.