Startup

1
docker container run hello-world

first pull the image from the registry
then run this image to become a container

1
2
docker image pull alpine
docker image ls #the same as 'docker images'
1
docker container run alpine ls -l

a few things happen:

  • find the image
  • run image ==> container
  • run cmd
  • close container
1
docker container run -it alpine /bin/sh

launch the shell and you can type in the terminal

swarm

in one node: (manager)

1
docker swarm init --advertise-addr $(hostname -i)

in other node: (worker) copy & paste

1
docker swarm join --token <token> <host>

in the manager node

1
docker node ls

create a stack in the manager

1
docker stack deploy --compose-file=docker-stack.yml voting_stack

a docker-stack.yml is like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# this file is meant for Docker Swarm stacks only
# trying it in compose will fail because of multiple replicas trying to bind to the same port
# Swarm currently does not support Compose Spec, so we'll pin to the older version 3.9

version: "3.9"

services:

redis:
image: redis:alpine
networks:
- frontend

db:
image: postgres:15-alpine
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend

vote:
image: dockersamples/examplevotingapp_vote
ports:
- 8080:80
networks:
- frontend
deploy:
replicas: 2

result:
image: dockersamples/examplevotingapp_result
ports:
- 8081:80
networks:
- backend

worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
replicas: 2

networks:
frontend:
backend:

volumes:
db-data:

![[{3A3E94E1-7C8E-4C55-9E2F-155FDB1904DD}.png]]

container

docker run : 新建并启动

1
docker run -t -i ubuntu:18.04 /bin/bash

start a exited container

1
docker container start

后台运行:-d

1
docker run -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

在后台后可以通过 container logs 查看标准输出

容器终止:

1
docker container stop

或者等所有命令结束自动终止

删除所有终止状态的容器

1
docker container prune

Dockerfile