GithubHelp home page GithubHelp logo

haidv35 / docker-cheatsheet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vishnusudarsan/docker-cheatsheet

0.0 1.0 0.0 158 KB

An ultimate guide for Docker with extremely useful docker commands.

docker-cheatsheet's Introduction

Docker CheatSheet

An ultimate guide for Docker with extremely useful docker commands.

Glossary

  • Layer - a set of read-only files to provision the system
  • Image - a read-only layer that is the base of your container.Might have a parent image
  • Container - a runnable instance of the image
  • Registry / Hub - central place where images live
  • Docker machine - a VM to run Docker containers (Linux does this natively)
  • Docker compose - a utility to run multiple containers as a system

Useful Commands

List Docker CLI commands

docker
docker container --help

Display Docker version and info

docker --version
docker version
docker info

List all images that are locally stored with the Docker engine

docker image ls
docker images
docker images --all  /* Show all images (default hides intermediate images) */
docker images -a /* Show all images (default hides intermediate images) */

List Docker containers (running, all, all in quiet mode)

docker container ls
docker container ls --all
docker container ls -aq

List all the running containers

docker ps

List all the networks

docker network ls

Download an image

docker pull image_name

Rename a Docker Container

docker rename node_v1 node_v2

Docker machine commands

Use docker-machine to run the containers Start a machine

docker-machine start machine_name

Configure docker to use a specific machine

eval "$(docker-machine env machine_name)"

Start/Stop Command

Start/Stop a running container

docker [start|stop] container_name

Kill all running containers

docker kill $(docker ps -q)

Pause a Container

docker pause container_name

UnPaise a Paused Container

docker unpause container_name

Build Commands

Build an image from the Dockerfile in the current directory and tag the image

docker build -t myapp:latest . 

Run the app

Create/Start docker container and Create/Start docker contaier in a specific port

docker run <container_name>:<tag_name>
docker run -p <port_name>:<port_name> <container_name>
docker run -d --name <container_name> -p <port> <image_name>:<tag_name> /* Expose container but on a randomly available port */
docker run -d --name <container_name> -p <port_name>:<port_name> <image_name>:<tag_name>

Run command will always create a container. Examples of Run command based on the above order :

docker run -d redis:latest
docker run -p 3000:80 myapp
docker run -d --name redisDynamic -p 6379 redis:latest
docker run -d --name redisHostPort -p 6379:6379 redis:latest

Create and start container, run command

docker run -ti --name container_name image_name command

Link the created container with other containers

docker run --name <container_name> --restart always -p <port_name>:<port_name> --link <container_name_1> --net <network_name> --link <hostname> -d <container_name>

Logging

For more details about a running container

docker inspect <friendly-name|container-id>

Display messages the container has written to standard error or standard out.

docker logs <friendly-name|container-id>

Port information of a container

docker port <friendly-name|container-id> <port>

Delete Commands

Delete an image from the local image store

docker rmi myapp:latest

Delete all running and stopped containers

docker rm -f $(docker ps -aq)

Delete dangling images

docker rmi $(docker images -q -f dangling=true)

Removing all the Containers

docker rm `docker ps --no-trunc -aq`

Delete all images

docker rmi $(docker images -a -q)

Excecute Commands

Create a process inside the container

docker exec -it <conatiner_name> <process_command>

Example

docker exec -it <conatiner_name> bash

Export/Import Data Container

docker export dataContainer > dataContainer.tar
docker import dataContainer.tar

Dockerfile

# Use an official node runtime as a parent image - Define the base image
FROM node:latest

# creating folder for making as work directory
RUN mkdir -p /usr/src/app

# Set the working directory to /usr/src/app
WORKDIR /usr/src/app

# Copy package.json in current directory to work directory 
COPY package.json /usr/src/app/

# Install dependencies specified in package.json
RUN npm install

# Copy entire files in current directory to work directory
COPY . /usr/src/app

# Expose port 3000 for this container
EXPOSE 3000

# Command to start this container
CMD [ "npm", "start" ]

Docker compose syntax

docker-compose.yml file example

version: '3'
services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    image: shippingdocker/app:latest
    networks:
     - appnet
    volumes:
     - .:/var/www/html
    ports:
     - ${APP_PORT}:80
    working_dir: /var/www/html
  cache:
    image: redis:alpine
    networks:
     - appnet
    volumes:
     - cachedata:/data
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
    ports:
     - ${DB_PORT}:3306
    networks:
     - appnet
    volumes:
     - dbdata:/var/lib/mysql
  node:
    build:
      context: ./docker/node
      dockerfile: Dockerfile
    image: shippingdocker/node:latest
    networks:
     - appnet
    volumes:
     - .:/opt
    working_dir: /opt
    command: echo hi
networks:
  appnet:
    driver: bridge
volumes:
  dbdata:
    driver: local
  cachedata:
    driver: local

Create and start containers

docker-compose up

docker-cheatsheet's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.