Skip to content

Latest commit

 

History

History
203 lines (119 loc) · 4.59 KB

docker-basic-commands.md

File metadata and controls

203 lines (119 loc) · 4.59 KB

Docker Basic Commands

## Build Images
  • Create image using current directory’s Dockerfile
    docker build -t image-name .

  • List images
    docker images


Running Container

  • Create new container from specified image
    docker run image-name

  • Assign a name to the container
    docker run --name nick-name image-name

  • Run image with an entry point | override existing entrypoint
    docker run image-name cmd
    docker run image-name --entrypoint cmd

  • Run image in interactive mode
    docker run -it image-name

  • Run image in detached mode
    docker run -d image-name

  • Run image mapping container's port to the host
    docker run -p host-port:container-port image-name


Managing Containers

  • List running containers
    docker ps

  • List all containers
    docker ps -a

  • Stop one or more running containers
    docker stop containerId

  • Start one or more stopped containers
    docker start containerId

  • Fetch the logs of a container
    docker logs containerId

  • Fetch and follow log output of a container
    docker logs -f containerId

  • Run a command in a running container in interactive mode
    docker exec -it containerId cmd

  • Copy files/folders from container to local filesystem
    docker cp containerId:/workdir/file.ext .

  • Copy files/folders from local filesystem to container
    docker cp file.ext containerId:/workdir/

  • Remove container
    docker rm containerId

  • Remove running container
    docker rm -f containerId

  • Remove all running and stopped containers
    docker rm -f $(docker ps -a -q)


Persistant data using Volumes

  • Creates a new volume that containers store data
    docker volume create volume-name

  • Display detailed information on one or more volumes
    docker volume inspect volume-name

  • List volumes
    docker volume ls

  • Create a volume and then configure the container to use it
    docker run -v volume-name:/dir/dir container-name

  • Create mapping between dir in host and container
    docker run -v $(pwd):/workdir container-name


Managing Images

Tagging Images

  • Create tag to the image while building
    docker build -t image-name:tag .

  • Create tag to the image after building
    docker image tag src-image:latest dst-image:tag

Saving & Loading Images

  • Save one or more images to a tar archive
    docker image save -o image-name.tar image-name:tag

  • Load an image from a tar archive
    docker image load -i image-name.tar

Remove Images

  • Remove one or more images
    docker image rm image-name
    docker rmi image-name

  • Remove all images
    docker system prune -a

  • Remove all stopped containers
    docker container prune

  • Remove all dangling images
    docker image prune

  • Remove all unused containers, networks, dangling and unreferenced images
    docker system prune


Docker Swarm

  • Initialize

docker swarm init

  • Join Docker Cluster

```` docker swarm join --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx <manager/worker>:2377 ```

  • List Docker Nodes in Swarm Cluster

docker node ls

  • List all running applications on this Docker host

docker stack ls

  • Run the specified Compose file

docker stack deploy -c <composefile> <STACK_NAME>

  • List the services associated with an app

docker stack services <appname>

  • List the running containers associated with an app

docker stack ps <appname>

  • Tear down an application

docker stack rm <STACK_NAME>alias dstr='docker stack rm'

  • Docker Swarm Service list

docker service ls alias dsls='docker service ls'

  • List the tasks of one or more services

docker service ps <service_name> alias dsp='docker service ps'

  • Docker Swarm Service logs

alias dsl='docker service logs'

  • Remove specific docker swarm service

alias dsr='docker service rm'

  • Remove unused Containers, Images, Network, etc.

alias sprune='docker system prune'

  • Remove unused Volumes

alias vprune='docker volume prune'

  • Create Secret

docker secret create <SECRET_NAME> <SECRET_PATH>

  • Create Config

docker config create <CONFIG_NAME> <CONFIG_FILE_PATH>