Skip to content

Instantly share code, notes, and snippets.

@gbroques
Last active October 14, 2018 16:39
Show Gist options
  • Save gbroques/889a87c953aaa4bf5a0b23dee172c387 to your computer and use it in GitHub Desktop.
Save gbroques/889a87c953aaa4bf5a0b23dee172c387 to your computer and use it in GitHub Desktop.
Useful Docker Commands

Building Images

By default, Docker looks for a file named Dockerfile when building images.

  • docker build -t express-prod-i .
    • -t - Optionally tag, or name a docker image to avoid referring to the auto-generated ID (e.g. 50e8dde7e180)
    • . - Look for the Dockerfile in the current directory
    • -f - Override default Dockerfile name

Dockerfile

  • RUN - Is an image build step
  • CMD - Is a command the contanier executes when you launch the built image

List Images

  • docker images

Review Image Layers

  • docker history express-prod-i

Running Image Instances

  • docker run -d --name express-prod-app -p 7000:3000 -v $(pwd):/var/app express-prod-i
    • -d - Detached mode. Don't tie up current terminal.
    • --name - Name the container
    • -p - Map local host port 7000 to the exposed container port 3000
    • express-prod-i - The image we want to generate and run
    • -v - Mount a volume on the host machine.
      • $(pwd):/var/app mounts the present working directory on the host machine to /var/app on the container

Verify Running Containers

  • docker ps - Show running containers
  • docker ps -a - Show ALL containers. Useful when something goes wrong and the container stops.

Check Container Logs

  • docker logs hackershall-prod-app - Check the hackersall-prod-app container logs

Execute a Shell

  • docker exec -it hackershall-prod-app /bin/sh
    • Access a bash shell on a running container
    • -it - interactive terminal

Inspecting Containers

  • docker inspect express-prod-app
    • Shows volume and mount information within "Mounts"

Stopping and Starting Containers

  • docker stop express-prod-app
  • docker start express-prod-app

Deleting Containers

  • docker rm express-prod-app - Delete the express-prod-app container
  • docker rm $(docker ps -a -q) - Delete ALL containers

Deleting Images

  • docker rmi express-prod-i - Delete the express-prod-i image
  • docker rmi $(docker images -q) - Delete ALL images

Excluding Files in an Image

  • You can specify patterns to ignore when building an image within a .dockerignore file

Cleanup

  • docker system prune
    • Remove stopped containers
    • Remove images without any containers
    • Remove orphaned volumes

Docker Compose

  • docker-compose build - Build or rebuild services
  • docker-compose up - Create and start containers
  • docker-compose start - Start services
  • docker-compose stop - Stop services
  • docker-compose down - Stop and remove containers, networks, images, and volumes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment