Giter Site home page Giter Site logo

docker-commands's Introduction

Docker Commands

Quick Start Guide

Container vs Image

A Docker Image is a file that contains the actual binaries such as PostgresSQL, Redis, etc. It's the same as installing it directly on the machine.

A Docker Container is actually a virtual environment which has all the configurations installed i.e. original image, setting up environment, required libraries, protocol/network configuration, and everything in order to make the Docker image executable.

Docker Network

A Docker Network is a virtually created network environment where you can run your Docker containers inside (like a isolated piece of containers running in a specified Docker network).

Example can be:

  1. Creating a mongo-network
  2. Pulling mongodb and mongoexpress Docker images
  3. Running both images in their containers inside mongo-network (specifying by passing a flag arg)
  4. mongodb and mongoexpress now running inside mongo-network

Port Binding

Docker port binding is a technique to allow multiple Docker images listen to their respective ports even if they both running on same ports. This is done via port binding command.

How it works?

We bind every Docker image port to listen to their respective HOST (each HOST cannot have some ports shared).

You can think port binding like this: ... -p PORT:HOST ... where PORT is actual the port where your Docker image is listening to and HOST is your machine port bound with Docker image PORT.

This technique is very useful especially when you running two same Docker containers with different versions like PostgesSQL v14.XX and PostgresSQL < v14.XX because both PostgresSQL containers will listen to exact same port which you can't decide which container to connect. Under such scenarios port binding plays a very useful technique.

Commands

Containerization

  • docker pull [image] - to pull image from Docker Hub
  • docker run [image] - to start a Docker container
  • docker run [flags] [image] - to start a Docker container with flags
  • docker start [container_id] - to restart a Docker container
  • docker stop [container_id] - to stop a Docker container
  • docker build -t [container_name]:[version] - to build an image with a Dockerfile
  • docker rm [container_id] - to remove a container

Images

  • docker images - to list all Docker images
  • docker push [image_name]:version - to push a Docker image to Docker registry or any private Docker repository (AWS ECR, etc.)
  • docker tag -a [current_image_name] [new_image_name] - to rename a Docker image

Logs

  • docker ps -a - to get history of all ended/up containers
  • docker ps -a | grep [container_name] - to see history of a specific container
  • docker ps - to list all running containers
  • docker logs [container_id] or [container_name]- to display logs of running container
  • docker exec -it [container_id] /bin/bash - to get access as a root user for Bash
  • docker exec -it [container_id] /bin/sh - to get access as a root user for Shell

Example use-case for starting mongodb container

docker run -d \ --name mongodb \ -p 27017:27017 \ -e MONGO-INITDB_ROOT_USERNAME=admin \ MONGO-INITDB_ROOT_PASSWORD=password \ --net mongo-network \ mongo

Flags:

  • d: Start in detach mode
  • name: Name of the container
  • p: Port binding
  • e: Environment variables
  • net: Specifying which network to run in

docker-commands's People

Contributors

sshehrozali avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.