docker basics

install docker

Docker can be installed on multiple platforms, and comes in two different forms:

The installation process is different for each platform, but very straightforward.

Alternatively you can create a scalable Ubuntu 18.04 droplet with Docker pre-installed on Digital Ocean. That is exactly what I did, it works great, and you can create and delete droplets within seconds to separate or scale your services. If you go this route I recommend that you start with at least a 2GB RAM droplet.

To see if Docker is installed use this in your command prompt:

theory

Know the difference between an image and a container! Its what confused me most. Think of an image as a CD and a container as a CD reader. Containers are temporary instances that run an image, nothing gets saved in a container or on an image. All files/folders in a running container can be replaced real-time by local files/folders during development and production. You can mirror a folder or a file to a running container where data will be saved and can develop continuously in a local folder and the container will run it. <<<<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT REDO

docker image

Docker uses layers to built images, it's an incredibly clever system because it reduces the space images take on your hard drive. Docker Hub has massive list of images created by Docker and its community. You can pull a bare Linux image (options available), or an image that packs a server that will launch an SQL database without any extra configuration.

I built an image with dependencies that I use on all of my science projects. You can pull it from Docker Hub by running this command: docker pull ccci-io/sci

Bellow is a small tutorial on how I built my ccci-io/django image that I use as base for all of my engineering projects. I use a Ubuntu 18.04 server, but the process should be the same for almost all Linux platforms

Open up a command prompt on the device that you have Docker installed. I use PuTTY SSH to connect to my server.

Navigate to your root directory

cd ~                    # Go to your home directory
mkdir my-folder         # Create folder myproject
cd my-folder            # Go to myproject
touch Dockerfile        # Create file named Dockerfile

Now open Dockerfile in an IDE of your choice and enter following code. My IDE preference is Visual Studio Code.

FROM python:3.7                     # Base image          
 
LABEL maintainer="i@ccci.io"        # Email label
LABEL "ccci.io"="CC Code Island"    # Site and name label
LABEL version="1.0.8"               # Version label

WORKDIR /docked-folder/             # Equivalent to cd /docked-folder/

RUN pip3 install Django djangocms-installer django-cms
RUN pip3 install djangorestframework drf-firebase-auth django-cors-headers
RUN pip3 install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
RUN pip3 install uwsgi

# You can also list modules in requirements.txt and run this:
#COPY ./requirements.txt /docked-folder/requirements.txt
#RUN pip3 install -r ./requirements.txt

CMD python3 /docked-folder/start.py     # This command will run every time you start your container

Find more about Dockerfilehttps://docs.docker.com/engine/reference/builder/

Basic docker image manipulations:

docker build -t myimage .   # Build image with myimage tag (don't forget the dot)
docker images               # List docker images in the local repository
docker rmi myimage          # Remove myimage from the local repository

Find more docker image commands @ https://docs.docker.com/engine/reference/commandline/image/

docker container

I always run my containers using Docker Compose, but you should know how to run it from command line.

docker container run -p 8000:8080 -v ~/my-folder/ /docked-folder/ -d --name mycontainer myimage
-p 8000:8080 Proxy or expose the active port 8080 inside the container, to be available as 8000 outside.
-v ~/my-folder/ /docked-folder/ Share your local folder my-folder with the container as /docked-folder/
-d Detached mode (background mode).
--name mycontainer  Create a callable name for your container.
myimage At the end of the command write the name of your image.

Find more docker run commands @ https://docs.docker.com/engine/reference/commandline/run/

Find more docker container commands @ https://docs.docker.com/engine/reference/commandline/container/

Now remove the container and let's run it with Docker Compose.

docker-compose

Now that you understand basics in images and containers, let me show you how I develop. I use Docker Compose to make my projects reproducible, clean and orderly.

Docker Compose makes Docker a powerful development environment primarily ballows you to run multiple containers that can interact together, but you can use it to run single containers.

version: '3'
 
services:
  cc:       # Name that will be used for container interactions / internal IP address
    container_name: mycontainer     # Callable name for your container
    image: myimage                  # Docker image
    ports:
    - "8000:8080"                   # Port proxy
    expose:
    - "8080"                        # Container port expose
    volumes:
    - ~/my-folder:/docked-folder    # Shared folder between local machine and container
    restart: always                 # Restart always
    command: bash -c "python3 /usr/share/tt/wsgi.py"
    # Command that will run every time the container starts
  nginx:
    image: nginx            # This image is on docker hub repositories and
                            # will be downloaded if you never ran it before
    container_name: nginx-proxy     # Callable container name
    ports:
    - "80:80"                       # Exposing HTTP port
    - "443:443"                     # Exposing HTTPS/SSL port
    volumes:
    - /etc/letsencrypt:/etc/letsencrypt
    - ~/nginx:/etc/nginx/conf.d
    depends_on:
    - cc                            # nginx will start after cc starts
    restart: always
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"

Find more about docker-compose @ https://docs.docker.com/compose/reference/overview/

docker hub repository

Docker Hub Repository

hi