Skip to content

Instantly share code, notes, and snippets.

@jgram925
Last active July 18, 2019 18:42
Show Gist options
  • Save jgram925/cc25c92f4ddb1b0e74d353a4c0965b1d to your computer and use it in GitHub Desktop.
Save jgram925/cc25c92f4ddb1b0e74d353a4c0965b1d to your computer and use it in GitHub Desktop.
Docker & Docker-Compose Notes.md

Docker Overview

Docker-Compse & Django Tutorial

DockerHub

Useful Commands

Enter Container CLI: docker exec -it <running container ID> bash

See All Images: docker image ls

See Live Containers: docker container ls (add --all for offline)

###Directory Structure

  • New Directory
    • Dockerfile
    • requirements.txt
    • docker-compose.yaml
    • manage.py
    • Django Project Dir
      • Django Project Files/Sub Dirs

Dockerfile

This file describes the setup of the image. The example below uses the base Python3 image to create the new image. It makes a directory in the image called ‘code’. It also installs the required dependencies using the given requirements.txt file.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Docker-compose.yml

This file is used to start several containers at once. If the containers do not exist they use the associated images to create the container or if they do and have been changed they will recreate the containers from their original state. The example below uses Python3 and starts two services: db and web. DB, is an official Postgres image that is downloaded from DockerHub. Web, builds an image from the current directory and includes several other configurations. The example below will be different depending on if you’re building the image, what machine you are one, and for several other reasons.

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Django Project

The project is created in the same directory and needs to be modified to work with the Postgres image that was imported with the ‘docker-compose.yml’ (the default db settings are listed below). Other settings may need to be changed depending on what other images are imported.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

Running Containers with Docker-Compose

To run a particular docker-compose.yml you must be in the same directory as the file. The commands to run in verbose and detached mode. Docker-compose will recreate the containers if there has been any change to them you can prevent this by using the appropriate flag while starting.

Start in Verbose: docker-compose up

Start in Verbose & Prevent Recreating Containers: docker-compose up -- no-recreate

Start in Detached: docker-compose up -d

Stop in Verbose: ctrl + C

Stop either: docker-compose down

DockerHub Push & Pull

  1. Need to first login to DockerHub (if you don't have an account you must first create one here using the command below).

    docker login

  2. Get the image id using the command below.

    docker image ls

  3. Add a tag to your image using the command below.

    docker tag <image ID> <dockerHubUserName/<imageName>:<versionControl>

  4. Push image to DockerHub using the command below.

    docker push <dockerHubUserName/<imageName>

  5. From another machine use the pull command using the command below.

    docker pull <dockerHubUserName/<imageName>

Running a Pulled Docker Container

As mentioned above docker-compose.yml needs to be changed for each variable (i.e. different machine) to run the proper set of containers. Example yml for the containers that were built in the earlier section using the docker-compose.yml

version: '3'

services:
  db:
    image: postgres
  web:
    image: joswar84/djdock:latest
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    depends_on:
      - db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment