Skip to content

Instantly share code, notes, and snippets.

@khansun
Last active September 26, 2022 10:23
Show Gist options
  • Save khansun/f79f01aefbc8bcc5d0638a9f870612e3 to your computer and use it in GitHub Desktop.
Save khansun/f79f01aefbc8bcc5d0638a9f870612e3 to your computer and use it in GitHub Desktop.
Useful docker commands

Useful Docker Commands

Docker Run:

  • Pull image from DockerHub: docker pull [imageID]
  • Run image in detached mode along with a command: docker run -d [imageID] [command]
  • Attatch a running container: docker attatch [containerID]
  • Show Running Containers: docker ps
  • Show All Containers: docker ps -a
  • Kill running container: docker stop [ContainerID/Name]
  • Remove container: docker rm [ContainerID/Name]
  • Show stored images: docker images
  • Remove stored images: docker rmi [imageID/Name]
  • Execute a command on a running container: docker exec [containerID] [command]
  • Run a specific version of an image: docker run [imageID:tag]
  • Run interactive mode: docker run -i [containerID]
  • Run interactive mode and attatch terminal: docker run -it
  • Run image with an environment variable and assign container name: docker run -e [variableName]=[value] --name [containerName]
  • Port mapping: docker run -p [externalPort]:[internalPort] [containerID]
  • Volume mapping: docker run -v [targetDir]:[internalDir] [containerID]
  • Inspect Conainer as JSON: docker inspect [containerID]
  • Container logs: docker logs [containerID]

Docker Images:

  • Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3 python3-pip
RUN pip install flask
RUN pip install flask-mysql

COPY . /opt/app.py
ENTRYPOINT ["FLASK_APP=/opt/app.py", "flask", "run", "--host=0.0.0.0"]
  • Build Image:
    • docker build Dockerfile -t username/[myApp]
    • docker push username/[myApp]

Docker Compose:

  • From the directory containing the following docker-compose.yml file:
    • docker compose up [OPTIONS] [SERVICE...]
# version is now using "compose spec"
# v2 and v3 are now combined!
# docker-compose v1.27+ required

services:
  vote:
    build: ./vote
    # use python rather than gunicorn for local dev
    command: python app.py
    depends_on:
      redis:
        condition: service_healthy 
    volumes:
     - ./vote:/app
    ports:
      - "5000:80"
    networks:
      - front-tier
      - back-tier

  result:
    build: ./result
    # use nodemon rather than node for local dev
    command: nodemon server.js
    depends_on:
      db:
        condition: service_healthy 
    volumes:
      - ./result:/app
    ports:
      - "5001:80"
      - "5858:5858"
    networks:
      - front-tier
      - back-tier

  worker:
    build:
      context: ./worker
    depends_on:
      redis:
        condition: service_healthy 
      db:
        condition: service_healthy 
    networks:
      - back-tier

  redis:
    image: redis:5.0-alpine3.10
    volumes:
      - "./healthchecks:/healthchecks"
    healthcheck:
      test: /healthchecks/redis.sh
      interval: "5s"
    ports: ["6379"]
    networks:
      - back-tier

  db:
    image: postgres:9.4
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"
    volumes:
      - "db-data:/var/lib/postgresql/data"
      - "./healthchecks:/healthchecks"
    healthcheck:
      test: /healthchecks/postgres.sh
      interval: "5s"
    networks:
      - back-tier

volumes:
  db-data:

networks:
  front-tier:
  back-tier:


Docker Network

  • Show docker networks: docker network ls
  • Inspect docker network: docker network inspect [NetworkType]
  • Run a container named alpine-2 using the alpine image and attach it to the none network: docker run --name alpine-2 --network=none alpine
  • Create a new network named wp-mysql-network using the bridge driver: docker network create --driver bridge --subnet 182.18.0.1/24 --gateway 182.18.0.1 wp-mysql-network
  • Inspect the created network: docker network inspect wp-mysql-network
  • Deploy a mysql database using the mysql:5.6 image and name it mysql-db. Attach it to the newly created network wp-mysql-network. Set the database password to use db_pass123. The environment variable to set is MYSQL_ROOT_PASSWORD: docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db --network wp-mysql-network mysql:5.6
  • Deploy a web application named webapp using the kodekloud/simple-webapp-mysql image. Expose the port to 38080 on the host: docker run --network=wp-mysql-network -e DB_Host=mysql-db -e DB_Password=db_pass123 -p 38080:8080 --name webapp --link mysql-db:mysql-db -d kodekloud/simple-webapp-mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment