前言
以下全部是我根据Docker官方文档:https://docs.docker.com/get-started/ 所做的笔记。
Docker官网是不用搬梯子的,直接就能打开。
如果你问我怎么科学上网?
不好意思,我不知道!!!如果你确实有科学上网需求,请找我面谈。
正文开始
第一部分:Get Started, Part 1: Orientation and setup
1、Ubuntu18虚拟机(用户名:ubuntu 密码:ubuntu)安装完成之后,给root用户设置密码
sudo passwd root
密码是:ubuntu
2、将用户ubuntu加入到sudoer file 中
root用户下输入
vi /etc/sudoers
然后在root下添加用户Docker并且设置执行sudo命令的时候不需要密码
root ALL=(ALL:ALL) ALL
ubuntu ALL=(ALL:ALL) NOPASSWD:ALL
3、安装Docker
在root用户下执行下面的命令
snap install docker
然后将snap/bin加入到环境变量中
vi ~/.bashrc
在最后一行输入
export PATH=$PATH:/snap/bin
wq!保存并退出之后记得刷新配置
source ~/.bashrc
4、验证docker是否安装成功
只查看docker的版本号:
docker --version
查看安装更多信息:
docker version(或者 docker info)
执行这个命令有可能出现下面的错误
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/create:dial unix /var/run/docker.sock: connect: permission denied.See 'docker run --help'.
说的是没有权限连接到Docker
原因就是你当前的用户没有加入到docker用户组里面
解决办法:
执行下面的命令
sudo usermod -a -G docker $USER
然后执行重启命令
init 6
5、测试安装的Docker能否使用
docker run hello-world
6、一些常用的命令
查看安装了哪些镜像
docker image ls
查看存在哪些容器
docker container ls --all
7、小结
使用Docker,可以摆脱虚拟机的臃肿。
------------------------------------------------------------------
第二部分:Get Started, Part 2: Containers
Prerequisites
1. Install Docker version 1.13 or higher.
2. Read the orientation in Part 1.
3. Give your environment a quick test run to make sure you’re all set up:
docker run hello-world
使用Dockerfile自定义容器
1.cd 到一个新的目录下
2.vi Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /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"]
保存退出
3.继续 vi 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)
保存退出 .
4.继续 vi requirements.txt
Flask
Redis
保存退出
5.执行下面的命令,最后是一个点
docker build -t friendlyhello .
6.查看一下镜像
docker image ls
7.运行app
docker run -p 4000:80 friendlyhello
然后在浏览器地址栏输入 http://localhost:4000
也可以直接在命令行查看 curl http://localhost:4000
8、停掉app
在终端 Ctrl + C
9、后台运行app
docker run -d -p 4000:80 friendlyhello
10、查看一下容器
docker container ls
然后停掉对应的容器
docker container stop 指定id号
11、将app发布到网上
11.1、先登录https://hub.docker.com/注册一个Docker账号
docker login
11.2、给app镜像打个标签
docker tag friendlyhello littlecurl/test:part2
11.3、推送app镜像到云端
docker push littlecurl/test:part2
11.4、运行云端镜像
docker run -p 4000:80 littlecurl/test:part2
12、命令小结
docker build -t friendlyhello . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag
---------------------------------------------------------------------------
Docker官网入门文档总共是有6部分,我打算三天更新完,今天就先到这。