Docker Commands with Examples for Container Operations

Very useful docker commands with examples for container operations. Here we are presenting a list of docker commands for Image, Container, Volume, Network, docker-compose, docker-machine, Swarm.

Image Docker Commands

Commands
$ docker build --tag .
$ docker build --tag --no-cache .
$ docker images
$ docker images :
ex) docker images mysql:latest
$ docker rmi

I think that it is common when wanting to erase it collectively

# Unused Image
$ docker rmi $(docker images -f "dangling=true" -q)

# Repository specification
$ docker images {REPOSITORY_NAME} -aq | xargs docker rmi

Learn how to use Docker, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Docker Training Demo!

Container Docker Commands

Commands
$ docker run --name -it
$ docker run -td --net
$ docker ps -a
$ docker exec -it bash

# System to erase collectively
$ docker rm $(docker ps -aq)
$ docker rm $(docker ps -aq --filter name=)

Example

# MySQL With Password
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:latest
$ do exec -it some-mysql bash

# MySQL Without Password
$ docker run --name some-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:latest


Volume Docker Interview Questions

Commands
$ docker volume create --name
$ docker volume ls
$ docker volume rm

# System to erase collectively
$ docker volume rm $(docker volume ls --filter name= -q)
$ docker volume rm $(docker volume ls -f dangling=true -q)

Example

# MySQL
$ docker volume create --name hoge
$ docker run --name piyo -d -v hoge:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pw mysql
$ docker exec -it piyo bash

Frequently Asked Docker Interview Questions & Answers

Network Docker Commands

$ docker network ls
$ docker network inspect
docker-compose
$ docker-compose build
$ docker-compose up
$ docker-compose --project-name= up -d
$ docker-compose ps
$ docker-compose stop
$ docker-compose start
$ docker-compose -f up
$ docker-compose exec bash
$ docker-compose rm

# Check log
$ docker-compose logs

docker-machine

$ docker-machine create -d virtualbox manager

$ docker-machine create 
  --driver amazonec2 --amazonec2-region ap-northeast-1 --amazonec2-vpc-id vpc-02c94966 
  --amazonec2-subnet-id subnet-4bcd853d mochizukitest02

$ ll ~/.docker/machine/machines
$ docker-machine ls
$ docker-machine ip manager
$ docker-machine create -d virtualbox worker
$ docker-machine ssh worker
$ docker swarm join 
    --token SWMTKN-1-26vtuh7mtfofdxkj721ap6eftvqw2i3dsd7yrp5vlsft2t2xzu-8sjf8e5z1aojs3l4htw3r0wg7 
    192.168.99.100:2377
$ docker info
$ docker-machine stop worker
$ docker-machine start worker

MindMajix YouTube Channel

Swarm Docker Commands

$ docker-machine ip manager

$ docker ssh manager
$ docker swarm init --advertize-addr
$ docker swarm join-token manager

$ docker ssh worker
$ docker swarm join 
    --token SWMTKN-1-1ydb7x3wyc4ong1y08we9smq61672en9z851rkytcbmfwhj282-1hftyp3smd45bm78k0o33fzp1 
    192.168.99.103:2377
$ docker node ls
$ docker swarm leave

# stack
$ docker-machine ssh manager
$ docker stack deploy --compose-file docker-stack.yml vote
$ docker stack services vote

Explore Docker Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 

List Of MindMajix Docker Courses:

 Kubernetes Administration
 OpenShift
 Docker Kubernetes
 OpenShift Administration

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Docker TrainingNov 12 to Nov 27View Details
Docker TrainingNov 16 to Dec 01View Details
Docker TrainingNov 19 to Dec 04View Details
Docker TrainingNov 23 to Dec 08View Details
Last updated: 03 Apr 2023
About Author

Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.

read less