Docker Commands Cheatsheet

student cheating during an exam
Docker Cheatsheet

CheatSheet

All Docker Commands in one place

Images

  • docker image ls
  • docker image pull hello-world
  • docker version
  • docker image inspect imagename
  • docker image history imagename
  • docker image run hello-world
  • docker image build -t imageName .
  • docker image history imageName
  • docker image rm imageName
  • docker image tag imageName jonesjalapatgithub/imageName
  • docker login
  • docker image push jonesjalapatgithub/imageName
  • docker image prune

Container

  • docker container create hello-world –name hello
  • docker container ls -a
  • docker container start hello
  • docker container stop hello
  • docker container run –name hello hello-world
  • docker container inspect hello
  • docker container logs hello
  • docker container run –name hello –rm hello-world
  • docker container run –name ubuntu –rm ubuntu date
  • docker container run –name ubuntu -it ubuntu
  • docker container attach ubuntu
  • docker container exec -it ubuntu bash
  • docker container rm ubuntu –force
  • docker container run -itd –name ubuntu ubuntu 
  • docker container run -itd –name mysql -e MYSQL_ROOT_PASSWORD=root mysql
  • docker container run -itd –name nginx nginx
  • docker container run -itd -p 8081:80 –name nginx nginx

Miscellanious

  • docker volume prune
  • docker volume ls
  • docker volume create volumeName
  • docker network inspect bridge
  • docker network create –subnet 192.168.1.0/24 mybridge
  • docker network ls
  • docker network connect mybridge container
  • docker network disconnect bridge container
  • docker container run –name httpd-1 –network host -itd httpd

Facts

Some Facts about Docker

  • By-default Docker runs with 1GB memory
  • Containers are immutable
  • Container shares OS memory – OS Virtualisation
  • Docker Inc started by Solomon Hykes
  • Docker image made of many layers, any update adds new layers
  • Docker uses Union FS for implementing Layered docker images

DockerFile

Create a simple docker File for running a webapp

  • FROM httpd
  • COPY ./index.html /usr/local/apache2/htdocs/
  • EXPOSE 80
  • CMD apachectl -D FOREGROUND

Create a simple docker File for running a Java

  • FROM openjdk:8
  • WORKDIR /src
  • COPY ./Test.java .
  • RUN javac ./Test.java
  • CMD java Test

Multi-Stages Build

Create a multi Stage docker File for running a Java

# build
FROM openjdk:8 AS Stage1
WORKDIR /src
COPY Test.java .
RUN javac Test.java

# execution
FROM alpine
RUN apk add openjdk8-jre
WORKDIR /app
COPY --from=Stage1 /src/Test.class .
CMD java Test

Create a multi Stage docker File for running Angular

FROM node:14.15 AS Stage1

RUN npm install -g @angular/cli
WORKDIR /src
COPY . .
RUN ng build --prod

FROM httpd
COPY --from=Stage1  /src/dist/app1/ /usr/local/apache2/htdocs/
EXPOSE 80
CMD apachectl -D FOREGROUND

Run a MongoDB Container with Volumes

/var/lib/docker/volumes – Volumes location

  • docker container run -itd –name mysql2 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -v myvolume:/var/lib/mysql mysql
  • docker container run -itd –name bindmount -p 8080:80 -v /Users/jonesalapat/jones/work/docker/app1/app3/:/usr/local/apache2/htdocs httpd

Custom Network

Container have address starting from 172.17.0.x, CIDR for Docker container :- 172.17.0.0/16

docker container run -itd –name c3 –network mybridge alpine : RUN a container on your network

DOCKER COMPOSE

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

First APP
Second APP

docker-compose.yml
version: '3'

services: 
  pythonapp: 
    build: ./FirstAPP
    ports:
      - '3000:3000'
  
  nodeapp:
    build: ./SecondAPP
    ports:
      - '4000:4000'

The command to up docker compose file

  •  docker-compose up -d
  •  docker-compose down

DOCKER SWARM :

A Docker Swarm is a group of virtual machines running the Docker and that have been configured to join together in a cluster.
ocker works to maintain that desired state. For instance, if a worker node becomes unavailable, Docker schedules that node’s tasks on other nodes. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container. These act similar to Kubernetes/ ECS etc.

Leave a Comment

Your email address will not be published.

error: Content is protected !!
Scroll to Top