Docker Basics

Docker allows developers to develop, deploy and run containers – this process of using Linux containers to run applications is called containerization.

An image is an executable that contains everything to run the application: code, libraries, environment variables and config files.

A container is a runtime instance of an image – what an image becomes when it’s loaded into memory.

The steps to install docker for Linux are detailed very clearly on the docker website: https://docs.docker.com/install/linux/docker-ce/ubuntu/


Docker Commands Cheat Sheet

# getting information
docker container --help
docker info
docker image ls

# build an image - Dockerfile must exist in the directory
docker build --tag=whatevername .

# run an image and map exposed port 80 to 4000
docker run -p 4000:80 whatevername

# run the app in the background in detached mode
docker run -d -p 4000:80 whatevername

# list containers
docker container ls

# stop container, first get the id with command above, then
docker container stop id

# login to hub.docker.com
docker login

# tag an image
docker tag whatevername username/repository:tag

# publish the image
docker push username/repository:tag

# pull and run image from the remote repository
docker run -p 4000:80 username/repository:tag

# initializing docker swarm
docker swarm init

# run docker compose file app
docker stack deploy -c docker-compose.yml somename

# list docker services
docker service ls

# list the tasks in a service
docker service ps servicename

# take the app down with
docker stack rm somename

# take down the swarm
docker swarm leave --force

The Dockerfile

Dockerfiles define how the environment inside the container should behave: network config, disk drives, what files to “copy in”. For example:

# import desired version of python                                                 
FROM python:2.7-slim

# set working directory this will be in the runtime container
WORKDIR /app

# copy anything from a host directory into a directory in the container
COPY . /app

# run any commands, maybe to install some dependancies such as
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# expose a port to the world
EXPOSE 80

# define an environment variable
ENV NAME World

# cmd to run when container launches
CMD ["python", "app.py"]

Services

Different pieces of an application in a distributed systems are called services, e.g database service, back-end, front-end, etc. Services can be considered as containers running in production. Scaling an application just increases the number of replicas of a container, with a specific configuration that determines how they interact. This is done using a docker-compose.yml file. This yaml file defines how docker containers behave in production. Installing docker compose is also described on the docker website: https://docs.docker.com/compose/install/. A single container running in a service is called a task. Each is given a unique ID that increment up to the number of repliaces. An example docker-compose.yml file:

version: "3"                                                                                                                                                             
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/get-started:part2
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  # defaults to load-balanced overlay network
  webnet:
Docker Basics

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top