Skip to content

Instantly share code, notes, and snippets.

@noelmathewisaac
Last active January 28, 2024 16:29
Show Gist options
  • Save noelmathewisaac/6671f73949a1b1c0fe20dd882f06e906 to your computer and use it in GitHub Desktop.
Save noelmathewisaac/6671f73949a1b1c0fe20dd882f06e906 to your computer and use it in GitHub Desktop.
Docker Commands

Dockerfile

COPY

  • COPY is used to copy contents from the local directory to the container.
  • This is necessary as without it the container will not have any files. Files in .dockerignore are not copied
  • Copy everything to the home container directory: COPY . .

WORKDIR

  • WORKDIR is similar to cd and is used to change the working directory inside the container.
  • Some commands like RUN python ... do not accept relative imports and may require multiple WORKDIR commands to change the path.
  • When using multiple WORKDIR commands the changes to the directory location should be relative to the previous WORKDIR

EXPOSE

  • EXPOSE exposes the specified port and makes it available only for inter-container communication
  • EXPOSE does not make the ports of the container accessible to the host

Docker Compose

Docker Compose is used to make it easy to build and run multiple container with using a single cofiguration file called docker-compose.yml.

Example docker-compose.yml File

version: "3" #This is the version of the docker-composr file format and not the docker-compose tool itself
services:
  #Service without build
  elasticsearch: #This is the service name and all the properties under this will be used to build a container for this service
    image: "docker.elastic.co/elasticsearch/elasticsearch:7.8.0" #The name of the pre-built image to be pulled and used
    container_name: elasticsearch #Name of the container created
    environment: #Environment variables can be specifoed here (specified with -e in the docker build command)
      - discovery.type=single-node
    ports: #Port mapping with the format <host_port>:<container_port>. The container port will be mapped to the host port. This is necessary to access a service like an API from our host.
      - 5000:80 #Here the service is running on port 80 on the container and any request received by the local port 5000 will be passed to port 80 on the container
      - 9200:9200
      - 9300:9300
  #Service with build
  chatbot-api:
    build: . #Specify the relative path to the directory containing the Dockerfile to build this container image. 
    image: chatbot-backend:latest
    container_name: chatbot-backend
    volumes: #Map volume from host to container for use cases like hot reloading.
      - ./:/app
    ports:
      - 8112:8112

Accessing Host IP Address from Container

  • host.docker.internal resolves to the internal IP address used by the host on windows and Mac
  • 172.17.0.1 should be used for Linux

Example

version: '3.7'

services:
  app:
    image: your-app:latest
    ports:
      - "8080:8080"
    environment:
      DB_UPSTREAM: http://${DOCKER_GATEWAY_HOST:-host.docker.internal}:3000
  • The DB_UPSTREAM should point to the host's IP and port 3000. ${DOCKER_GATEWAY_HOST:-host.docker.internal} is the critical piece here. This expression tells docker-compose to either us the environment variable DOCKER_GATEWAY_HOST or use the fallback host.docker.internal when resolving this value.

  • This works out of the box now without anything left to do.

  • If you are running this stack on Linux you need to have the DOCKER_GATEWAY_HOST environment variable set for the Docker gateway host. Simply put this line into your .bashrc (.bash_profile or .zshrc):
    export DOCKER_GATEWAY_HOST=172.17.0.1

Accessing Running Container Shell

  1. Get container id by listing all the running containers with docker ps
  2. Run docker exec -it <container_id> /bin/bash

Install Docker on Ubunutu 18.04

sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu `lsb_release -cs` test"
sudo apt update
sudo apt install docker-ce

Install Docker Compose on Ubunutu 18.04

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version #If you get an error when running docker-compose up, try doing it with sudo

Delete all images that contains a particular substring zulh inside the name, run:

docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' | grep 'zulh')

Optimsing Docker Image Size

Article Link - https://drive.google.com/file/d/1I0pxCgSgKKfz_MO_x24m3alTfJNSJE5D/view?usp=sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment