• Docker series: github.com/LiangJunron…

First, the previous review & preface

Nginx and Node containers and some simple concepts and instructions.

So this article will explain some of the instructions and operations that newcomers to Docker need to know.

  • Image instruction
  • Container directive
  • The Shell commands
  • Host and container code synchronization
  • Isolate the Hosts
  • Modifying the container time

Image command

  • Creating an image:Docker image build./ -t hello-docker:1.0.0
  • View the mirror list:docker image ls

  • Deleting a mirror:docker rmi <image id>
  • Delete all mirrors:docker rmi $(docker images -q)
  • Forcibly deleting all mirrors:docker rmi -f $(docker images -q)

Container Directive

  • Create a container:Docker container create -p 2333:80 Hello-docker :1.0.0
  • Run the container (where the long string of characters is the result of creating the container) :docker container start 4a4d2ac6bf3977ee7c5b4a15562bab17d59b4c61531908fd52632fb0c989f430
  • Create and run live containers (exit to close and delete containers) :Docker runit --rm --name hell-docker -p 2333:80 hell-docker :1.0.0
  • View the container list:docker ps -a

  • Enter the specified container:docker exec -it 7961990657ef bash
    • bash: varies according to different images, for examplealpineIt needs to become/bin/sh

After entering a Container, switch between Shell commands (essentially the same as VS Code editor terminal, Windows CMD, etc.)

  • Exit container:exit
  • Start/stop/restart containers:docker start/stop/restart CONTAINER_ID
  • Delete a single container:docker rm CONTAINER_ID
  • Force a container to drop:docker rm -f CONTAINER_ID
  • Stop all containers:docker stop $(docker ps -a -q)ordocker stop $(docker ps -aq)
  • Delete all containers:docker rm $(docker ps -a -q)ordocker rm $(docker ps -aq)

Shell command

  • View current folder:ls

  • Open the specified folder:cd html

  • View the specified file contents (with line numbers) :cat -n index.html

4. Code synchronization

It was easy to change the code when we started the native Docker service.

But! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Let’s use the Node.js Demo as an example to see how to synchronize code.

Code repository address: github.com/LiangJunron…

  • Copy host code to container:
Docker cp "E: / MyWeb / 036 - docker for Node/README. Md" 8 e1910aa2a12: / usr/SRC/app/README. Md docker cp hosting the path of the container name | container ID: path of the file in the containerCopy the code

Docker cp E:/ myweb/036 – docker for Node/ SRC /usr/src/app/ Copy, copy, copy

  • Copy the container code to the host:
8 e1910aa2a12: docker cp/usr/SRC/app/tsconfig. Json E: \ MyWeb \ all - for - one docker cp container name: container the path of the file path of the host machineCopy the code
  • Let the container code synchronize with the host code in real time:
Docker run -d E:\MyWeb\all-for-one\ SRC :/usr/src/app/src docker-node:1.0.0 docker run -d v Docker run -d v docker run -d E:\MyWeb\all-for-one\ SRC :/usr/src/app/src docker-node:1.0.0 docker run -d vCopy the code

What we need to know:

  1. -d: Background running
  2. -v A:B: Mount files/folders from host path A to container B (both are absolute paths)
  3. Docker -node: 1.0.0: that is,docker image lsCheck toREPOSITORYTAG

5. Isolate Hosts

After entering the container, you can view the hosts of the container by running cat /etc/hosts:

By the way, we have changed the local Hosts file to 127.127.127.127. Ping Baidu.com from the container, we can see the Hosts changes with the host.

So how do you get it to have a mind of its own and not move?

First, we find the project directory and add two files:

hosts

127.0.0.1       localhost
Copy the code

resolv.conf

Nameserver 114.114.114.114Copy the code

For these two files, one is to modify Hosts and the other is to modify DNS port. That is, we want to make the container open with independent Hosts and DNS port.

Next, we run the command:

  • docker run -d -v E:\MyWeb\all-for-one\hosts:/etc/hosts -v E:\MyWeb\all-for-one\resolv.conf:/etc/resolv.conf Docker -node: 1.0.0

This creates a container that acts as an isolation.

Finally, we can enter the new container and ping Baidu.com:

As you can see, we have successfully modified the container Hosts!

Modify the container time

Normally, the Docker container time is different from our machine time, because it is set to the default time zone problem:

  • Query machine time:date

As you can see, the time of the container above, which is UTC, is completely different from that of the host machine below.

However, some of our code needed to store the correct time, and this had to be changed.

Modifying them is as simple as setting up a Dockerfile:

Dockerfile

# set the Docker container time zone RUN apk add - no - cache tzdata && cp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime \ && echo Asia/Shanghai > /etc/timezoneCopy the code

Let’s use the node.js service as an example to try to unify the host and container times.

Code repository address: github.com/LiangJunron…

Let’s create an image and run the container as per readme. md of the repository:

Enter the container to view date:

You can see the time is normal!

Seven, summary

In this chapter we have sorted out some common content, small friends in the first time to see so many instructions may be a little confused, in fact, Jsliang is also a bit of knocking up proficiency.

So these can only be more familiar, I tried a few days before did not knock, turned back to forget, so sorted into this article is convenient for myself and everyone to review.

Stay tuned for part 2:06 – Getting Started & Puppeteer Services


Do not toss the front end, and what is the difference between salted fish!

Thumbs up/Star if you think the article is good.

If you need to contact jsliang:

  • Github

Personal contact information stored in Github home page, welcome to toss ~

Strive to become a lifelong learning slash programmer who is eager to explore, play around, and expand his knowledge.

Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.