or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Docker Container CLI
Week 19 Day 2
Lecture Videos 1 (15 minutes)
Watch:
docker container run
(Official documentation)Usage:
Commonly used flags for
docker container run
:--name
--name hello
-p
/--publish
-p 8080:80
-d
-d
--rm
--rm
-it
-it
/-i -t
Pop quiz
Let's run a container based on the nginx image, in detached mode. We want to publish the contents of that container's internal port 80, to my localhost at port 8080.
Let's run a container based on the alpine image and name it "juice". We want to run it with an interactive terminal, and use the command
sh
.Let's run a container based on the ubuntu image and name it "greet_doggo". We want the container to be removed automatically once it is stopped. Let's have it use the command
echo hi juice
.Pop quiz (answer key)
docker container ls
(Official documentation)Purpose: list containers (can be used interchangeably with
docker ps
)-a
flag includes stopped containersUsage:
docker container stop
(Official documentation)Purpose: stop a container (or containers) that is currently running.
Usage:
docker container start
(Official documentation)Purpose: start a stopped container (or containers)
Usage:
docker container rm
(Official documentation)Purpose: remove a container
-f
flag forces removal even if container is runningUsage:
docker container prune
(Official documentation)Purpose: remove all stopped containers
Usage:
docker container exec
(Official documentation)Purpose: execute a command within a running container
-it
flag as container run)Lecture Videos 2 (10 minutes)
Watch:
Docker networks
Networks allow containers to communicate with each other, whether or not they are exposing ports to the host.
Official documentation
What is a network?
Networking with Docker
By default, containers are connected to the "bridge" network.
However, that network won't allow containers to communicate with one another—by default, containers are isolated.
Creating custom networks (Official documentation)
Creating a custom network will allow the containers on that network to interact with each other.
Networking demo [1/2]
Let's create a network, and attach containers to it so we can see how networked containers communicate.
For comparison, we'll use two containers that are on the default network.
Networking demo [1/2]
Lecture videos 3 (20 minutes)
Watch:
Docker volumes/bind mounts
Using bind mounts and volumes allows data to persist even after a container is gone.
Bind mounts directly mount the contents of a folder on your filesystem into your container.
Volumes are managed by Docker—so you wouldn't directly access the files, but can be accessed and modified from within a container.
Options for creating volumes/bind mounts
There are two different types of syntax you can use with
docker container run
to establish volumes and bind mounts. Both flags can create either volumes or bind mounts.-v
and--mount
flags have the same purpose.--mount
is typically preferred as it is considered to be clearer.-v
/--volume
syntax-v
/--volume
syntaxFor bind mounts
-v
/--volume
syntaxFor volumes
--mount
syntaxPass in a series of
<key>=<value>
pairs in any order, separated by a comma.Type must be "bind" for bind mounts, or "volume" for volumes
--mount
syntaxfor bind mounts
for volumes
Bind mounts
Official documentation
Demo: Serve a Website [1/2]
First, create a folder called app in your current directory, and make an empty index.html file inside the folder.
Demo: Serve a Website [2/2]
Once your container is running, visit localhost:8080—you should see a blank page. Add some content to your html page, then save and refresh localhost:8080. Now you can see the new content you added, because the contents of your folder stays in sync with the contents of the folder inside your container.
Volumes
Official documentation
Volumes are managed from within Docker—don't depend on your file structure.
You wouldn't modify the contents directly (only from inside a container), but you can use it to spin up new containers with same contents.
Instead of providing a path to the folder (like you would with a bind mount) you can provide the name of the volume.
Demo: Persist data from a postgres instance that runs in a container
Demo: Persist data from a postgres instance that runs in a container