Docker containers Technology Notes

learning target

  • Docker master the basic knowledge to understand the concept of the mirror and Docker container
  • To complete the installation and start Docker
  • Docker master mirror and container related commands
  • Installation master Tomcat Nginx and other commonly used software applications
  • Master docker migration and backup-related command
  • Dockerfile can use to write scripts to create container
  • Able to build and use docker private warehouse

Docker installation

The official recommended to use Ubuntu operating system, because my server is Centos7.X, I will show you
(1) yum update to the latest package

sudo yum update

(2) requires the installation package

sudo yum install -y yum-utils device-mapper-persitent-data lvm2

(3) Set yum source cloud Ali

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

(4) mounted docker

sudo yum install docker-ce

(5) Installation View version

docker -v

Set ustc Mirror

(1) Edit the following file

vi /etc/docker/deamon.json

(2) add the following in which

{
	"registry-mirrors" : ["https://docker.mirrors.ustc.edu.cn"]
}

use

# 1.开启服务
systemctl start docker
# 2.重启服务
systemctl restart docker
# 3.查看启动状态
systemctl status docker
# 4.设置开机自启
systemctl enable docker
# 5.停止服务
systemctl stop docker

Mirroring commonly used commands

(1) View Mirror

 docker images

REPOSITORY: image name
TAG: Mirror Tags
IMAGEID: Mirror ID
CREATED: image creation date
SIZE: Size mirror
those images are stored at the host Docker / var / lib / docker directory

(2) Search mirror
if you need to get the desired image from the network, you can search by the following command

docker search 镜像名

NAME: warehouse Name
DESCRIPTION: Mirror Description
STARS: User rating
OFFICIAL: Are official
AUTOMATED: automatically build, indicating that the image created by the Docker Hub automatic process framework

(3) pulling the mirror
pulling image is downloaded from a central repository to a local mirror

docker pull 镜像名称

For example, I want to download Centos7 Mirror

docker pull centos:7

(4) Remove Mirror

docker rmi 镜像ID

Delete all mirrors

docker rmi `docker images -q` #注意这里不是单引号,是esc下面的按键

Container commonly used commands

(1) View container
to view the container is running

docker ps

View all containers

docker ps -a

View the last run of the container

docker ps -l

Check stop container

docker ps -f status=exited

(2) create and launch container
to create a container command:

docker run
-i:表示运行容器
-t:表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个终端。
--name:为创建的容器命名
-v:表示目录映射关系(前者是宿主机命令,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。
-d:在run后面加上此参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加 -i -t参数,创建后会自动进入容器
-p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射

Interactive way to create container

docker run -it --name=容器名称 镜像名称:标签 /bin/bash

Then we see the ps command, you can see the launch of the vessel was found, state to start state

Exit the current container

exit

Create a container guardian way:

 docker run -di --name=容器名称 镜像名称:标签

Login guard vessel way

docker exec -it 容器名称(或者容器id) /bin/bash

(3) start and stop container

Stop the container

docker stop 容器名称 (或者容器ID)

Start container

docker start 容器名称(或者容器ID)

(4) a copy of the file
if we need to copy the file into the container can use the cp command

docker cp 需要拷贝的文件或目录 容器名称:容器目录

Also can copy files from the container

docker cp 容器名称:容器目录 需要拷贝的文件或目录

(5) directory is mounted
us, will host the directory mapping when creating a directory within the container and the container, so that we can modify the host file to a directory in order to influence the container.
After you create a container add -v parameter becomes the host directory: container directory, for example

docker run -di -v /usr/local/myhtml:usr/local/myhtml --name=mycentos3 centos:7

If you share a multi-level directory, prompts may appear insufficient privileges
because of Centos 7 security module, selinux permissions to ban, and we need to add parameters --privileged = true to resolve this problem
(6) View container ip address
we can view various data container running the following command

docker inspect 容器名称(容器ID)

Ip address may be output directly through the following command

docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称(容器ID)

(7) Delete container
to delete the specified container:

docker rm 容器名称(容器id)

Application deployment

Mysql deployment

(1) Pull mirror mysql

docker pull centos/mysql-57-centos7

(2) create the container

docker run -di --name=tensquare_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
-p代表端口映射,格式为 宿主机映射端口:容器运行端口
-e代表添加环境变量 MYSQL_ROOT_PASSWORD表示root用户的登录密码

(3) into the container mysql

docker exec -it tensquare_mysql /bin/bash

(4) Log in mysql

mysql -u root -p

(5) a remote link mysql
connected host ip, designated port 3306

tomcat deployment

(1) Mirror Pull

docker pull tomcat:7-jre7

(2) create the container

docker run -di --name=mytomcat -p 9000:8080 -v /usr/local/webapps:/usr/local/webapps tomcat:7-jre7
-p表示地址映射

Nginx deployment

(1) Mirror Pull

docker pull nginx

(2) create Nginx container

docker run -di --name=mynginx -p 80:80 nginx

Redis deployment

(1) Mirror Pull

docker pull redis

Create a container

docker run -di --name=myredis -p 6379:6379 redis

Migration and Backup

Storage containers for the mirror

We can save the following command vessel for the mirror

docker commit mynginx mynginx_i

Image backup

We can save the image with the following command vessel for the tar file

docker save -o mynginx.tar mynginx_i

Mirroring recovery and migration

First we removed mynginx_img mirror and then execute the following command to recover

docker load -i mynginx.tar
-i代表输入的文件

Dockerfile

What is Dockerfile

Dockerfile is the script by a number of commands and parameters constituted these commands to the base image and ultimately create a new image.

  1. For developers: provide a consistent development environment for the development team;
  2. For testers: You can take a direct mirror image of the constructed building or developing a new image file to work through Dockerfile
  3. For operation and maintenance personnel: At deployment time, enables seamless migration of applications.
command effect
FORM image_name:tar Which defines the use of the base image to start the process of building
MAINTAINER user_name Disclaimer mirror creator
ENV key value Set Environment Variables (you can write multiple)
RUN command Dockerfile is the core of the (multiple writable)
ADD source_dir / file dest_dir / file Copy files to the host in the content, if it is a compressed file will automatically decompress after copying
COPY source_dir / file dest_dir / file ADD and similar, but if you can not extract the compressed file
WORKDIR path_dir Set the working directory

Docker private warehouse

Private warehouse set up and configuration

(1) Pull private repository mirroring

docker pull registry

(2) Start private container warehouse

docker run -di --name=registry -p 5000:5000 registry

Local point your browser to http: Your ip: 5000 / v2 / catalog to see {"repositories":[]}representation of private warehouse and build a successful content is empty
(4) modify daemon.json

vi /etc/docker/daemon.json

Add the following

{"insecure-registries":"[你的ip:5000]"}

This step allows docker trust for private warehouse address
(5) Restart docker Service

systemctl restart docker

Mirroring uploaded to a private warehouse

(1) This image is marked private warehouse

docker tag jdk1.8 你的ip:5000/jdk1.8

(2) marked image upload

docker push 你的ip:5000/jdk1.8
Published 17 original articles · won praise 15 · views 4173

Guess you like

Origin blog.csdn.net/qq_45455361/article/details/104278125