Try   HackMD

Author: Vincent Lau
Note: This material is intended for educational purposes only. All rights reserved. Any unauthorized sharing or copying of this material, in any form, to any individual or party, for any use without prior permission, is strictly prohibited.

Docker Series - Docker Commands

This chapter you will get to know basic docker commands, which can be used in Docker CLI to interact with Docker Daemon.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Docker Daemon is the background service responsible for managing Docker containers, images, volumes, networks, and other Docker resources.

Docker CLI provides a set of commands that allow you to control and manage Docker resources.

Key operations of docker commands

Building Docker images from a Dockerfile: The docker build command is used to build a Docker image based on the instructions specified in a Dockerfile.

Running containers: The docker run command is used to create and start a new container from a Docker image.

Managing containers: The Docker CLI provides commands to start, stop, restart, pause, or remove containers. For example, docker start, docker stop, docker restart, docker pause, and docker rm are some of the commands used for managing containers.

Managing images: You can use the Docker CLI to pull Docker images from a registry using the docker pull command, as well as to list, tag, and remove images. Commands such as docker images, docker tag, and docker rmi are used for image management.

Managing volumes and networks: Docker CLI commands are available for creating, listing, and deleting volumes and networks that are used by containers. For example, docker volume create, docker volume ls, docker network create, and docker network rm are used for managing volumes and networks.

Viewing container logs and executing commands within containers: The Docker CLI allows you to view the logs generated by containers using docker logs, and to execute commands inside running containers using docker exec.

The Docker CLI

Manage images

  1. docker build
# Create an image from a Dockerfile.
docker build [options] . 
    -t "app/container_name"    # image name
  1. docker run
# Run a command in an image.
docker run [options] IMAGE # see `docker create` for options

Manage containers

  1. docker create
docker create [options] IMAGE
  -a, --attach               # attach stdout/err
  -i, --interactive          # attach stdin (interactive)
  -t, --tty                  # pseudo-tty
      --name NAME            # name your image
  -p, --publish 5000:5000    # port map
      --expose 5432          # expose a port to linked containers
  -P, --publish-all          # publish all ports
      --link container:alias # linking
  -v, --volume `pwd`:/app    # mount (absolute paths needed)
  -e, --env NAME=hello       # env vars

Example

# Create a container from an image
$ docker create --name app_redis_1 \
  --expose 6379 \
  redis:3.0.2
  1. docker exec
docker exec [options] CONTAINER COMMAND
  -d, --detach        # run in background
  -i, --interactive   # stdin
  -t, --tty           # interactive

Example

# Run commands in a container.
$ docker exec app_web_1 tail logs/development.log
$ docker exec -t -i app_web_1 rails c
  1. docker start
docker start [options] CONTAINER
  -a, --attach        # attach stdout/err
  -i, --interactive   # attach stdin
  1. docker stop
# Start/stop a container.
docker stop [options] CONTAINER`
  1. docker ps
# Manage containers using ps/kill.
$ docker ps
$ docker ps -a
$ docker kill $ID

Images

Example

  1. docker images
# Manages images
$ docker images
  REPOSITORY   TAG        ID
  ubuntu       12.10      b750fe78269d
  me/myapp     latest     7b2431a8d968
$ docker images -a   # also show intermediate
  1. docker rmi
# Deletes images.
docker rmi b750fe78269d

docker-compose

docker-compose.yml

version: '2'

services:
  web:
    build: .
    # build from Dockerfile
    context: ./Path
    dockerfile: Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: redis

docker-compose commands

docker-compose start
docker-compose stop
docker-compose pause
docker-compose unpause
docker-compose ps
docker-compose up
docker-compose down

sevices

# To view list of all the services runnning in swarm
docker service ls 
# # To see all running services
docker stack services stack_name
# to see all services logs
docker service logs stack_name service_name 
# To scale services quickly across qualified node
docker service scale stack_name_service_name=replicas

clean up

# To clean or prune unused (dangling) images
docker image prune 
# # To remove all images which are not in use containers , add - a
docker image prune -a 
# To prune your entire system
docker system prune 
# To leave swarm
docker swarm leave  
# To remove swarm ( deletes all volume data and database info)
docker stack rm stack_name  
# # To kill all running containers
docker kill $(docekr ps -q ) 

Docker Security

# Command line tool for Docker Scout
docker scout
# Analyzes a software artifact for vulnerabilities

docker scout cves [OPTIONS] IMAGE|DIRECTORY|ARCHIVE
# Display vulnerabilities from a docker save tarball
docker save redis > redis.tar
# Display vulnerabilities from an OCI directory
skopeo copy --override-os linux docker://alpine oci:redis
# Export vulnerabilities to a SARIF JSON file
docker scout cves --format sarif --output redis.sarif.json redis
# Comparing two images
docker scout compare --to redis:6.0 redis:6-bullseye
# # Displaying the Quick Overview of an Image
docker scout quickview redis:6.0

Docker Commands Cheatsheet