Getting started with docker

Getting started with docker

Original link: ssshooter.com/2020-02-02-...

Simply put, docker can create a mirror for your application , and then you can easily run your application with the original configuration on other hosts or even other systems with this mirror, and such an application becomes a container behind it . Using docker will require much less performance than directly using virtual machines, so you can run multiple containers on the host and ensure performance. And because the containers are separated from each other, the mutual influence between environments can be reduced, and some errors that are difficult to debug can be avoided.

Before learning docker, you must know that the official docker document is our good teacher. It is very detailed, the document is huge but it does not appear very confusing, and you can turn it over when you are confused.

docker architecture

The above picture is from the official document . The client on the far left is a command line client that can control the docker daemon through the api. This daemon can be local or other servers. The daemon manages its images and containers. The right side refers to that you can remotely obtain all kinds of ready-made mirror images, standing on the shoulders of giants, making your development easier! The most commonly used source of these images is definitely Docker Hub, which is the official docker image storage center.

The following focus is on images and containers.

Mirroring

Just like the previous virtual CD-ROM drive, all files in the CD can be restored by opening the iso image, and the same is true for the docker image.

Generate mirror

Use Dockerfile by docker image buildconstructing mirror. (You can also upload the project to docker hub to build, but it is not necessary, unless your computer really can't install docker )

Taking web front-end engineering as an example, the following is the Dockerfile of ant design pro:

FROM circleci/node:latest-browsers

WORKDIR/usr/src/app/
USER root
COPY package.json ./
RUN yarn

COPY ././

RUN npm run test:all

RUN npm run fetch:blocks

CMD ["npm", "run", "build"]
 
  • FROM means to generate a new image based on the specified image, which is similar to a class, one inherits one, and abstracts layer by layer.
  • WORKDIR specifies the container directory
  • COPY AB means to copy from A of the dockerfile directory to the container directory B
  • RUN is executed before the container is created, that is, the result of RUN is included in the image
  • CMD execution is generally used to start the service after the container is created. Only one CMD command can exist

If you want to build an image of a local project, you can run it in the folder where the Dockerfile is located docker image build ..

One sentence summary: project + dockerfile = mirror

For more configuration, please refer to the dockerfile document .

Get an existing image

Image sharing is an important part of docker. You can find many useful ready-made images on docker hub.

Run docker image pullplus username/imagename able to pull to a local image, for example:

docker image pull library/hello-world
 

Other common operations include:

docker image ls
docker image rm
 

container

The concept of a container is relatively simple, which is a running instance of an image . The container can be generated by running the image, for example:

docker container run hello-world
 

One sentence summary: run image container

Other common operations include:

docker container ls
docker container rm
 

docker compose

By the way, briefly mention docker compose.

With compose, you can match other containers on the basis of containers.

Run docker-compose updirectly download the current folder docker-compose.yamlall the mirrors used in, run together after the download is complete. (For linux, docker-compose should be installed separately)

Of course, you may also be inconvenient to always use docker-compose.yamlthe file name, such as the need to distinguish between several different projects by file name, so if you do not use the default file name, you can use the -fparameters to run compose, back to keep the file name:

docker-compose -f docker-compose.projectName.yml up

More configurations, refer to compose-file documents , indeed, the configuration docker-compose.yamlis also a science.

In addition, Docker Compose documents have docker-composedocuments related commands.