[docker] create and run app on your own container
docker
http://anandmanisankar.com/posts/docker-container-nginx-node-redis-example/
https://hackernoon.com/docker-tutorial-getting-started-with-python-redis-and-nginx-81a9d740d091
https://docs.docker.com/
https://hackernoon.com/docker-tutorial-getting-started-with-python-redis-and-nginx-81a9d740d091
https://docs.docker.com/
[eric_tu@localhost ~]$ sudo docker images
sudo: /var/db/sudo/eric_tu writable by non-owner (040770), should be mode 0700
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for eric_tu:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/node 4 78fee52adcbf 13 days ago 651.1 MB
docker.io/node latest 258031686918 13 days ago 666.6 MB
docker.io/ubuntu latest d355ed3537e9 2 weeks ago 119.2 MB
docker.io/ubuntu 14.04 4a2820e686c4 2 weeks ago 188 MB
docker.io/hello-world latest 1815c82652c0 3 weeks ago 1.84 kB
sudo docker run node ls -l
[eric_tu@localhost ~]$ sudo docker run node ls -l
sudo: /var/db/sudo/eric_tu writable by non-owner (040770), should be mode 0700
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for eric_tu:
total 12
drwxr-xr-x. 2 root root 4096 Jun 20 21:09 bin
drwxr-xr-x. 2 root root 6 Apr 20 21:43 boot
drwxr-xr-x. 5 root root 360 Jul 6 02:33 dev
drwxr-xr-x. 56 root root 4096 Jul 6 02:32 etc
drwxr-xr-x. 3 root root 18 Jun 23 00:57 home
drwxr-xr-x. 9 root root 123 Jun 20 21:09 lib
drwxr-xr-x. 2 root root 34 Jun 20 00:00 lib64
drwxr-xr-x. 2 root root 6 Jun 20 00:00 media
drwxr-xr-x. 2 root root 6 Jun 20 00:00 mnt
drwxr-xr-x. 3 root root 18 Jun 23 00:58 opt
dr-xr-xr-x. 163 root root 0 Jul 6 02:33 proc
drwx------. 3 root root 51 Jun 23 00:58 root
drwxr-xr-x. 4 root root 45 Jul 6 02:33 run
drwxr-xr-x. 2 root root 4096 Jun 20 00:00 sbin
drwxr-xr-x. 2 root root 6 Jun 20 00:00 srv
dr-xr-xr-x. 13 root root 0 Jul 6 01:02 sys
drwxrwxrwt. 2 root root 6 Jun 20 21:09 tmp
drwxr-xr-x. 10 root root 105 Jun 23 00:58 usr
drwxr-xr-x. 11 root root 139 Jun 23 00:57 var
[eric_tu@localhost ~]$ sudo docker run node /bin/sh
sudo: /var/db/sudo/eric_tu writable by non-owner (040770), should be mode 0700
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for eric_tu:
甚麼事都沒發生? docker 在執行完任何命令後都會離開,所以想要持續互動的話就必須加上 -i 讓他不要執行完就exit.
-i, --interactive Keep STDIN open even if not attached
[eric_tu@localhost ~]$ sudo docker run -it node /bin/sh
sudo: /var/db/sudo/eric_tu writable by non-owner (040770), should be mode 0700
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for eric_tu:
# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
#
[eric_tu@localhost ~]$ docker --help
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files
-D, --debug Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help Print usage
-l, --log-level=info Set the logging level
--tls Use TLS; implied by --tlsverify
--tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
--tlscert=~/.docker/cert.pem Path to TLS certificate file
--tlskey=~/.docker/key.pem Path to TLS key file
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on a container, image or task
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry.
logout Log out from a Docker registry.
logs Fetch the logs of a container
network Manage Docker networks
node Manage Docker Swarm nodes
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart a container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
service Manage Docker services
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
swarm Manage Docker Swarm
tag Tag an image into a repository
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
volume Manage Docker volumes
wait Block until a container stops, then print its exit code
Run 'docker COMMAND --help' for more information on a command.
Bulid a web app
-d, --detach Run container in background and print container ID
[root@localhost eric_tu]# docker run -d seqvence/static-site
Unable to find image 'seqvence/static-site:latest' locally
Trying to pull repository docker.io/seqvence/static-site ...
latest: Pulling from docker.io/seqvence/static-site
fdd5d7827f33: Pull complete
a3ed95caeb02: Pull complete
716f7a5f3082: Pull complete
7b10f03a0309: Pull complete
aff3ab7e9c39: Pull complete
Digest: sha256:41b286105f913fb7a5fbdce28d48bc80f1c77e3c4ce1b8280f28129ae0e94e9e
c9df77ea8ff7d41510a3c9617d71345f618175f9934f8c2ba608891e00e26631
[root@localhost eric_tu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9df77ea8ff7 seqvence/static-site "/bin/sh -c 'cd /usr/" About a minute ago Up About a minute 80/tcp, 443/tcp awesome_lalande
8bc3b78d6fa2 node "/bin/sh" 20 minutes ago Exited (0) 15 minutes ago sleepy_austin
409b4d7243bd node "/bin/sh" 21 minutes ago Exited (0) 21 minutes ago mad_leavitt
69efc485614a node "echo hi" 21 minutes ago Exited (0) 21 minutes ago gigantic_aryabhata
fc3ea29bd24a node "ls -l" 23 minutes ago Exited (0) 23 minutes ago pedantic_torvalds
2401da561136 hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago stupefied_boyd
d3d989e51eca ubuntu:14.04 "/bin/bash" 19 hours ago Exited (130) 19 hours ago pensive_jones
e0dfa34694f7 node "/bin/bash" 5 days ago Exited (0) 5 days ago sad_visvesvaraya
7990bb7e2f90 ubuntu "/bin/bash" 5 days ago Exited (0) 5 days ago silly_bartik
93203c4149aa hello-world "/hello" 5 days ago Exited (0) 5 days ago
停止與移除
[root@localhost eric_tu]# docker stop c9df77ea8ff7
c9df77ea8ff7
[root@localhost eric_tu]# docker rm c9df77ea8ff7
c9df77ea8ff7
自訂義
In the above command:
-d will create a container with the process detached from our terminal
-P will publish all the exposed container ports to random ports on the Docker host
-e is how you pass environment variables to the container
–name allows you to specify a container name
AUTHOR is the environment variable name and Your Name is the value that you can pass
-P will publish all the exposed container ports to random ports on the Docker host
-e is how you pass environment variables to the container
–name allows you to specify a container name
AUTHOR is the environment variable name and Your Name is the value that you can pass
[root@localhost eric_tu]# docker run --name static-site -e AUTHOR="Eric" -d -P dockersamples/static-site
Unable to find image 'dockersamples/static-site:latest' locally
Trying to pull repository docker.io/dockersamples/static-site ...
latest: Pulling from docker.io/dockersamples/static-site
Digest: sha256:daa686c61d7d239b7977e72157997489db49f316b9b9af3909d9f10fd28b2dec
ce55eb8983bf946fd1e8098b43260970c8b4c50a26a3b73be028d3c362b3a828
[root@localhost eric_tu]# docker port --help
Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
List port mappings or a specific mapping for the container
Options:
--help Print usage
[root@localhost eric_tu]# docker port static-site
443/tcp -> 0.0.0.0:32768
80/tcp -> 0.0.0.0:32769
http://[YourDockerIP]:32769.
How to find docker ip
docker inspect [CID]
[root@localhost eric_tu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce55eb8983bf dockersamples/static-site "/bin/sh -c 'cd /usr/" 6 minutes ago Up 6 minutes 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp static-site
8bc3b78d6fa2 node "/bin/sh" 31 minutes ago Exited (0) 26 minutes ago sleepy_austin
409b4d7243bd node "/bin/sh" 32 minutes ago Exited (0) 32 minutes ago mad_leavitt
69efc485614a node "echo hi" 32 minutes ago Exited (0) 32 minutes ago gigantic_aryabhata
fc3ea29bd24a node "ls -l" 34 minutes ago Exited (0) 34 minutes ago pedantic_torvalds
2401da561136 hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago stupefied_boyd
d3d989e51eca ubuntu:14.04 "/bin/bash" 20 hours ago Exited (130) 20 hours ago pensive_jones
e0dfa34694f7 node "/bin/bash" 5 days ago Exited (0) 5 days ago sad_visvesvaraya
7990bb7e2f90 ubuntu "/bin/bash" 5 days ago Exited (0) 5 days ago silly_bartik
93203c4149aa hello-world "/hello" 6 days ago Exited (0) 6 days ago pedantic_dijkstra
[root@localhost eric_tu]# docker inspect ce55eb8983bf
[
{
"Id": "ce55eb8983bf946fd1e8098b43260970c8b4c50a26a3b73be028d3c362b3a828",
"Created": "2017-07-06T03:01:24.496455445Z",
"Path": "/bin/sh",
"Args": [
"-c",
"cd /usr/share/nginx/html \u0026\u0026 sed -e \"s/Docker/$AUTHOR/\" Hello_docker.html \u003e index.html ; nginx -g 'daemon off;'"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4244,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-07-06T03:01:25.055523702Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:f589ccde7957fa3ddf76a2eeee4d2f5d687b32176f559b703b6b8cacf6d36bc4",
"ResolvConfPath": "/var/lib/docker/containers/ce55eb8983bf946fd1e8098b43260970c8b4c50a26a3b73be028d3c362b3a828/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/ce55eb8983bf946fd1e8098b43260970c8b4c50a26a3b73be028d3c362b3a828/hostname",
"HostsPath": "/var/lib/docker/containers/ce55eb8983bf946fd1e8098b43260970c8b4c50a26a3b73be028d3c362b3a828/hosts",
"LogPath": "",
"Name": "/static-site",
"RestartCount": 0,
"Driver": "devicemapper",
"MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c171,c903",
"ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c171,c903",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "61",
"DeviceName": "docker-253:0-102807693-e1ae0c377ebe3005dac0d2a58a9a8bb7851f64ba4ef1c72edd29d0a2b3e9fbee",
"DeviceSize": "10737418240"
}
},
"Mounts": [],
"Config": {
"Hostname": "ce55eb8983bf",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"443/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"AUTHOR=Eric",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.9.12-1~jessie"
],
"Cmd": [
"/bin/sh",
"-c",
"cd /usr/share/nginx/html \u0026\u0026 sed -e \"s/Docker/$AUTHOR/\" Hello_docker.html \u003e index.html ; nginx -g 'daemon off;'"
],
"Image": "dockersamples/static-site",
"Volumes": null,
"WorkingDir": "/usr/share/nginx/html",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "20b32752f98c5b13c306d398b8f3c0413979f031cf91119eb6e52b2a74b300e0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"443/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
],
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32769"
}
]
},
"SandboxKey": "/var/run/docker/netns/20b32752f98c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4e509b02739a9357fb921131274dded34789110f3e3a60ce234c173e6c48e8c4",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8b50fd48fa113f350fef11a8b8c55146b33274fe5a22c8edfd3a91b46a438e4f",
"EndpointID": "4e509b02739a9357fb921131274dded34789110f3e3a60ce234c173e6c48e8c4",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
訪問後就可以看到歡迎畫面
[root@localhost eric_tu]# links http://172.17.0.1:32769
自訂義 port mapping
You can also run a second webserver at the same time, specifying a custom host port mapping to the container’s webserver.
[root@localhost eric_tu]# docker run --name static-site-2 -e AUTHOR="Eric 2 " -d -p 8888:80 seqvence/static-site
d35382f231bb792bb1f25874aff3071c834c38311a8c8418e2a41c2431bd9a47
[root@localhost eric_tu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d35382f231bb seqvence/static-site "/bin/sh -c 'cd /usr/" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:8888->80/tcp static-site-2
[root@localhost eric_tu]# links http://172.17.0.1:8888
停止與移除 by name
[root@localhost eric_tu]# docker stop static-site
static-site
[root@localhost eric_tu]# docker rm static-site
static-site
[root@localhost eric_tu]# docker rm -f static-site-2
static-site-2
[root@localhost eric_tu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8bc3b78d6fa2 node "/bin/sh" 48 minutes ago Exited (0) 43 minutes ago sleepy_austin
409b4d7243bd node "/bin/sh" 49 minutes ago Exited (0) 49 minutes ago mad_leavitt
69efc485614a node "echo hi" 49 minutes ago Exited (0) 49 minutes ago gigantic_aryabhata
fc3ea29bd24a node "ls -l" 52 minutes ago Exited (0) 51 minutes ago pedantic_torvalds
2401da561136 hello-world "/hello" 54 minutes ago Exited (0) 54 minutes ago stupefied_boyd
d3d989e51eca ubuntu:14.04 "/bin/bash" 20 hours ago Exited (130) 20 hours ago pensive_jones
e0dfa34694f7 node "/bin/bash" 5 days ago Exited (0) 5 days ago sad_visvesvaraya
7990bb7e2f90 ubuntu "/bin/bash" 5 days ago Exited (0) 5 days ago silly_bartik
93203c4149aa hello-world "/hello" 6 days ago Exited (0) 6 days ago pedantic_dijkstra
[root@localhost eric_tu]#
create and run app on your own container
2.3.1 Create a Python Flask app that displays random cat pix
For the purposes of this workshop, we've created a fun little Python Flask app that displays a random cat .gif every time it is loaded - because, you know, who doesn't like cats?
Start by creating a directory called flask-app where we'll create the following files:
app.py
requirements.txt
templates/index.html
Dockerfile
Make sure to cd flask-app before you start creating the files, because you don't want to start adding a whole bunch of other random files to your image.
- create app.py
app進入點
from flask import Flask, render_template
import random
app = Flask(__name__)
# list of cat images
images = [
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif",
"http://ak-hdl.buzzfed.com/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif"
]
@app.route('/')
def index():
url = random.choice(images)
return render_template('index.html', url=url)
if __name__ == "__main__":
app.run(host="0.0.0.0")
2.create requirement.txt
拿來安裝python套件
拿來安裝python套件
Flask==0.10.1
- create view templates/index.html
<html>
<head>
<style type="text/css">
body {
background: black;
color: white;
}
div.container {
max-width: 500px;
margin: 100px auto;
border: 20px solid white;
padding: 10px;
text-align: center;
}
h4 {
text-transform: uppercase;
}
</style>
</head>
<body>
<div class="container">
<h4>Cat Gif of the day</h4>
<img src="{{url}}" />
<p><small>Courtesy: <a href="http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p>
</div>
</body>
</html>
4.config DockerFile
# 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"]
- build
[root@localhost learnDocker]# docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--build-arg value Set build-time variables (default [])
--cgroup-parent string Optional parent cgroup for the container
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--help Print usage
--isolation string Container isolation technology
--label value Set metadata for an image (default [])
-m, --memory string Memory limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--shm-size string Size of /dev/shm, default value is 64MB
-t, --tag value Name and optionally a tag in the 'name:tag' format (default [])
--ulimit value Ulimit options (default [])
-v, --volume value Set build-time bind mounts (default [])
[root@localhost learnDocker]# docker build -t eric:myfirstapp
"docker build" requires exactly 1 argument(s).
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
[root@localhost learnDocker]# docker build . -t eric:myfirstapp
Sending build context to Docker daemon 7.68 kB
Step 1 : FROM alpine:3.5
Trying to pull repository docker.io/library/alpine ...
3.5: Pulling from docker.io/library/alpine
019300c8a437: Pull complete
Digest: sha256:af2a5bd2f8de8fc1ecabf1c76611cdc6a5f1ada1a2bdd7d3816e121b70300308
---> 074d602a59d7
Step 2 : RUN apk add --update py2-pip
---> Running in e585442a4f59
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
(1/12) Installing libbz2 (1.0.6-r5)
(2/12) Installing expat (2.2.0-r1)
(3/12) Installing libffi (3.2.1-r2)
(4/12) Installing gdbm (1.12-r0)
(5/12) Installing ncurses-terminfo-base (6.0-r7)
(6/12) Installing ncurses-terminfo (6.0-r7)
(7/12) Installing ncurses-libs (6.0-r7)
(8/12) Installing readline (6.3.008-r4)
(9/12) Installing sqlite-libs (3.15.2-r0)
(10/12) Installing python2 (2.7.13-r0)
(11/12) Installing py-setuptools (29.0.1-r0)
(12/12) Installing py2-pip (9.0.0-r1)
Executing busybox-1.25.1-r0.trigger
OK: 61 MiB in 23 packages
---> f364a2331ad3
Removing intermediate container e585442a4f59
Step 3 : COPY requirements.txt /usr/src/app/
---> 1064e28c9427
Removing intermediate container 9bb3f951f819
Step 4 : RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
---> Running in ca9dc85c142b
Collecting Flask==0.10.1 (from -r /usr/src/app/requirements.txt (line 1))
Downloading Flask-0.10.1.tar.gz (544kB)
Collecting Werkzeug>=0.7 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
Collecting Jinja2>=2.4 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
Collecting itsdangerous>=0.21 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
Downloading MarkupSafe-1.0.tar.gz
Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask
Running setup.py install for MarkupSafe: started
Running setup.py install for MarkupSafe: finished with status 'done'
Running setup.py install for itsdangerous: started
Running setup.py install for itsdangerous: finished with status 'done'
Running setup.py install for Flask: started
Running setup.py install for Flask: finished with status 'done'
Successfully installed Flask-0.10.1 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 itsdangerous-0.24
You are using pip version 9.0.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
---> c39058fad9b3
Removing intermediate container ca9dc85c142b
Step 5 : COPY app.py /usr/src/app/
---> a9349662edcc
Removing intermediate container f1222f9e34ef
Step 6 : COPY templates/index.html /usr/src/app/templates/
---> efd6a7c7b687
Removing intermediate container ca63fcdfb84a
Step 7 : EXPOSE 5000
---> Running in 0000200e1536
---> f13d6c45e77c
Removing intermediate container 0000200e1536
Step 8 : CMD python /usr/src/app/app.py
---> Running in 9e35d0e364ff
---> 0933c43e0ec4
Removing intermediate container 9e35d0e364ff
Successfully built 0933c43e0ec4
5.run
[root@localhost learnDocker]# docker run -p 8888:5000 --name myfirstapp eric:myfirstapp
File "/usr/src/app/app.py", line 25
return render_template('index.html', url=url)
^
IndentationError: unexpected indent
噴錯QQ python 的縮排有特別要求
3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
print(‘Hello!’)
print(‘Howdy!’)
print(‘Howdy!’)
或者:
if spam == 42:
print(‘Hello!’)
print(‘Howdy!’)
print(‘Hello!’)
print(‘Howdy!’)
或者:
if spam == 42:
print(‘Hello!’)
print(‘Hello!’)
編輯完之後重新build
[root@localhost learnDocker]# vim app.py
[root@localhost learnDocker]# docker build . -t eric:myfirstapp
Sending build context to Docker daemon 7.68 kB
Step 1 : FROM alpine:3.5
---> 074d602a59d7
Step 2 : RUN apk add --update py2-pip
---> Using cache
---> f364a2331ad3
Step 3 : COPY requirements.txt /usr/src/app/
---> Using cache
---> 1064e28c9427
Step 4 : RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
---> Using cache
---> c39058fad9b3
Step 5 : COPY app.py /usr/src/app/
---> f588b0a29cbd
Removing intermediate container a658fb1a3ed5
Step 6 : COPY templates/index.html /usr/src/app/templates/
---> 9b7b5465cbe4
Removing intermediate container 1bff9b36c433
Step 7 : EXPOSE 5000
---> Running in 88238c9ce73f
---> 266154aeb49d
Removing intermediate container 88238c9ce73f
Step 8 : CMD python /usr/src/app/app.py
---> Running in f29320972d6c
---> 49d4582c3324
Removing intermediate container f29320972d6c
Successfully built 49d4582c3324
再run一遍
[root@localhost learnDocker]# docker run -p 8888:5000 --name myfirstapp eric:myfirstapp
/usr/bin/docker-current: Error response from daemon: Conflict. The name "/myfirstapp" is already in use by container 90fead17e58e8f7a5fa278b8175d980165a76936029b5c3ff66b24ea97e31a09. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.
[root@localhost learnDocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
eric myfirstapp 49d4582c3324 About a minute ago 57.39 MB
<none> <none> 0933c43e0ec4 15 minutes ago 57.39 MB
docker.io/alpine 3.5 074d602a59d7 8 days ago 3.988 MB
docker.io/node 4 78fee52adcbf 13 days ago 651.1 MB
docker.io/node latest 258031686918 13 days ago 666.6 MB
docker.io/ubuntu latest d355ed3537e9 2 weeks ago 119.2 MB
docker.io/ubuntu 14.04 4a2820e686c4 2 weeks ago 188 MB
docker.io/hello-world latest 1815c82652c0 3 weeks ago 1.84 kB
docker.io/dockersamples/static-site latest f589ccde7957 15 months ago 190.5 MB
docker.io/seqvence/static-site latest f589ccde7957 15 months ago 190.5 MB
[root@localhost learnDocker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90fead17e58e 0933c43e0ec4 "python /usr/src/app/" 7 minutes ago Exited (1) 7 minutes ago myfirstapp
8bc3b78d6fa2 node "/bin/sh" About an hour ago Exited (0) About an hour ago sleepy_austin
409b4d7243bd node "/bin/sh" About an hour ago Exited (0) About an hour ago mad_leavitt
69efc485614a node "echo hi" About an hour ago Exited (0) About an hour ago gigantic_aryabhata
fc3ea29bd24a node "ls -l" 2 hours ago Exited (0) About an hour ago pedantic_torvalds
2401da561136 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago stupefied_boyd
d3d989e51eca ubuntu:14.04 "/bin/bash" 21 hours ago Exited (130) 21 hours ago pensive_jones
e0dfa34694f7 node "/bin/bash" 6 days ago Exited (0) 6 days ago sad_visvesvaraya
7990bb7e2f90 ubuntu "/bin/bash" 6 days ago Exited (0) 6 days ago silly_bartik
93203c4149aa hello-world "/hello" 6 days ago Exited (0) 6 days ago pedantic_dijkstra
[root@localhost learnDocker]# docker rm myfirstapp
myfirstapp
[root@localhost learnDocker]# docker run -p 8888:5000 --name myfirstapp eric:myfirstapp
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Head over to http://localhost:8888 and your app should be live. Note If you are using Docker Machine, you may need to open up another terminal and determine the container ip address using docker-machine ip default.
由於 run 的時候把 5000 port mapping 到 本地的 8888 prot 所以訪問8888可以看到結果:
[eric_tu@localhost ~]$ links http://127.0.0.1:8888
留言
張貼留言