Docker command operation

Use docker command requires administrator privileges, if you want each time you enter docker related commands without sudo, can be set by the following command

sudo usermod -a -G docker $USER
sudo service docker restart
newgrp - docker

The official ubuntu system image, the tool command is rarely, wishing to install internal ping, ifconfig card or vim and other tools that can be set by the following command [need an Internet connection and time-consuming]

apt-get update
apt install net-tools        # ifconfig 
apt install iputils-ping     # ping

Mirroring

# 搜索镜像  
docker search 镜像名
# 拉取镜像
docker pull 镜像名
# 查看所有镜像
docker image ls || docker images || docker images ls --all
# 运行镜像创建为容器<"这里的内容为可选参数">
docker run -itd <-p,-v...> <--name=别名>镜像名字<:版本>
# 删除镜像(需要镜像没有成为容器在运行)
docker image rm 镜像名  || docker rmi 镜像名<id> || docker rmi -f 镜像名<id>  # -f 强制删除加显示信息

# 批量操作删除镜像
docker rmi `docker image ps -aq `
# 导入镜像
docker load -i 路径文件名.tar <镜像名> ||  docker load < 路径文件名.tar.gz
# 保存镜像<打包>
docker save -o 文件名.tar <镜像名>    || docker save 镜像名 > 路径文件名.tar.gz
# 提交自己定义的镜像
docker commit 镜像id 镜像名

parameter

exec        # 进入容器的参数
-i          # 交互式的操作容器
-t          # 开启一个terminel终端,用于和linux交互
-d         # 后台运行容器
/bin/sh    # 指定使用centos的bash解释器
bash      # 进入容器命令窗口,配合-it使用
-c         # 运行一段shell命令
    
docker search  hello-docker     # 搜索docker镜像 ,就是去 docker hub搜索 hello-docker而已
docker pull hello-world         # 下载公网的,docker镜像
docker image ls                # 查看本地的镜像有哪些
docker images                  # 这两条命令一样的,docker进行了很多的命令升级,不用慌
docker run hello-world         # 运行一个docker镜像,产生一个容器实例
docker container  ls           # 列出所有正在运行的容器
docker ps  -a              # 列出所有曾经运行过的容器记录,以及正在运行的容器记录
docker pull  centos             # 下载cnetos镜像

Container operations

There must be a mirror

# 运行镜像后台创建为容器<"这里的内容为可选参数">-p主机端口:指定端口,-v主机路径:指定路径
docker run -itd <-p,-v...> <--name=别名>镜像名字<:版本>
# 单纯的创建容器           || 创建容器并输出hehe
docker run hello-world   || docker run --name namecentos -it centos /bin/echo "hehe"
# 查看容器
docker container ls || docker ps -a 
# 启动容器
docker container start 容器名或id
# 停止容器
docker container stop 容器名或id
# 强制停止容器
docker container kill 容器名或id
# 批量操作容器
docker stop/start/rm `docker ps -aq`
# 进入容器
docker container exec -it 容器名或id  bash  || docker exec -it 容器id bash
# 删除容器
docker container rm 容器名或id
# 把容器保存成镜像
docker commit <容器名或id> <新镜像名>
# 查看容器日志
docker logs 容器id
# 查看容器端口映射本地
docker port 容器id
# 查看容器内的进程
docker top 容器id

Removal process (stop container -> Delete container -> Remove Mirror)

docker container stop <容器名称/容器ID>
docker container rm  <容器名称/容器ID>
docker image rm <容器名称/容器ID>

Copying a re-name image modification

docker tag 镜像名:版本 10.0.3.33:5000/新镜像名:版本
docker tag hello-world:latest 10.0.3.33:5000/hello-world:latest

Warehouse operations

docker提供了一个类似于github的仓库dockerhub, 
网址https://hub.docker.com/

Login docker warehouse in linux

docker login
# 提示输入账户和密码

Note To ensure that the image tag is the name of the account, if the mirror name does not need to change ittag

Mirror name change

# 语法是:  docker tag 镜像名 账户名/新镜像名
docker tag nginx wsh885/newnginx1.0

Push docker image to dockerhub

Push the mirror to the warehouse

docker push wsh885/newnginx1.0:latest(版本)

Download warehouse mirror

docker pull wsh885/newginx1.0

Build private warehouse for internal use

Private warehouse docker official of the registry usage

https://yeasy.gitbooks.io/docker_practice/repository/registry.html

1. Download the registry mirror and start a private warehouse container

docker pull registry

Private repository will be created in the container /var/lib/registry, the parameters are stored so by -v image file to a local /opt/data/registrylower
port 5000 port 5000 to port mapping container host machine (not -itadded can not enter inside the container)

docker run -d -p 5000:5000 -v /opt/docker/registry:/var/lib/registry registry

2. Check the registry to start container

docker ps -a 

http://10.0.3.33:5000

3. Modify the image tag, with docker_registrythe beginning of the address of the port

Note: You need this format image name, or can not upload

docker tag 镜像名:版本 10.0.3.33:5000/新镜像名:版本
docker tag redis:latest 10.0.3.33:5000/newredis:latest
docker images

4.Docker default does not allow non-HTTPS mode push mirror. We can cancel this restriction by Docker configuration options, where you must write the correct json data

vim /etc/docker/daemon.json
// 添加一行配置("insecure-registries":["10.0.3.33:5000"]):
{   // 这条是容器加速器
  "registry-mirrors": ["http://95822026.m.daocloud.io"],
  "insecure-registries":["10.0.3.33:5000"]
}

5. write daemon.json docker service profile to write to [-Service] configuration block, this configuration file is loaded

vim /lib/systemd/system/docker.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
# 加这一条
EnvironmentFile=-/etc/docker/daemon.json
TimeoutSec=0
RestartSec=2
Restart=always

6. After modifying the configuration file docker, reload docker

systemctl daemon-reload

7. Restart docker Service

systemctl restart docker  # 注意,重启docker服务,所有的容器都会挂掉

8. The container restart dockerr

# 启动registry服务
docker ps -a 
docker start 容器id
docker start `docker ps -aq` # 批量启动

9. The image uploaded to a private warehouse

docker ps -a
docker push 10.0.3.33:5000/newredis
# 再创建一个
docker tag mysql:latest 10.0.3.33:5000/newmysql:latest
# 再上传
docker push 10.0.3.33:5000/newmysql

10. Use the following link to access the repository, you can view our image upload

http://10.0.3.33:5000/v2/_catalog
# 可以到目录里面去看
cd /opt/docker/registry/docker/registry/v2/repositories/

11. When we uploaded to a private warehouse is completed, the equivalent of this image has been backed up, then we can delete the local mirror of this, if you want to use, then it downloaded directly from the warehouse down private use

# 删除
docker rmi 容器名字
docker rmi 10.0.3.33:5000/newredis
# 下载
docker pull 10.0.3.33:5000/newredis

Custom docker container (dockerfile)

1. Create a download file

In /optcase the establishment of a docker directory, create a file and download a source package django-2.1.7 of

touch DockerfileAnd run.shwhich run.shis used to perform Djanog bash script, Dockerfilethat is, automatic build pythoncore file, here is the establishment of containerDockerfile

Download Ali cloud extended source file epel.repo

# 在/opt/docker/目录中下载
wget https://media.djangoproject.com/releases/2.1/Django-2.1.7.tar.gz
wget -O epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2. Write Dockerfile file

FROM centos
MAINTAINER TigerLee

ADD epel.repo /etc/yum.repos.d  # 将当前目录的epel.repo文件拷贝到容器的/etc/yum.repos.d目录下
RUN yum update -y               # 运行命令

RUN yum install -y python36
RUN yum install -y python36-pip
RUN pip3 install setuptools
ADD Django-2.1.7.tar.gz /opt/

WORKDIR /opt/                   # 进入/opt目录
RUN mv Django-2.1.7 django      # 运行命令mv

WORKDIR /opt/django             # 进入/opt/django目录
RUN python3 setup.py install

WORKDIR /opt
RUN django-admin.py startproject qishidj

ADD run.sh /opt/qishidj/run.sh
RUN sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['\*'\]/g" /opt/qishidj/qishidj/settings.py   # 替换django的配置文件,允许任何主机都可以访问
WORKDIR /opt/qishidj
RUN chmod 777 run.sh
EXPOSE 8000                        # 声明容器端口
CMD ["/bin/sh","run.sh"]           # 容器启动时默认执行的命令

3. Write run.sh

python3 manage.py runserver 0.0.0.0:8000

4. Construction of the mirror docker

sudo docker build -t django_file-217 .    # 注意,有一个点,表示在当前页
# 构建完成后,查看镜像,有当前创建的django_file-217镜像文件
docker images

5. Start to create a boot container

docker run -itd -p 9090:8000 django_file-217

6. Review

docker ps -a                        # 查看容器状态,运作中则可以进入容器
docker exec -it 容器id bash         # 使用交互式模式进入容器,再使用ps -ef查看运行的进程
ps -ef                             # 有运行状态则表示成功
# 外部通过访问ip:9090 即可访问django启动画面了
# 查看docker的log日志
docker logs -f container_id(容器id) # -f为持续显示

7. Export Mirror

# 如果想容器保存为镜像用此命令 ---> docker commit <容器名或id> <新镜像名>
# 本身已经有镜像了,直接导出 # -o 参数  指定地址和文件名   镜像名
docker save -o mydjango.tar.gz django_file-217  # 这样就有一个解压包了

8. pushed warehouse

# 推送到线上自己的仓库
docker login        # 登录
docker tag django_file-217 wsh885/mydjango_217
docker push wsh885/mydjango_217

docker Mirror accelerator

加速器镜像站:https://www.daocloud.io/mirror#accelerator-doc
资料:https://www.cnblogs.com/pyyu/p/6925606.html


# 一条命令加速
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

# 这条命令其实就是改了docker的一个配置文件里面的注册镜像地址, 可以查看一下 
cat /etc/docker/daemon.json   
### 添加 ###

Multi-port multi-path mapping

Viewdocker port 容器id

docker run -itd --name=88luffy -p 80:80 -v /home/ubuntu/luffyo/luffyclient/dist/:/usr/share/nginx/html -p 8080:80 -v /home/ubuntu/luffyo/luffyserver/luffyserver/static/:/usr/share/nginx/static nginx

Guess you like

Origin www.cnblogs.com/wshlym/p/11329913.html