This is the first day of my participation in the More text Challenge. For details, see more text Challenge

  • 1.Docker Quick Start
  • 2.Docker Quickstart 2
  • 3.Docker Quickstart 3
  • 4.Docker Quick Start

Docker

Docker introduction & Why to use Docker (slightly, interested small Turkey can be added)

The term

  • image, which is similar to a class in object orientation
  • containerContainer, similar to an instance of a class instantiated in object-oriented, mirror pairs of containers can be one-to-many, such as usingmysql8.0.25Two or more images are deployedmysql8.0.25Container, but the data is isolated, and it’s very common for development processes
  • Other: it doesn’t matter

start

  1. Installation: the author Mac direct installationDocker.WinThe operation is similar. Download and install it from the official website. Beginners are advised to use the CLI instead of relying on the GUI
  2. Pull docker image from official docker document:docker/getting-started
docker run -d -p 81:80 docker/getting-started
Copy the code
  • -d, detached mode, background operation
  • -p, port, port, local port :Docker internal port
  • -d -p = -dp
docker run -dp 81:80 docker/getting-started
Copy the code
  1. Open after running: http://localhost:81/, map to Docker port 80, this service do not stop or delete the container, later Demo useful

Demo

Dockerized (image made for sharing or publishing)

  1. Download the Demo code and decompress it
  2. Switch directory:CD Decompress directory /app
  3. createDockerfile:touch Dockerfile:
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY.
RUN yarn install --production
CMD ["node"."src/index.js"]
Copy the code
  1. Build a new image (.Meaning current directory) :
docker build -t getting-started .
Copy the code
  1. This command will automatically download and install the environment dependencies, and finally install the NPM dependencies and run:
docker run -dp 3001:3000 getting-started
Copy the code
  1. Open: http://localhost:3001/

share

Docker Hub Docker Hub Docker Hub Docker Hub Docker Hub Docker Hub Docker Hub Docker Hub Once logged in, click Create Repository:

Create (need to be set to public for testing) :

  1. Login:docker login -u YOUR-USER-NAME, enter the password
  2. Play tag:docker tag getting-started YOUR-USER-NAME/getting-started
  3. Push:docker push YOUR-USER-NAME/getting-started
  4. After the upload succeeds, refresh the page:

  1. Pull the newly published image:docker run -dp 3001:3000 YOUR-USER-NAME/getting-started
  2. Open: http://localhost:3001/

It worked.

The DB persistence

After the container is redeployed, the internal data will be emptied, and the database needs to be placed in the VM of Docker for management.

  1. Creating a volume (storage Space) :docker volume create todo-db
  2. Specify volumes when creating a container:docker run -dp 3001:3000 -v todo-db:/etc/todos getting-started
  3. View volume (Mountpoint is located in Docker VM, physical hard disk cannot be found, you must enter Docker VM to view) :
docker volume inspect todo-db
[
    {
        "CreatedAt": "2019-09-26T02:18:36Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/todo-db/_data",
        "Name": "todo-db",
        "Options": {},
        "Scope": "local"
    }
]
Copy the code

Dev heat more

Using Bind Mounts, you can control the exact mount points on your host. We can use it to persist data, but it is usually used to provide additional data to the container. When working with an application, we can use bind mount to mount the source code into a container, allowing the container to see the code changes, respond, and let us see the changes immediately.

Since the demo project is a Node project, Nodemon is a great tool for node-based applications to monitor file changes and then restart the application. Similar tools exist in most other languages and frameworks.

  1. Make sure you haven’t run any startup containers before,Switch to the project directory
  2. Run:
docker run -dp 3000:3000 \
    -w /app -v "$(pwd):/app" \
    node:12-alpine \
    sh -c "yarn install && yarn run dev"
Copy the code

PowerShell

docker run -dp 3000:3000 `
    -w /app -v "$(pwd):/app" `
    node:12-alpine `
    sh -c "yarn install && yarn run dev"
Copy the code
  1. usedocker logs-f < container-id >Monitor log
  2. Change the code can be hot

Quick query of common commands

View image: docker image ls View container: docker ps Monitor logs: docker logs-f < container-id >

Pay special attention to

  • Note That the port is occupied. If the command is occupied, the startup command fails. Stop or delete the corresponding container

Each big handsome boy, big beautiful, four encourage again and again below: dot like, message, collect, concern, promise me!