All you need to know about Docker as a beginner

Oct 27, 2019 00:00 · 642 words · 4 minute read Docker

This is a beginner’s tutorial for using Docker.

1. Why Docker?

Docker is an open platform for developing, shipping, and running applications. With Docker, you can make you applications portable, scalable and run on any platforms. Docker is like a virtual machine, but lighter and more powerful. Docker official website gives a very good introduction about Docker and how to setup Docker on your machine. I also found this beginner’s lab is very helpful to get us started.

2. Setup

You can setup Docker on Mac, Linux, Windows or Cloud following this instruction. Once it’s done, run docker run hello-world in terminal to see if your Docker works or not.

3. Run your first container

After the setup, we can now run our first container using the following commands.

# pull image from Docker Hub
$docker pull alpine
# check images at your local machine
$docker images
# run your images and execute ls command
$docker run alpine ls -l
# check running docker images
$docker ps
# run docker image in background
$docker run -it alpine /bin/sh

A few jargons: * Images: The file system and configuration of our application which are used to create containers. * Containers - Running instances of Docker images — containers run the actual applications. * Docker daemon - The background service running on the host that manages building, running and distributing Docker containers.

3. Create your own Docker images

The general flowchart of creating your own Docker images is as follow: * Write your own app code as usual * Create a requirements.txt file to include all dependencies such as Flask==0.10.1 * Create a Dockerfile to include all the operations you’d like Docker to do

A typical Dockerfile would be like this:

# our base image
FROM alpine:3.5

# Install python and pip
RUN apk add --update py2-pip

# install Python modules needed by the Python app
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# copy files required for the app to run
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/

# tell the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python", "/usr/src/app/app.py"]

Once that’s done, we can do the following to build and run our image:

# Build image
$docker build -t <YOUR_USERNAME>/myfirstapp .
# Run image
$docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp
# Stop image
$docker stop myfirstapp
# Remove image
$docker rm myfirstapp

4. Deploy your Docker image

Of course, we can use docker run to deploy images in our local machine. However, to better handle the containers efficiently and scalable, Kubernetes and Docker Swarm are preferred for Docker container deployment. Because Kubernetes is more popular, we will discuss Kubernetes only here.

All containers in Kubernetes are scheduled as pods, which are groups of co-located containers that share some resources. Furthermore, in a realistic application we almost never create individual pods; instead, most of our workloads are scheduled as deployments, which are scalable groups of pods maintained automatically by Kubernetes. Lastly, all Kubernetes objects can and should be described in manifests called Kubernetes YAML files; these YAML files describe all the components and configurations of your Kubernetes app, and can be used to easily create and destroy your app in any Kubernetes environment.

An example of YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bb-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      bb: web
  template:
    metadata:
      labels:
        bb: web
    spec:
      containers:
      - name: bb-site
        image: bulletinboard:1.0
---
apiVersion: v1
kind: Service
metadata:
  name: bb-entrypoint
  namespace: default
spec:
  type: NodePort
  selector:
    bb: web
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30001

Then we can do

#deploy your application to Kubernetes:
$kubectl apply -f bb.yaml
#list all deployments
$kubectl get deployments
#check services
$kubectl get services

It’s done!

5. Docker cheat sheet

Finally, you can find a Docker cheat sheet here. Enjoy!

tweet Share