docker容器教程
From official docs: https://docs.docker.com/engine/install/ubuntu/
来自官方文档: https : //docs.docker.com/engine/install/ubuntu/
Uninstalling old versions:
卸载旧版本:
sudo apt-get remove docker docker-engine docker.io containerd runcSet up the respository
设置存储库
sudo apt updateInstall packages to allow apt to use a repository over HTTPS:
安装软件包以允许通过HTTPS使用存储库:
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-commonAdd Docker’s official GPG key:
添加Docker的官方GPG密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -Add Docker repository:
添加Docker存储库:
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"Install Docker CE
安装Docker CE
$ $ sudo apt-get install docker-ce docker-ce-cli containerd.ioVerify that Docker CE is installed correctly by running the hello-world image:
通过运行hello-world映像来验证Docker CE是否已正确安装:
$ sudo docker run hello-world$The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
docker守护程序绑定到Unix套接字而不是TCP端口。 默认情况下,Unix套接字是由root用户拥有的,其他用户只能使用sudo访问它。 docker守护程序始终以root用户身份运行。
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
如果你不想使用sudo当您使用docker命令,创建一个Unix组名为docker和用户添加到它。 当docker守护程序启动时,它将使docker组可以读写Unix套接字的所有权。
To create the docker group and add your user:
要创建docker组并添加用户:
Create the docker group and add your user to the docker group:
创建docker组并将您的用户添加到docker组:
sudo groupadd dockersudo usermod -aG docker $USER2. Log out and log back in so that your group membership is re-evaluated.
2.注销并重新登录,以便重新评估您的组成员身份。
You can find existing images at https://store.docker.com/ or by using the command docker search <name>. For example, to find an image for Redis, you would use:
您可以在https://store.docker.com/上找到现有图像,也可以使用docker search <name>.命令找到现有图像docker search <name>. 例如,要查找Redis的图像,可以使用:
docker search redisThe Docker CLI has a command called run which will start a container based on a Docker Image. The structure is docker run <options> <image-name>/.
Docker CLI有一个名为run的命令,它将基于Docker Image启动一个容器。 结构为docker run <options> <image-name> /。
To run the latest (default) release of Redis image as a background service:
要将Redis图像的最新 (默认)版本作为后台服务运行:
docker run -d redisIf a particular version was required, it could be specified as a tag, for example, version 3.2 would be docker run -d redis:3.2.
如果需要特定版本,则可以将其指定为标记,例如,版本3.2将为docker run -d redis:3.2 。
Note: without option -d, the container will start then stop immediately after finishing specific command. For example: docker run alpine ls /
注意:没有选项-d ,容器将启动,然后在完成特定命令后立即停止。 例如: docker run alpine ls /
Lists all running containers, the image used to start the container and uptime:
列出所有正在运行的容器,用于启动容器的图像和正常运行时间:
docker psSample out:
取样:
Get more details about a running container:
获取有关正在运行的容器的更多详细信息:
docker inspect <friendly-name|container-id>Display messages the container has written to standard error or standard out:
显示容器已写入标准错误或标准输出的消息:
docker logs <friendly-name|container-id>The reason is that each container is sandboxed. If a service needs to be accessible by a process not running in a container, then the port needs to be exposed via the Host. Ports are bound when containers are started using -p <host-port>:<container-port> option:
原因是每个容器都被沙箱化。 如果服务需要由不在容器中运行的进程访问,则该端口需要通过主机公开。 使用-p <host-port>:<container-port>选项启动容器时,将绑定端口 :
docker run -d --name redisHostPort -p 6379:6379 redis:latestBy default, the port on the host is mapped to 0.0.0.0, which means all IP addresses. You can specify a particular IP address when you define the port mapping, for example, -p 127.0.0.1:6379:6379
默认情况下,主机上的端口映射到0.0.0.0,即所有IP地址。 定义端口映射时,可以指定特定的IP地址,例如-p 127.0.0.1:6379:6379
The option -p 6379 exposes Redis but on a randomly available port. This will allow us to run multiple instances of Redis container at the same time:
选项-p 6379公开Redis,但在随机可用的端口上。 这将使我们能够同时运行多个Redis容器实例:
docker run -d --name redisDynamic -p 6379 redis:latestTo discover that random port, use this command: docker port redisDynamic 6379 or use familiar docker ps command.
要发现该随机端口,请使用以下命令: docker port redisDynamic 6379或使用熟悉的docker port redisDynamic 6379 docker ps命令。
Containers are designed to be stateless, in the way that that the data stored keeps being removed when you delete and re-create a container. Binding directories (also known as volumes) with option -v <host-dir>:<container-dir> allow data to be persisted on the host. This allows you to upgrade or change containers without losing your data.
容器被设计为无状态的,以这种方式存储的数据在您删除并重新创建容器时会不断被删除。 使用选项-v <host-dir>:<container-dir>绑定目录(也称为卷)可以使数据持久保存在主机上。 这使您可以升级或更改容器而不会丢失数据。
docker run -d --name redisMapped \-v /opt/docker/data/redis:/data redisdocker ps -aTo exec a command inside the container:
要在容器内执行命令:
docker exec {container-id} {command}To interact with the container (for example, to access a bash shell) you could include the options -it:
要与容器交互(例如,访问bash shell),可以包括-it选项:
docker exec -it {container-id} shor:
要么:
docker exec -it {container-id} /bin/bashList containers:
列出容器:
docker ps -aStop using container id
停止使用容器ID
docker stop <id>Remove a container
取出容器
docker rm <id>List images
列出图片
docker imagesRemove an image
移除图片
docker image rm <image_name:tag or id>Docker Images start from a base image. The base image should include the platform dependencies required by your application, for example, having the JVM or CLR installed.
Docker映像从基础映像开始。 基本映像应包括应用程序所需的平台依赖性,例如,安装了JVM或CLR。
This base image is defined as an instruction in the Dockerfile. Docker Images are built based on the contents of a Dockerfile. The Dockerfile is a list of instructions describing how to deploy your application.
该基本映像在Dockerfile被定义为指令。 Docker映像是基于Dockerfile的内容Dockerfile 。 Dockerfile是说明如何部署应用程序的指令列表。
In this example, our base image is the Alpine version of Nginx. This provides the configured web server on the Linux Alpine distribution.
在此示例中,我们的基本映像是Nginx的Alpine版本。 这将在Linux Alpine发行版上提供已配置的Web服务器。
Create your Dockerfile for building your image by copying the contents below into the editor:
通过将以下内容复制到编辑器中来创建用于构建映像的Dockerfile :
FROM nginx:alpineCOPY . /usr/share/nginx/htmlYou should have an index.html file as well:
您还应该有一个index.html文件:
<h1>Hello World</h1>The Dockerfile is used by the Docker CLI build command. The build command executes each instruction within the Dockerfile. The result is a built Docker Image that can be launched and run your configured app.
该Dockerfile由多克尔CLI 生成命令使用。 build命令执行Dockerfile中的每条指令。 结果是构建的Docker映像可以启动并运行您配置的应用程序。
The format is docker build -t <build-directory>. The -t parameter allows you to specify a friendly name for the image and a tag, commonly used as a version number.
格式为docker build -t <build-directory> 。 -t参数允许您指定图像的友好名称和标签,通常用作版本号。
docker build -t webserver-image:v1 .Then you can view a list of all the images on the host using:
然后,您可以使用以下命令查看主机上所有图像的列表:
docker imagesLaunch our newly built image providing the friendly name and tag. As it’s a web server, bind port 80 to our host using the -p parameter.
启动我们提供的友好名称和标签的新图像。 由于是Web服务器,请使用-p参数将端口80绑定到我们的主机。
docker run -d -p 80:80 --name webapp webserver-image:v1Once started, you’ll be able to access the results of port 80 via curl docker
启动后,您将可以通过curl docker访问端口80的结果
When working with multiple containers, it can be difficult to manage the starting along with the configuration of variables and links. To solve this problem, Docker has a tool called Docker Compose to manage the orchestration, of launching, of containers.
当使用多个容器时,可能难以管理启动以及变量和链接的配置。 为了解决这个问题,Docker有一个名为Docker Compose的工具来管理容器的编排,启动。
Official docs: https://docs.docker.com/compose/install/
官方文档: https : //docs.docker.com/compose/install/
To install Docker Compose, just download latest version (found on the link above) and apply executable permission to the binary:
要安装Docker Compose,只需下载最新版本(在上面的链接中找到)并将可执行权限应用于二进制文件即可:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-composedocker-compose --versionAnother tutorial:
另一个教程:
Docker Compose files work by applying mutiple commands that are declared within a single docker-compose.yml configuration file. The basic structure of a Docker Compose YAML file looks like this:
Docker Compose文件通过应用在单个docker-compose.yml配置文件中声明的多个命令来工作。 Docker Compose YAML文件的基本结构如下所示:
version: 'X'services: web: build: . ports: - "5000:5000" volumes: - .:/code redis: image: redisExample:
例:
version: '3'services: web: # Path to Dockerfile. # '.' represents the current directory in which # docker-compose.yml is present. build: . # Mapping of container port to host ports: - "5000:5000" # Mount volume volumes: - "/usercode/:/code" # Link database container to app container for reachability. links: - "database:backenddb" database: # image to fetch from docker hub image: mysql/mysql-server:5.7 # Environment variables for startup script # container will use these variables # to start the container with these define variables. environment: - "MYSQL_ROOT_PASSWORD=root" - "MYSQL_USER=testuser" - "MYSQL_PASSWORD=admin123" - "MYSQL_DATABASE=backend" # Mount init.sql file to automatically run # and create tables for us. # everything in docker-entrypoint-initdb.d folder # is executed as soon as container is up nd running. volumes: - "/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"These commands should be run inside a folder with docker-compose.yml.
这些命令应在带有docker-compose.yml的文件夹中运行。
Builds images in the docker-compose.yml file:
在docker-compose.yml文件中构建图像:
docker-compose buildCreate and start containers:
创建并启动容器:
docker-compose upStart and run containers in background (detach mode):
在后台启动和运行容器(分离模式):
docker-compose up -dStops the running containers of specified services:
停止运行指定服务的容器:
docker-compose stopList all the containers in the current docker-compose file:
列出当前docker-compose文件中的所有容器:
docker-compose psRemove containers, networks and images (for services defined in the compose file):
删除容器,网络和映像(对于在撰写文件中定义的服务):
docker-compose downScale with multiple instances of a service:
扩展服务的多个实例:
docker-compose up -d --scale SERVICE=NUMfor example:
例如:
docker-compose up -d --scale workers=5翻译自: https://medium.com/@thucnc/a-docker-containers-tutorial-a6d6184a5617
docker容器教程
相关资源:四史答题软件安装包exe