Docker

If you unfamiliar with Docker and containers, you can read the following article https://www.cio.com/article/2924995/what-are-containers-and-why-do-you-need-t

Install docker on mac

Docker

brew install docker

Check versions

docker version

docker-compose version

docker-machine --version

Autocomplete

see https://docs.docker.com/compose/completion/

In particular for .zshrc with oh-my-zsh:

plugins=(... docker docker-compose )

You may need to remove .zcompdump-(...) files by

rm ~/.zcompdump*

Test it

docker run hello-world

Basic commands

Run docker container

This case nginx with name webserver

docker run --detach --publish=80:80 --name=webserver nginx

View containers

Running

docker container ls

All

docker container ls -a

Stop container by name

docker container stop webserver

Remove container

docker container rm webserver

View images

docker image ls

Remove image

by RESPOSITORY

docker image rm nginx

Dockerfile: Flask app

First we create a directory sample_flask_app. Inside it we will create the following files:

  1. Dockerfile
  2. hello_world_app.py
  3. requirements.txt

Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "hello_world_app.py"]

hello_world_app.py

Create hello_world_app.py with the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello world"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

requirements.txt

And finally requirements.txt

Flask

Build

docker build --tag=flaskhelloworld .

Check if it is created:

docker image ls

Run

Well, as before

docker run  --detach --publish=4001:80 --name=flaskhelloworld flaskhelloworld

Test it

Now you can go to http://localhost:4001 or simply

curl http://localhost:4001

Login into docker

docker exec -it flaskhelloworld /bin/bash

Push docker to docker hub

On https://hub.docker.com/ create account if you do not have one. Note that, in what follows, <username> is your username from docker hub.

docker tag flaskhelloworld <username>/flaskhelloworld:1.0.0
docker push <username>/flaskhelloworld:1.0.0

This push refers then to repository docker.io/<username>/helloworldflask. You should be able to see the repo in your account on https://hub.docker.com/repositories. Moreover you should be able to run the container without building it.

docker run  --detach --publish=5000:80\
    --name=flaskhelloworld <username>/flaskhelloworld

Travis

You can add .travis file to automatically build docker and push it docker hub. Remember to add credential to travis.

language: python
python:
  - 3.6.8
services:
  - docker
before_install:
  - docker build --tag $DOCKER_USERNAME/flaskhelloworld .
  - docker images
script:
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
  - docker push $DOCKER_USERNAME/flaskhelloworld

Other useful commands

Docker inspect

docker inspect flaskhelloworld

Kill all dockers

docker kill $(docker ps -q)

Delete all containers

docker rm $(docker ps -a -q)

Delete all images

docker rmi $(docker images -q)

Links

Updated: 2019-12-21