• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    Dockerfile
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ‹ wtf is docker and why is everyone talking about it

πŸ‹ ELI5: Docker

This is a small repo which serves as an example/tutorial walking through what Docker is, why people use it, and a few basic examples.

Workshop

I also gave a workshop on this repo at Hack the North 2020++! You can find the recording at this YouTube link.

Table of Contents

πŸ‘‹ Introduction

What is Docker?

Docker is a tool that makes it really easy to package applications into self-sustaining 'containers'.

What are containers?

Containers, as their name suggests, contain things. In the case of Docker, these contain all the parts the application needs to run, everything from libraries and dependencies to the actual source code.

Why containers?

Containerization means that everything to do with your application stays inside the container. You shouldn't need to worry about how stuff on your machine (e.g. which version of Python you have) affects how your program runs. As a side benefit, this means that Docker containers are dependency-free. Never worry about "oh, it works on my machine" ever again! After a Docker image is created, all of its contents are frozen so it should work exactly the same on your computer as it does for someone else (assuming you both have Docker).

Why Docker?

Docker makes it super easy to work with these containers and, by proxy, you can get all the cool benefits of containers easily too! It also allows you to programmatically define a container through code, meaning you can collaborate and work on Docker containers just as you would with a regular piece of code through version control like git.

πŸ”¨ Installing Docker

More detailed instructions can be found here.

βš™οΈ Parts of Docker

Docker Containers

I think the intro covered this pretty well so I'll repeat it again here.

Containers, as their name suggests, contain things. In the case of Docker, these contain all the parts the application needs to run, everything from libraries and dependencies to the actual source code.

This means that 'containerized' applications don't need to rely on a system to have certain dependencies (e.g. Node.js) installed on the user's system to run because the container will have it packaged.

You can think of Docker containers like a fully self-contained and running version of your application.

Docker Image

You can think of Docker images like a sort of 'template' that describes to Docker how to create a container from scratch. You can build these images by providing instructions on how to build them in the form of layers.

Layers

Docker images, like ogres (or cakes if you're a boring person), have many layers. The base layer often provides some basic functionality like providing git, bash or apt -- otherwise your container has nothing to run off of! We can then add our own layers on top of that base layer, like installing dependencies, copying files into the image, and defining the command to run when the container starts up. These instructions are programmatically defined through a Dockerfile.

One of the coolest parts of Docker is that these layers get cached between builds if nothing has changed. That means that if you rebuild an image and only changed the last layer, it'll only need to rebuild the last layer rather than rebuilding the whole image, making for some really fast iteration times.

Dockerfile

The Dockerfile are the actual specific instructions for how to create the actual image or 'template'. The Dockerfile starts off by defining a 'base-layer', which serve as the basis for your actual image. Some common base layers are ubuntu (which contains a minimal install of the actual Ubuntu operating system) and python (which contains everything needed to run a basic Python app).

I won't dive into too much details about each command you can use as these will be described more in-depth within the examples. You can find detailed documentation on the commands you can use in a Dockerfile here: https://docs.docker.com/engine/reference/builder/

Docker CLI

Great, so now I know a little bit about how Docker actually works, how do I get started? First, let's make sure our Docker install works correctly. You can do this by opening your favourite terminal and entering docker run hello-world. You should get something that looks like:

$ docker run hello-world

Hello from Docker.
This message shows that your installation appears to be working correctly.
...

Now, let's go over a few basic CLI commands that you'll probably be using as you work with Docker.

docker build

How do I turn a Dockerfile into an actual image?

To built an image, you can do docker build . -t <name-of-image> which tells Docker to look in the current directory for a file called Dockerfile and to follow the instructions inside to build an image. After doing so, tag the image so we can easily find it later. Docker image tags let you version your images as well. Say you wanted to build a v1 for your image, you would do docker build . -t <name-of-image>:v1. If you have a different name for your Dockerfile, you can also refer to it using the -f flag like so: docker build . -t <name-of-image> -f <name-of-dockerfile>

More info can be found here: https://docs.docker.com/engine/reference/commandline/build/

docker image ls

How do I get a list of all the images I've built?

The command will give you an output that looks something like the following.

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
image1                     0.1.1               9eb95c7f06b0        2 days ago          343MB
image2                     <none>              c99ac06cf60a        2 days ago           23MB
...

More info can be found here: https://docs.docker.com/engine/reference/commandline/image_ls/

docker run

How do I create a container from an image?

Now that you've built an image, you can just run it by doing docker run <name-of-container>:<version>. More often than not, you can just use the latest version of the image, docker run <name-of-container>:latest. However, certain applications (like servers) need to listen on specific ports. By default, Docker doesn't allow containers to use ports on your local machine, but you can allow this by specifying ports using the -p flag, docker run -p <host-port>:<container-port> <name-of-container>. If your container listens on port 3000, but you want it to appear as port 5000 on your local machine, it would look like docker run -p 5000:3000 <name-of-container>

If you want to run your container in the background in a detached manner, you can just add the -d flag.

More info can be found here: https://docs.docker.com/engine/reference/commandline/run/

docker ps

How do I figure out what containers are currently running?

You can get a list of currently running containers by doing docker ps, which will give you each container running along with details about its Container ID, what image it was created from, when it was created, as well as which ports are open.

docker exec

How do I run a command inside a container?

You can use the command docker exec -it <container name> /bin/bash to get a bash shell in the container, allowing you to run commands from within the container as if it was a full-fledged machine. If you know specifically what command you want to execute, you can use docker exec -it <container name> <command> to execute whatever command you specify in the container.

πŸ“ Docker Examples

  1. Basic Python app with dependencies
  2. Node.js and Express app
  3. Multi-stage Go app
  4. Rust Deps Caching
  5. Production-grade React with NGINX

πŸ“š Further reading

These topics will not be talked about within this repository, but I've added a few resources I've found helpful in my understanding of each of them.

Docker Compose

Docker Compose is a tool that lets you start multiple Docker containers together and configure how they interact.

Kubernetes and Microservices

mIcRoSerViCeS you may hear them say. What's all the hype about? Basically, its the single responsibility principle but applied to services. This means that each responsibility should, ideally, be split out into its own service and be completely responsible for that one thing. This lends itself really easily to Docker and containers. Kubernetes is a system that makes it really easy to deploy, scale, and manage a bunch of containers, making it near ideal in creating a microservices architecture using Docker.

Deploying to the Cloud

Now that you got some cool new containers? How do I run them in the Cloud like all the other cool kids? Thankfully, Docker makes this super easy too.

Google Cloud Run (GCR)

https://cloud.google.com/run

GCR completely manages scaling and deploying your containers for you so you don't need to worry about server management (yay serverless)! Simply upload your images to Google Container Registry, and create a new Cloud Run deployment.

Google Kubernetes Engine (GKE)

https://cloud.google.com/kubernetes-engine

Want to run your own Kubernetes cluster and have a lot of money to burn? GKE may be right for you! Other than the price, GKE is super user friendly and makes it really easy to manage and visualize your deployments.

Amazon Fargate

https://aws.amazon.com/fargate/

Think GCR but Bezos.

Closing

Thanks for coming to my TED talk. I hope you learned a thing or two about Docker! If you found anything the be incomplete, poorly explained, or just garbage, feel free to open an issue about it (and maybe even make a PR improving it)! If you liked it, feel free to give it a ⭐ so both you and I can feel fuzzy inside.

More Repositories

1

quartz

🌱 a fast, batteries-included static-site generator that transforms Markdown content into fully functional websites
TypeScript
6,508
star
2

portal

πŸ”— zero-config peer-to-peer encrypted live folder syncing that respects your `.gitignore`
TypeScript
364
star
3

cursor-chat

πŸ’¬ cursor chat Γ  la Figma for digital co-existing + presence
TypeScript
219
star
4

bft-json-crdt

🏰 the first JSON-like Byzantine Fault Tolerant CRDT
Rust
201
star
5

hugo-obsidian

simple GitHub action to parse Markdown Links into a .json file for Hugo
Go
148
star
6

ctrl-v

πŸ“‹ a modern, open-source pastebin with latex and markdown rendering support
JavaScript
117
star
7

tabspace

✍️ A scratchspace for your new Tab page
TypeScript
115
star
8

jackyzha0.github.io

✨ website v3
TypeScript
97
star
9

telescopic-text

πŸ”­ an open-source library to help with creating expandable text
TypeScript
88
star
10

nanoDB

πŸ’Ύ a simple, easy, and debuggable document database for prototyping and hackathons
Go
74
star
11

miniraft

πŸš£β€β™€οΈ <1kloc, well-documented Raft consensus algorithm implementation
Rust
46
star
12

DroneNet

Decentralized drone swarm communication for search and rescue missions
Python
35
star
13

min-react

Yet another minimal React template
JavaScript
17
star
14

paypaya

[πŸ† 5th place + Best Fintech at HackWestern] PayPaya -- SMS Money Transfer for Low Bandwidth Regions with Biometric Authentication
Python
15
star
15

rs-openai

A Rust crate for easy serving of OpenAI's API with rate limiting and token use tracking out of the box
Rust
12
star
16

discord-steward

🌿 A pace-layered approach to high-volume Discord Servers.
TypeScript
10
star
17

lite.css

a dead-simple just-add-water css library
HTML
10
star
18

template-react

For when create-react-app just doesn't give you what you want.
JavaScript
9
star
19

PacketBook

[πŸ† Top 30 at nwHacks 2018, SAP iXP Prize] PacketBook SMS financial inclusion platform powered by the Stellar Blockchain
JavaScript
7
star
20

play

artifact from interact circle on hackathon culture + play
JavaScript
7
star
21

NEAT-genetic-algo

BIOL111 Group Project
Python
6
star
22

treehacks2020-backend

[πŸ† Azure Prize at TreeHacks] readAR -- 🌲 TreeHacks 2020 Backend
Python
5
star
23

hruid

human-readable base16 IDs
JavaScript
5
star
24

hackTheNorth2018

πŸ“± Tapp.it! - Cryptopayments over NFC
Java
5
star
25

Speech2Braille

[πŸ† Silver Medal at CWSF] Tensorflow Implementation of TIMIT Deep BLSTM-CTC with Tensorboard Support
Python
5
star
26

go-auth-w-mongo

Simple session based authentication with Mux and MongoDB
Go
4
star
27

go-remote-debug

A quick tutorial on debugging containerized Go applications!
Go
3
star
28

htn22

HTN22 - Intro to Computer Networking and Peer-to-peer
HTML
3
star
29

front-proxy-PoC

Simple Envoy Service Mesh with External Authorization and Custom Header Injection.
Go
3
star
30

blog

some mildly coherent ramblings
CSS
3
star
31

jackyzha0

a cool readme
TypeScript
3
star
32

dotfiles

dotfiles n things
Lua
3
star
33

monGo-driver-wrapper

A small Go wrapper to reduce boilerplate of using the official Go Mongo Driver
Go
2
star
34

curius-viz

JavaScript
2
star
35

website-v2

✨ now with templating and other cool stuff ✨
CSS
2
star
36

riverbed

TypeScript
2
star
37

groupCalendar

A Group Web Calendar created with Meteor
JavaScript
1
star
38

python-spacegame

2D Top Down Space Shooter
Python
1
star
39

y-webrtc-signalling

a simple y-webrtc signalling server for use in serverless environments
JavaScript
1
star
40

apcs-spaceshooter

Short project for APCS A
Java
1
star
41

wholesome-bot

A fun little web scrapping project to bring some happiness to the world
HTML
1
star
42

dailycodingproblem

Python
1
star
43

gameoflife

Java Implementation of Conway's Game of Life
Java
1
star
44

playspace

playspace website
HTML
1
star