Summary and table of contents

Now that we’ve finished installing the Linux server, let’s learn about the Docker environment installation and basic usage

Install the Docker

  • Docker is an open source application container engine, Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then distribute them to any popular Linux machine, as well as virtualization. And the configuration of the various environments is isolated and simple and doesn’t require you to spend a lot of time fixing the environment

Containers are completely sandboxed, have no interfaces with each other (like iPhone apps), and most importantly, have very low performance overhead.

In the production line environment of Newegg, large-scale applications are supported by Docker

This should be very very simple, here is not detailed description, just lists a general process and steps, the most recommended is to use the official script for one-click installation

 curl -sSL https://get.daocloud.io/docker | sh
Copy the code

The main content comes from Teacher Ruan Yifeng’s Docker tutorial. If you need to get started quickly, please refer to the following part. If you don’t need to click Teacher Ruan’s blog to learn

Docker basic knowledge literacy

The theoretical knowledge

  1. The image file

Docker packages the application and its dependencies in an image file (which is a binary). Only from this file can the Docker container be generated. The image file can be thought of as a template for the container. In practice, an image file is usually inherited from another image file

#Lists all image files on this machine.
$ docker image ls

#Delete image file
$ docker image rm [imageName]

#Generate an image based on the local Dockerfile
$ docker image build -t yourImageName .

Copy the code
  1. Container we said that the image is a binary, and container is the box that makes these containers run

Here are a few common commands

#View the running container
$ docker container ls 

#View running containers (all of them)
$ docker container ls --all

#Operation of the container
$ docker container run yourImageName

#Run in the background and specify ports 
$Docker container run --name=gitlab-runner- ci-d -p 3002:3002 gitlab-runner-ci-test

#Go to the bash command line that the container runs
$ docker container run -it yourImageName bash

#Kill the terminating container
$ docker container kill  [containerID]

#Remove the container
$docker container rm [containerID]

Copy the code

The actual operation

We learned about containers in theory, and then we learned about containers in practice

  1. Example 1: Pull a container from the Docker-hub and run it,

Docker-hub is similar to Github, which contains a lot of docker-images. You can also upload your own docker-image for others to download and use


#Or docker image pull Hello-world because the official image is in the library
$ docker image pull library/hello-world

#Check whether the file is pulled successfully
$ docker image ls 

#Run and you see the output, and the container stops automatically (the container can be set to stop automatically or not)
$ docker container run hello-world

Copy the code
  1. Pull a container from docker-hub and run make your own image and publish it to docker-Hub

The first step is to register your account in docker-hub. The second step is to log in locally

$Docker login -u nothid0232
#Submission rejectedRoot @ VM - 0 - # 14 - centos ~] docker push myapp: 1.0 The push refers to The repository docker. IO/library/mycentos 99 b9cbc97f40: Preparing abb030d27363: Preparing 2653d992f4ef: Preparing denied: requested access to the resource is denied [root@VM-0-14-centos ~]
#The image name and version are not specified
#Docker [username]/[image name]: indicates the version number
#First modify the label[root @ VM - 0 to 14 - centos dockerfile] # docker tag 7 a63efdb8e59 chenyu199702 / mycentos: 3.0
#Submit again, because it is slow to upload on the Internet[root @ VM - 0 to 14 - centos dockerfile] # docker push chenyu199702 / mycentos: 3.0 99 b9cbc97f40: Pushing 2.692MB/29.46MB ABB030D27363: Pushing 4.914 kB/68.07MB 2653D992F4ef: Pushing 4.914MB/209.3MB
#Then log into your DockerHub to see it
Copy the code
  1. Make your own image

First we use the front-end stack as an example. We create a directory locally and init a Node project (using KOA to make the meanest HTTP-server)

mkdir mapp & cd ./mapp
npm init
npm install koa
Copy the code

Then create the startup file main.js with the following contents

  const Koa = require('koa');
  const app = new Koa();

  // response
  app.use(ctx= > {
    ctx.body = 'Hello Koa';
  });

  app.listen(3000);
Copy the code

Finally, create docker-related files

.dockerignore Ignores unwanted packages

.git
node_modules
npm-debug.log
Copy the code

Dockerfile file

The image file inherits the official Node image file. The colon represents the tag, which is 8.4, or version 8.4 of node.
FROM node:8.4  

Copy all files in the current directory (except the path excluded by.dockerignore) into the /app directory of the image file
COPY . /app
# specify the next working path as /app.
WORKDIR /app

# In the /app directory, run NPM install to install dependencies. Note that all dependencies will be packaged into the image file after installation.
RUN npm install --registry=https://registry.npm.taobao.org

Expose container port 3000 to allow external connections to this port.
EXPOSE 3000

Copy the code

Next we build the startup container, go into its bash and try to run it

$Docker image build-t koa-demo:0.0.1

#The following command will take us into the container
$Docker container run -p 8000:3000 it koa-demo:0.0.1 /bin/bash

#Go to the container and find main.js and run it
$ root@66d80f4aaf1e:/app# node ./main.js
Copy the code
  1. Use Docker-compose to simplify operations

In the previous article, we needed to use execution on the command line

Docker ru xxxx-p-x...Copy the code

This is obviously inconvenient if the command is complex, so we came up with a solution to simplify the complexity of the run using the Docker-compose pull as shown in the 👇 example below

# Here is a main use of the brain docker to run a nginx and configure the file and host mapping and specify some other environment variable configuration,
version: '2'
services:
  nginx:
    image: nginx:stable-alpine
    restart: unless-stopped
    network_mode: host
    volumes:
      - /srv/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /srv/nginx/conf.d:/etc/nginx/conf.d
      - /srv/nginx/html:/usr/share/nginx/html
      - /var/log/nginx:/var/log/nginx
    ports:
      - "80:80"
    environment:
      - NGINX_HOST=your.domain
      - NGINX_PORT=80
Copy the code

With this file, after you build the image, all you need is a word from the command line

sudo docker-compose up
Copy the code

So that’s the easiest way to get started with Docker