Learn to build and deploy your distributed applications easily to the cloud with Docker.
# fetch docker image from Docker Hub
docker pull busybox
# list all images
docker images
# run a container based on the image
docker run busybox
# run a container with a command
docker run busybox echo "hello world"
# list all containers
docker ps -a
# run with -it flag to attach us to an interactive tty in the container
docker run -it busybox sh
# clean up containers
docker rm <container_id>
# clean up a bunch of containers
# -a flag to list all containers
# -q flag to only output the container id
# -f flag to filter the output
docker rm $(docker ps -a -q -f status=exited)
# clean up images
docker rmi <image_id>
# pull the image from Docker Hub
docker pull prakhar1989/static-site
# Create a static container
# --rm automatically removes the container when it exits
# -it attaches us to an interactive tty session in the container
docker run --rm -it prakhar1989/static-site
# Hit Ctrl+C to exit the container
# Create a static container with exposing port
# -d flag to run the container in detached mode
# -P flag to publish all exposed ports to random ports
# --name flag to name the container
docker run -d -P --name static-site prakhar1989/static-site
# list ports that are mapped to the container
docker port static-site
# specify a custom port mapping
docker run -d -p 8888:80 --name static-site prakhar1989/static-site
# stop the container
docker stop static-site
docker images
Create a simple Flask application:
git clone https://github.com/prakhar1989/docker-curriculum.git
cd docker-curriculum/flask-app
A Dockerfile is a simple text file that contains a list of commands that Docker clients calls while creating an image.It is a simple wait to automate the image creation process.
# Use an official Python runtime as a parent image
FROM python:3.8
# Set the working directory to /app
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /app
COPY . .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Specify the port number the container should expose
EXPOSE 5000
# Run app.py when the container launches
CMD [ "python", "./app.py" ]
Build the image:
# build the image
# -t flag to tag the image
# . flag to specify the current directory which contains the Dockerfile
docker build -t cellinlab/catnip .
Publish the image to Docker Hub:
# login to Docker Hub
docker login
# tag the image
docker tag cellinlab/catnip cellinlab/catnip:latest
# push the image
docker push cellinlab/catnip
Dockerrun.aws.json
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "cellinlab/catnip",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "5000",
"HostPort": "9000"
}
],
"Logging": "/var/log/nginx"
}
Clone the repo:
git clone https://github.com/prakhar1989/FoodTrucks
cd FoodTrucks
The flask-app folder contains the Python application, while the utils folder has some utilities to load the data into Elasticsearch.
tree -L 2
# output:
# .
# ├── aws-ecs
# │ └── docker-compose.yml
# ├── docker-compose.yml
# ├── Dockerfile
# ├── flask-app
# │ ├── app.py
# │ ├── package.json
# │ ├── package-lock.json
# │ ├── requirements.txt
# │ ├── static
# │ ├── templates
# │ └── webpack.config.js
# ├── README.md
# ├── setup-aws-ecs.sh
# ├── setup-docker.sh
# ├── shot.png
# └── utils
# ├── generate_geojson.py
# └── trucks.geojson
We need two containers. One for the Python application and one for Elasticsearch.
# search for elasticsearch image
docker search elasticsearch
# pull the image
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2
# run the container
# -d flag to run the container in detached mode
# --name flag to name the container
# -p flag to publish the container port to the host
# -e flag to set environment variables
docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
# check if the container is running
docker container ls
# check the logs
docker container logs es
# check if Elasticsearch is running
curl localhost:9200
Dockerfile
for the flask app:
# start from base
FROM ubuntu:18.04
LABEL maintainer="Cell <cellinlab@gmail.com>"
# install system-wide deps for python and node
RUN apt-get -yqq update
RUN apt-get -yqq install python3-pip python3-dev curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install -yq nodejs
# copy our application code
ADD flask-app /opt/flask-app
WORKDIR /opt/flask-app
# fetch app specific deps
RUN npm install
RUN npm run build
RUN pip3 install -r requirements.txt
# expose port
EXPOSE 5000
# start app
CMD [ "python3", "./app.py" ]
Build the image:
# build the image
docker build -t cellinlab/foodtrucks-web .
# run the container
docker run -P --rm --name foodtrucks-web cellinlab/foodtrucks-web
Flask app was unable to connect to Elasticsearch.
When docker is installed, it creates there three networks automatically:
docker network ls
# output:
# NETWORK ID NAME DRIVER SCOPE
# 3ba686ae5939 bridge bridge local
# 872df209dfe2 host host local
# 295b09245ca6 none null local
# inspect the bridge network
docker network inspect bridge
The bridge network is the network in which containers are run by default.The bridge network is shared by every conatiner by default.
Create a new network:
# create a new network
docker network create foodtrucks-net
# check if the network is created
docker network ls
# output:
# NETWORK ID NAME DRIVER SCOPE
# 3ba686ae5939 bridge bridge local
# f7ba51f89c4a foodtrucks-net bridge local
# 872df209dfe2 host host local
# 295b09245ca6 none null local
A bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.
# run the elasticsearch container in the new network
docker conatiner stop es
docker conatiner rm es
docker run -d --name es --net foodtrucks-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
# check if the container is joined to the network
docker network inspect foodtrucks-net
Try to run the flask app container in the new network:
docker run -it --rm --name foodtrucks-web --net foodtrucks-net cellinlab/foodtrucks-web bash
# check if the app can connect to elasticsearch
curl es:9200
# output:
# {
# "name" : "zFgYJzl",
# "cluster_name" : "docker-cluster",
# "cluster_uuid" : "C9llxDniSImHAqupRj5bjg",
# "version" : {
# "number" : "6.3.2",
# "build_flavor" : "default",
# "build_type" : "tar",
# "build_hash" : "053779d",
# "build_date" : "2018-07-20T05:20:23.451332Z",
# "build_snapshot" : false,
# "lucene_version" : "7.3.1",
# "minimum_wire_compatibility_version" : "5.6.0",
# "minimum_index_compatibility_version" : "5.0.0"
# },
# "tagline" : "You Know, for Search"
# }
Launch the flask app container in the new network:
docker run -d --net foodtrucks-net -p 5000:5000 --name foodtrucks-web cellinlab/foodtrucks-web
# check if the container is running
docker container ls
# check the app
curl -I 0.0.0.0:5000
Do these steps in a bash script:
setup-docker.sh
:
#!/bin/bash
# build the image
docker build -t cellinlab/foodtrucks-web .
# create a new network
docker network create foodtrucks-net
# run the elasticsearch container in the new network
docker run -d --name es --net foodtrucks-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
# run the flask app container in the new network
docker run -d --net foodtrucks-net -p 5000:5000 --name foodtrucks-web cellinlab/foodtrucks-web
Compose is a tool for defining and running multi-container Docker applications with YAML files.
Compose works in all environments: production, staging, development, testing, as well as CI workflows.
Install docker-compose:
# install docker-compose
pip install docker-compose
# check if docker-compose is installed
docker-compose --version
Docker Compose file docker-compose.yml
:
version: "3"
services:
es:
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
container_name: es
environment:
- discovery.type=single-node
ports:
- 9200:9200
volumes:
- esdata1:/usr/share/elasticsearch/data
web:
# image: cellinlab/foodtrucks-web
build: . # build the image from the Dockerfile
command: python3 app.py
depends_on:
- es
ports:
- 5000:5000
volumes:
- ./flask-app:/opt/flask-app
volumes:
esdata1:
driver: local
Run the docker-compose file:
# run the docker-compose file
docker-compose up -d
# check
docker-compose ps