创建Docker账号及安装
- 创建Docker Hub账号
https://hub.docker.com/
- 安装Docker Desktop
https://hub.docker.com/?overlay=onboarding
安装完成后,登陆到Docker Desktop
- 验证安装 Open Terminal (Mac / Linux) / PowerShell
docker --version
它会打印版本信息如下
Docker version 19.03.2, build abc123
你的第一个Docker项目
cd path/to/your/dev/folder
创建Dockerfile
touch Dockerfile
Dockerfile is the convention and best practice name. No extension.
Dock file
A docker file is a standardized way for Docker to build your Docker Image. A Docker Image is essentially an operating system and typically a linux one. The Docker Images can be moved around easily and repurposed for a variety of use cases.
接下来我们编辑Docker文件
# In path/to/your/dev/folder/Dockerfile
# Base Image
FROM python:3.6
RUN apt-get update && apt-get install -y --no-install-recommends \
python3-setuptools \
python3-pip \
python3-dev \
python3-venv \
git \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
EXPOSE 8000
CMD python -c "print('hello world')"
注意:大写很重要
From
FROM
is going to build your image from another pre-existing image. This can get pretty advanced so for now, we just use a good python 3.6 image.
RUN
RUN
is a command that allows you to do any bash shell command you'd do normally. In our case, we just do some basic system updates and basic installs.
EXPOSE
EXPOSE
allows your docker image to have a port or posts exposed to outside the image. This is important for web applications and software you want to receive requests.
CMD
CMD
this is the final command your docker image will run. It's typically reserved for something like running a web application.
COPY
COPY
copy is another command we haven't yet added to our Dockerfile. This will allow you to copy local files to your Docker image.
Docker命令
$ docker build -t <your-tag> -f Dockerfile .
运行下面命令
$ docker build -t hello-world -f Dockerfile .
它将会编译image, 第一次可能需要5~15分钟,后面可能会快一点
-t
portion means "tag" and you can add your own tag name I used hello-world since this might be your first time using Docker. When in doubt, include a tag.-f
is the path to the Dockerfile you're going to use to build your image.
运行结果最好打印类似下面的结果:
......
Successfully built c21994628041
Successfully tagged hello-world:latest
$ docker images
Running this command anywhere on your system, assuming Docker Desktop is running, will show you all of the images your system has. It will also have the python 3.6 image we grabbed in our Dockerfile. Pretty cool right?
Pro tip, to remove all of these images just run
docker rm $(docker ps -a -q)
thendocker rmi $(docker images -q)
$ docker ps -a
This will show you all of the containers that are currently running. After we build our image, we need to run it as a container.
$ docker run -it <your-tag>
This creates a "container" from your tagged image. In other words, a container is an instance of an image. Following the example above, you'll run:
$ cd path/to/your/dev/folder
$ ls
Dockerfile
Now run the build:
$ docker run -it hello-world
hello world
If you copied our Dockerfile
exactly, you should see the response from docker run -it hello-world
as simply hello world
. This means your image is working!
Tip: You can actually run
docker run -it <your-tag>
anywhere on your system and it should run your project.
$ docker run -d -it <your-tag>
If you add -d
it will run in the background. This has it's own section because running your container in the background can cause conflicts with other containers using the same port.
To stop a background running container, you can first list all running containers:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5966a461fa9d hello-world "/bin/sh -c 'python …" 3 seconds ago Exited (0) 1 second ago thirsty_cori
$ docker stop <your-container-id>
Above we ran $ docker ps -a
. This showed us our docker container had an id of 5966a461fa9d
. We can now stop and remove that container
docker stop 5966a461fa9d
docker rm 5966a461fa9d
docker image ls
docker image search
docker container ls
docker run -it -p ...
docker exec -i
Docker run 命令大全
docker run :创建一个新的容器并运行一个命令
语法
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS说明:
-a stdin
: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;-d
: 后台运行容器,并返回容器ID;-i
: 以交互模式运行容器,通常与 -t 同时使用;-P
: 随机端口映射,容器内部端口随机映射到主机的端口-p
: 指定端口映射,格式为:主机(宿主)端口:容器端口-t
: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;--name="nginx-lb"
: 为容器指定一个名称;--dns 8.8.8.8
: 指定容器使用的DNS服务器,默认和宿主一致;--dns-search example.com
: 指定容器DNS搜索域名,默认和宿主一致;-h "mars"
: 指定容器的hostname;-e username="ritchie"
: 设置环境变量;--env-file=[]
: 从指定文件读入环境变量;--cpuset="0-2" or --cpuset="0,1,2"
: 绑定容器到指定CPU运行;-m
:设置容器使用内存最大值;--net="bridge"
: 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;--link=[]
: 添加链接到另一个容器;--expose=[]
: 开放一个端口或一组端口;--volume , -v
: 绑定一个卷
Bash Shell in a Docker Image
docker run -it <your-tag> /bin/bash
That's it. You now have access to your docker container's bash shell.
docker发布
查看当前的images
docker image ls
本机结果如下
~/hello-world$ sudo docker image ls
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
hello-world | latest | c21994628041 | 9 minutes ago | 1.01GB |
idiot-inspect-on-docker | latest | beb52e9b60ea | 3 days ago | 1.09GB |
python | 3.6 | 13efce2de907 | 3 weeks ago | 914MB |
hello-world | <none> | bf756fb1ae65 | 6 months ago | 13.3kB |
给image打上标签
docker tag c21994628041 hordechief/hello-world:latest
其中:
c21994628041
是你的image的imageid- hordechief是你注册的docker hub上的用户名
登录你的docker hub
docker login
上传到docker hub
docker push hordechief/hello-world
再次查看本地image
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
hordechief/hello-world | latest | c21994628041 | 18 minutes ago | 1.01GB |
hello-world | latest | c21994628041 | 18 minutes ago | 1.01GB |
idiot-inspect-on-docker | latest | beb52e9b60ea | 3 days ago | 1.09GB |
python | 3.6 | 13efce2de907 | 3 weeks ago | 914MB |
hello-world | <none> | bf756fb1ae65 | 6 months ago | 13.3kB |
其他命令
docker stop
停止container
docker start
启动停止的container
docker exec
执行运行中的容器