Docker docker Linux installation and basic operation

A. Installation docker


Docker required to run on Centos 7, the system of claim 64, kernel Version 3.10

1.uname -an view the current system version

2.yum -y install docker download and install docker

3.service docker start start docker Service

4.docker version docker check whether the installation was successful

When you see the information in the figure below, is expressed native docker has been successfully installed, very simple 

                               

II. Mirroring operation


To create a container operation in mirror-based, so talk about the next docker mirror

Search Mirror

docker images ll see whether the unit and have mirrored

Currently the machine has not been mirrored, to Docker Hub Download (Mirror can also be custom, will not elaborate here)

docker search java, but also specify a specific version download,

For example: docker search Ubuntu: 1.2.5.4, can be found docker Hub lists a lot of mirrors 

Download image 

docker pull docker.io/nginx download

Download to search out the local docker Hub larger than the mirror, because the download process automatically extract, looking at a list of mirrors there just downloaded image

The list contains the warehouse name, version label, image ID, creation time and space occupied

Remove Mirror

Delete unwanted Mirror Mirror id docker rmi

III. Creating and managing mirrored


We have a good front Nginx download the image, then we create a container Nginx only applied docker run -i -t <IMAGE_ID> / bin / bash: -i: input to the standard container -t: assign a virtual terminal / bin / bash: bash script execution,

docker run -idt --name container_nginx -p 8080:80  docker.io/nginx


Start using a mirror docker.io/nginx, name container_nginx container, -p 8080: 80 shows a map of container port 80 to port 8080 of the host, so we only need to access the computers can access the service on port 8080 to the container.

Note: The name is in front two - in front of the port there -p, docker.io/nginx is image name, 8080 is the port of the host, port 80 is applied Nginx

A port on the host can be mapped a container port, the port may not correspond to a plurality of containers host port (CentOS based system if the container is installed, the container port just set, but if the vessel only a simple application, the container If the application itself port port)

So we have created and started a container!

exit 退出容器
 
docker ps 查看运行中的容器
 
docker ps -a  查看运行中和非运行中的所有容器
 
docker exec -it container_nginx /bin/bash  进入容器

如果容器还未启动 执行docker start container_nginx 


After entering the container starts Nginx

whereis nginx 找Nginx的启动目录
 
[root@iz2zehzeir87zi8q99krk1z ~]# docker start container_nginx
container_nginx
[root@iz2zehzeir87zi8q99krk1z ~]# docker exec -it container_nginx /bin/bash
root@84683e425116:/# whereis  nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@84683e425116:/#  /usr/sbin/nginx 
 


   At this point the browser to access  http://51.110.218.9:8080/  Nginx can be accessed directly within the container 

If access is unsuccessful, it may be a host firewall port open, execute the following command to shut down

/ sbin / iptables -I INPUT -p tcp --dport 8080 -j ACCEPT


Because Ali cloud server I use, so it is necessary to open port 8080 in Ali cloud

                               

Delete container 

容器删除之前先将容器停止  
 
docker stop container_nginx 或者是容器的id
 
docker rm -f container_nginx  容器删除


Difference docker start of the docker run

docker start name 启动一个已经创建的容器
 
docker run 创建并启动一个容器
docker run 命令其实是 docker create 和 docker start 的命令组合,先执行docker create 创建一个容器 再接着docker start启动

Host container file and copy each other  

从主机复制到容器 sudo docker cp host_path containerID:container_path
 
从容器复制到主机 sudo docker cp containerID:container_path host_path


Please note that the above two commands are executed on the host, the container can not be executed

docker cp container_nginx:/usr/local/xin.txt  /usr/local/software/   容器向主机复制文件
docker cp /usr/local/xinzhifu.txt  container_nginx:/usr/local/  主机向容器复制文件

Such a basic docker container to create finished. . . . . . . . . . . .


In turn then take a look at container and mirrored docker difference   https://www.cnblogs.com/linjiaxin/p/7381421.html

So in fact the essential difference between the mirror and the container is not large, the image can be generated container, the container if it can be made into a mirror?

docket commit container_nginx  image_nginx:v1
             
              容器名            自己起一个镜像的名字:版本号


 Generated with the current mirror container redis

For example: A, B two machines want to install redis, create a container on the machine and check all the configuration A redis in the container, so that after the container docker commit mirrored image_redis, B machine also want to install redis, the direct use mirror image_redis create a container on the line, docker is doing things once and for all.

And the traditional way was installed on each machine configuration redis very troublesome

 

IV. Importing and Exporting mirror


 
Image compression packing (operating on the host), there are two ways docker save the docker load and with docker import docker export

docker save nginx | gzip > nginx_xin_image.tar.gz  将现有的镜像压缩打包
 
docker load -i nginx_xin_image.tar.gz  压缩的镜像解压
 
docker images 进行查看


docker save the image directly packaged docker save <image name> or <image id> 

docker export container_nginx> nginx_image.tar  
 
cat nginx_image.tar | sudo docker import  - nginx_image:import


docker export container directly packaged docker save <container name> or <container id> 

Note that both methods of supporting, must not be mixed. Although no problem importing and exporting, but in the creation of the container will complain

If you use import import file save generated, although not import an error, but will be prompted to fail at startup container,

": Error response from daemon: Container command not found or does not exist docker" similar error will occur.

Similarly, the use of load load file export generated, there will be a problem
 

Published 352 original articles · won praise 115 · views 130 000 +

Guess you like

Origin blog.csdn.net/Aidam_Bo/article/details/103223521