baicai

白菜

一个勤奋的代码搬运工!

Installing Docker and basic usage

Introduction to Docker, including 3 basic concepts#

1.1 Docker consists mainly of images and containers#

Image: A Docker image is like a template, similar to a file system

Container: Containers are created using images. Images and containers are like classes and instances in object-oriented programming. Containers can be created, started, stopped, and deleted, etc.

Repository: A repository is a place to store images, divided into private repositories and public repositories. Similar to Git

1.2 Docker's operating principle#

Docker is a Client-Server system, with the Docker daemon running on the host and accessed from the client through a socket. When the Docker server receives instructions from the Docker client, it executes the command.

I. Installing Docker on Mac#

1.1 Homebrew's cask should support Docker for Mac, so you can directly install it with brew cask install docker

1.2 You can also download it directly from the official website: https://download.docker.com/mac/stable/Docker.dmg

1.3 Docker's reference documentation: https://docs.docker.com

1.4 Docker Hub for finding image sources: https://hub.docker.com

II. Using Docker#

1.1 Check the version#

docker --version

docker-compose --version 

docker-machine --version

1.2 Check Docker system information, including the number of images and containers, etc.#

docker info

1.3 Help command#

docker help

1.4 Check CPU usage#

docker stats

III. Basic Docker commands#

Commands related to images:

1.1 Check available versions of an image (using nginx as an example)#

docker search nginx

1.2 Download an image#

docker pull nginx:latest #(followed by the image version)

1.3 Run an nginx server#

docker run -d -p 81:80 --name webserver nginx

Options:

#--name webserver: Container name for distinguishing containers

#-p 81:80: Port mapping, mapping the local port 81 to the internal port 80 of the container

#-v ~/nginx/html:/usr/share/nginx/html: Data volume mounting ro/rw, mounting the directory in the host project to the directory in the container. By default, rw can only be changed outside the host, and cannot be changed inside the container.

#-d: Run the container in the background

#-it: Run interactively, enter the container to view the content

#-P: Random port

#-e: Environment configuration settings

Note: To start in the background, there must be a foreground process. If Docker does not find an application, it will automatically stop.

Key point: Data volume mounting can achieve data sharing, container persistence, and synchronization operations. You can use docker volume to view the status of volumes, and use volumes-from to achieve data sharing between multiple containers.

1.4 Stop the nginx service#

docker stop webserver(container ID)

1.5 Delete the nginx service#

docker rm webserver

1.6 Start/restart the nginx service#

docker start/restart webserver

1.7 List all images (the list includes repository name, tag, image ID, creation time, and space occupied)#

docker images ls

Explanation:

REPOSITORY: Repository source of the image

TAG: Tag of the image

IMAGE ID: ID of the image

CREATED: Creation time of the image

SIZE: Size of the image

Options:

-a: List all images

-q: Only display the image ID

Note: The image ID is a unique identifier. One image can have multiple tags.

1.8 Check the space occupied by images, containers, and volumes#

docker system df

1.9 Delete an image#

Specify the image:

docker rmi [image name/image short ID/image long ID/image digest]

Multiple images:

docker rmi image ID image ID image ID

All images:

docker rmi $(docker images -aq)

2.0 Delete all images with the repository name "redis" using the docker images ls command#

docker rmi $(docker images ls -q redis)

2.1 View the running history of an image#

docker history image ID

Commands related to containers

1.1 List containers#

docker ps

Options:

-a: Show all containers, including those not running

-l: Show the most recently created container

-n: List the n most recently created containers

-q: Only display the container ID

1.2 Enter a container#

docker exec -it [container name] /bin/bash

docker attach container ID

Difference: docker exec opens a new terminal after entering the container, allowing operations inside; docker attach enters the terminal currently running in the container without starting a new process.

1.3 Exit a container#

Stop the container and return to the host:

exit

Exit the container without stopping it:

ctrl+p+q

1.4 Delete a container#

Specify the container:

docker rm [container ID]

Multiple containers:

docker rm container ID container ID container ID

All containers:

docker rm $(docker ps -aq)

docker ps -a -q | xargs docker rm

Note: You cannot delete a running container. To delete a running container, you need to add the -f parameter: docker rm -f container ID

1.5 Start/restart a container#

docker start/restart container ID

1.6 Stop/force stop a container#

docker stop/kill container ID

1.7 View container logs#

docker logs -f -t --tail 100 container ID

#--tail must be followed by the number of lines

1.8 View process information in a container#

docker top container ID

1.9 View metadata of a container (important command)#

docker inspect container ID

2.0 Copy data from a container to the host#

docker cp container ID:container path host path

IV. Dockerfile instructions

FROM: Base image, everything starts from here

MAINTAINER: Author of the image, Name <Email>

RUN: Commands to be run during image build

ADD: Step, add content

WORKDIR: Working directory of the image

VOLUME: Mounted directory

EXPOSE: Port configuration

CMD: Specifies the command to run when the container starts. Only the last one will take effect and can be replaced.

ENTRYPOINT: Specifies the command to run when the container starts, additional commands can be appended

ONBUILD: Runs when building an inherited Dockerfile

COPY: Similar to ADD, copies files to the image

ENV: Sets environment variables during the build process
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.