Docker安装完成后,运行后,会启动一个linux系统,主机地址为启动时的IP地址,如下
我们也可以直接通过VirtualBox进入系统,默认登录用户名/密码:docker/tcuser
有了这个我们就可以通过其它SSH工具连接上了。
下面以官方的一个demo进行验证。
1、在/home/docker下新建目录python
mkdir python
2、进入python,在目录下新建三个文件Dockerfile,app.py,requirements.txt
cd python
2.1 Dockerfile文件
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /home/docker/app
# Copy the current directory contents into the container at /app
COPY . /home/docker/app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
2.2 requirements.txt
Flask
Redis
2.3 app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
3、在/home/docker/python下进行docker构建
docker build --tag=firstapp:v1.0 .
4、查看镜像
docker image ls
5、运行镜像
docker run -p 4000:80 firstapp:v1.0
6、查看容器(运行中)
docker container ls
7、访问http://192.168.99.100:4000/,具体IP地址似绑定的为准
其它命令:
查看所有容器
docker ps -a
停止容器命令(启动使用start)
docker container stop 容器ID
删除容器命令(必须停止该容器才能删除)
docker rm 容器ID
删除镜像命令(必须删除已经生成的容器)
docker rmi 镜像ID
本人开发的一个网站:编程之道,欢迎来踩!!!