Introduction to Docker
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, which can then be distributed to any popular Linux machine, and can also be virtualized . Containers are completely sandboxed and do not have any interface with each other.
Docker is written in Go language. Docker is also a command line tool that provides all the tools needed in the execution of a central "docker".
Docker itself needs to run on a Linux system , so if our system is Windows or MacOS, then we need to download a DockerToolbox , this installer provides two tools for using docker: Kitematic (Alpha) and Docker Quickstart Terminal (the former It is a graphical docker (the latter is the docker command line) and a virtualized Oracle VM VirtualBox, which can create a Linux virtual machine for us named default when we open the terminal for the first time. We also call this a " docker machine ".
image is the docker image. Important concepts in docker. In terms of docker image service, there is an official website dockerhub, on which we can find some executable image files that others have done: centos, Ubuntu, etc. In China, such as Alibaba Cloud, similar services are also provided, and the access speed is relatively fast. Based on the image we can create a (sandbox environment) container to run our image.
Docker common commands
Create and run a container for the docker image: docker run image name (one image we can create and run multiple containers)
Search for related images on dockerhub in the terminal, such as: docker search centos
(dockerhub provides some official version mirrors, so in the search list, we can see that OFFICIAL is [OK], which means this mirror is the official version)
Download the cloud image on dockerhub: docker pull image name
(Downloading images on dockerhub will be very slow, we can use domestic image management services such as Alibaba Cloud )
View local images: docker images
Query running containers: docker ps
Query all containers: docker ps --all (shorthand: docker ps -a)
List of directories in the output image: docker run image name ls
Specify a name when creating a container: docker run --name name image name
View the most recently created container: docker ps --all --latest
View the log of the container: docker logs container name
Stop a running container: docker stop container name
Delete container: docker rm container ID
Restart the container: docker restart container name
Run the container: docker start container name
Create an interactive container, such as logging into the container: docker run --ineteractive --tty centos /bin/bash
(--interactive means to create an interactive container, shorthand: -i;
--tty means to create a terminal for the container, shorthand: -t;
The terminal location is: /bin/bash)
Create a container running in the background, such as implementing the centos system image container to perform ping operations for a long time:
docker run --detach centos ping www.baidu.com (--detach means that this container is a background running container, abbreviation: -d)
(docker will return a complete container id, we can view the running container, and view the container log to monitor the container running status)
docker practical skills
Accelerator Service Configuration
Use Alibaba Cloud Accelerator to improve the speed of obtaining official Docker images in China
Step 1: Open Alibaba Cloud Accelerator
Log in to your Alibaba Cloud account, find the console --> Cloud Computing Basic Services --> Container Image Service --> Image Accelerator, and open the tab of the corresponding operating system:
Step 2: Create a new docker machine
Before creating an upgraded version of docker machine, we need to delete the docker machine we created by default before:
Then, go to create a new docker machine:
Create a Linux virtual machine with a Docker environment installed, specify the machine name as default, and configure the Docker accelerator address.
docker-machine create --engine-registry-mirror=https://31u25vtb.mirror.aliyuncs.com -d virtualbox default
Copy the above line of command directly, paste it into the terminal, and press Enter. (Note: Each Alibaba Cloud user will have a dedicated acceleration address, and they need to change the acceleration address by themselves, namely https://31u25vtb.mirror.aliyuncs.com)
Step 3: Configure docker machine
View the environment configuration of the machine, configure it locally, and access the Docker service through the docker client.
docker-machine env default
eval "$(docker-machine env default)"
docker info
Copy it one by one and execute it in the terminal.
To sum up the above three steps, the Alibaba Cloud acceleration service has been set up. This time, downloading the official image through the pull command will be much faster.
Dockerfile to create a custom image image
We can use the Dockerfile file to make the program jar package into a docker image file, upload it to Alibaba Cloud or run it locally.
Step 1: Create a folder for generating images and put the Dockerfile in it
The folder reference directory structure is shown in the following figure:
Among them, the start.sh script file is as follows:
#!/bin/bash nohup java -jar -Dserver.port=8888 app.jar >./log.out 2>&1 & tail -f /dev/null
The Dockerfile is as follows:
FROM centos MAINTAINER [email protected] ENV APPHOME /apphome RUN mkdir $APPHOME WORKDIR $APPHOME COPY jdk8 $APPHOME/jdk8 ADD app.jar $APPHOME/app.jar ADD start.sh $APPHOME/ ENV JAVA_HOME=$APPHOME/jdk8/jre ENV CLASSPATH=.:$JAVA_HOME/lib \ PATH=$JAVA_HOME/bin:$PATH EXPOSE 8888 RUN chmod 777 -R $APPHOME CMD /bin/bash -c $APPHOME/start.sh
Step 2: Use the Docker command line tool to generate a docker image
docker build -t secosecurity:0.1.2 .Note: There is a dot at the end, secosecurity is the name of the generated image
The newly generated images named "secosecurity" can be viewed through the docker images command.
Docker command line push image to Alibaba Cloud
First, log in to Alibaba Cloud on the webpage
Find the push information in the registry that has been created:
Execute the three statements in the red box in sequence on the docker command line.