Difference between revisions of "Docker"
m |
m |
||
(21 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Docker= | =Docker= | ||
+ | |||
+ | https://docker.io | ||
+ | |||
+ | https://hub.docker.io | ||
+ | |||
+ | [[:File:Docker-cheat-sheet.pdf|Docker-cheat-sheet.pdf]] | ||
+ | |||
+ | Remote Docker Engine (aka containers running on a remote host) | ||
+ | docker -H=remote-docker-engine:2375 run nginx | ||
+ | |||
+ | |||
+ | ==Training== | ||
+ | |||
+ | https://www.youtube.com/watch?v=fqMOX6JJhGo | ||
+ | https://www.freecodecamp.org/ https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ | ||
+ | https://kodekloud.com/p/docker-labs | ||
==Containers== | ==Containers== | ||
Line 7: | Line 23: | ||
docker ps -a | docker ps -a | ||
Run a container | Run a container | ||
− | docker run [image] | + | docker run [image] [command] |
-d detached | -d detached | ||
-i interactive | -i interactive | ||
-t terminal (usually -it) | -t terminal (usually -it) | ||
-e VAR=value (environment variables) | -e VAR=value (environment variables) | ||
+ | -v VOLUME_NAME:/container/mount/point | ||
+ | --entrypoint=sleep [image] [attribute] | ||
--name name | --name name | ||
+ | --network network_name | ||
+ | --link real_container_name:target_name (adds entries to /etc/hosts) <-- Depreciated | ||
+ | |||
run:version (using a tag) | run:version (using a tag) | ||
− | + | Stop a container | |
+ | docker stop [name]/[id] | ||
Port mapping | Port mapping | ||
Line 30: | Line 52: | ||
Remove image (must have no containers attached) | Remove image (must have no containers attached) | ||
docker rmi [image] | docker rmi [image] | ||
+ | |||
+ | Repository | ||
+ | |||
+ | Docker Hub | ||
+ | |||
+ | Assumes docker.io | ||
+ | image: nginx/nginx account/image <-- if nginx is specified it uses that as the username/account name also | ||
+ | |||
+ | Private Registry | ||
+ | docker login private-registry.io | ||
+ | docker run private-registry.io/apps/internal-app | ||
+ | |||
+ | Deploy Private Registry | ||
+ | docker run -d -p 5000:5000 --name registry registry:2 | ||
+ | docker image tag my-image localhost:5000/my-image | ||
+ | docker push localhost:5000/my-image | ||
+ | docker pull localhost:5000/my-image | ||
==Dockerfile== | ==Dockerfile== | ||
Contains Instructions or Arguments | Contains Instructions or Arguments | ||
+ | |||
+ | CMD can be as per cli or in json format | ||
+ | <pre> | ||
+ | CMD command param1 | ||
+ | CMD ["command","param1"] | ||
+ | </pre> | ||
<pre>FROM ubuntu:18.04 | <pre>FROM ubuntu:18.04 | ||
COPY . /app | COPY . /app | ||
+ | ADD root/bashrc /root/.bashrc | ||
+ | ENV HOME /root | ||
+ | WORKDIR /root | ||
RUN make /app | RUN make /app | ||
CMD python /app/app.py</pre> | CMD python /app/app.py</pre> | ||
+ | |||
+ | ==Build== | ||
+ | docker build [path/Dockerfilefolder] | ||
+ | -t name:tag | ||
+ | |||
+ | Create a Ubuntu container that sleeps for 5 seconds before exiting | ||
+ | Dockerfile | ||
+ | From Ubuntu | ||
+ | CMD sleep 5 | ||
+ | |||
+ | docker build -t ubuntu-sleeper . | ||
+ | docker run ubuntu-sleeper | ||
+ | |||
+ | Dockerfile | ||
+ | From Ubuntu | ||
+ | ENTRYPOINT ["sleep"] | ||
+ | CMD ["5"] (becomes the default) | ||
+ | |||
+ | docker build -t ubuntu-sleeper . | ||
+ | docker run ubuntu-sleeper 10 | ||
+ | |||
+ | ==Networks== | ||
+ | docker network ls | ||
+ | |||
+ | Containers can resolve containers by name in the same network (internal dns server 127.0.0.11) | ||
+ | |||
+ | ===Create a network=== | ||
+ | docker network create --driver bridge --subnet 182.18.0.0/16 custom-isolated-network | ||
+ | |||
+ | ===Bridge=== | ||
+ | Internal network common to all docker containers in 172.17.0.0/24 network | ||
+ | |||
+ | ===None=== | ||
+ | Containers are isolated. | ||
+ | Containers are not connected to any network | ||
+ | |||
+ | ===Host=== | ||
+ | Container runs directly on the host network. | ||
+ | |||
+ | |||
+ | ==Storage== | ||
+ | Default storage | ||
+ | /var/lib/docker | ||
+ | |||
+ | Create volume | ||
+ | docker volume create data_volume (generated /var/lib/docker/volumes/data_volume | ||
+ | |||
+ | Use Volume | ||
+ | docker run --mount data_volume:/var/lib/mysql mysql | ||
+ | |||
+ | Bind Mounting (with new syntax) | ||
+ | docker run --mount type=bind,source=/host/data/mysql,target=/var/lib/mysql mysql | ||
+ | |||
+ | ==Compose== | ||
+ | Yaml - run multiple containers together on a single Docker Host. | ||
+ | docker compose up | ||
+ | |||
+ | /blah/application/docker-compose.yml | ||
+ | <pre> | ||
+ | version: 2 | ||
+ | services: | ||
+ | db: | ||
+ | environment: | ||
+ | POSTGRES_PASSWORD: mysecretpassword | ||
+ | image: postgres | ||
+ | wordpress: | ||
+ | image: wordpress | ||
+ | links: | ||
+ | - db | ||
+ | ports: | ||
+ | - 8085:80 | ||
+ | version: '3.0' | ||
+ | </pre> | ||
+ | replace image with build (and path) to use local built images | ||
+ | |||
+ | Start | ||
+ | docker-compose up | ||
+ | |||
+ | Versions of docker-compose files (supports all) | ||
+ | * Version 1 - can not specify order or networks (all bridge and then links) | ||
+ | * Version 2 - starts with services: at the start, and version: 2 (all in bridge network, no need to use links), also has depends on feature | ||
+ | * version 3 - supports docker swarm | ||
+ | |||
+ | |||
+ | <pre> | ||
+ | version: 2 | ||
+ | services: | ||
+ | redis: | ||
+ | image: redis | ||
+ | ... | ||
+ | neworks: | ||
+ | - back-end | ||
+ | vote: | ||
+ | image: voting-app | ||
+ | networks: | ||
+ | - front-end | ||
+ | - back-end | ||
+ | networks: | ||
+ | front-end: | ||
+ | back-end: | ||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | ==Engine - Resources - cgroups== | ||
+ | Control Groups | ||
+ | docker run --cpus=.5 ubuntu <-- limit to max 50% | ||
+ | docker run --memory=100m ubuntu <-- limit to 100MB | ||
+ | |||
+ | ==Docker Swarm== | ||
+ | H/A and distribution across multiple hosts | ||
+ | Swarm Manager 1 --> n Workers (Nodes) |
Latest revision as of 09:00, 23 August 2020
Contents
Docker
Remote Docker Engine (aka containers running on a remote host)
docker -H=remote-docker-engine:2375 run nginx
Training
https://www.youtube.com/watch?v=fqMOX6JJhGo https://www.freecodecamp.org/ https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ https://kodekloud.com/p/docker-labs
Containers
List running containers
docker ps
List all containers
docker ps -a
Run a container
docker run [image] [command] -d detached -i interactive -t terminal (usually -it) -e VAR=value (environment variables) -v VOLUME_NAME:/container/mount/point --entrypoint=sleep [image] [attribute] --name name --network network_name --link real_container_name:target_name (adds entries to /etc/hosts) <-- Depreciated
run:version (using a tag)
Stop a container
docker stop [name]/[id]
Port mapping
-p (host/external):(container/internal)
Directory mapping
-v /host/dir:/container/dir
Inspect (returns json)
docker inspect [container]
Images
List images
docker image ls
Remove image (must have no containers attached)
docker rmi [image]
Repository
Docker Hub
Assumes docker.io
image: nginx/nginx account/image <-- if nginx is specified it uses that as the username/account name also
Private Registry
docker login private-registry.io docker run private-registry.io/apps/internal-app
Deploy Private Registry
docker run -d -p 5000:5000 --name registry registry:2 docker image tag my-image localhost:5000/my-image docker push localhost:5000/my-image docker pull localhost:5000/my-image
Dockerfile
Contains Instructions or Arguments
CMD can be as per cli or in json format
CMD command param1 CMD ["command","param1"]
FROM ubuntu:18.04 COPY . /app ADD root/bashrc /root/.bashrc ENV HOME /root WORKDIR /root RUN make /app CMD python /app/app.py
Build
docker build [path/Dockerfilefolder] -t name:tag
Create a Ubuntu container that sleeps for 5 seconds before exiting Dockerfile
From Ubuntu CMD sleep 5
docker build -t ubuntu-sleeper . docker run ubuntu-sleeper
Dockerfile
From Ubuntu ENTRYPOINT ["sleep"] CMD ["5"] (becomes the default)
docker build -t ubuntu-sleeper . docker run ubuntu-sleeper 10
Networks
docker network ls
Containers can resolve containers by name in the same network (internal dns server 127.0.0.11)
Create a network
docker network create --driver bridge --subnet 182.18.0.0/16 custom-isolated-network
Bridge
Internal network common to all docker containers in 172.17.0.0/24 network
None
Containers are isolated. Containers are not connected to any network
Host
Container runs directly on the host network.
Storage
Default storage
/var/lib/docker
Create volume
docker volume create data_volume (generated /var/lib/docker/volumes/data_volume
Use Volume
docker run --mount data_volume:/var/lib/mysql mysql
Bind Mounting (with new syntax)
docker run --mount type=bind,source=/host/data/mysql,target=/var/lib/mysql mysql
Compose
Yaml - run multiple containers together on a single Docker Host.
docker compose up
/blah/application/docker-compose.yml
version: 2 services: db: environment: POSTGRES_PASSWORD: mysecretpassword image: postgres wordpress: image: wordpress links: - db ports: - 8085:80 version: '3.0'
replace image with build (and path) to use local built images
Start
docker-compose up
Versions of docker-compose files (supports all)
- Version 1 - can not specify order or networks (all bridge and then links)
- Version 2 - starts with services: at the start, and version: 2 (all in bridge network, no need to use links), also has depends on feature
- version 3 - supports docker swarm
version: 2 services: redis: image: redis ... neworks: - back-end vote: image: voting-app networks: - front-end - back-end networks: front-end: back-end:
Engine - Resources - cgroups
Control Groups
docker run --cpus=.5 ubuntu <-- limit to max 50% docker run --memory=100m ubuntu <-- limit to 100MB
Docker Swarm
H/A and distribution across multiple hosts Swarm Manager 1 --> n Workers (Nodes)