Skip to content

Commit d02d3f8

Browse files
committed
Add individual docker run commands
1 parent 6ff0790 commit d02d3f8

9 files changed

Lines changed: 230 additions & 37 deletions

File tree

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
.PHONY: compose-build
2-
compose-build:
3-
docker compose build
4-
5-
.PHONY: compose-up
6-
compose-up:
7-
docker compose up
8-
9-
.PHONY: compose-up-build
10-
compose-up-build:
11-
docker compose up --build
12-
13-
.PHONY: compose-down
14-
compose-down:
15-
docker compose down
16-
171
.PHONY: build-sample
182
build-sample:
193
DOCKER_BUILDKIT=1 docker build \
@@ -35,24 +19,6 @@ build-multiarch:
3519
--push \
3620
.
3721

38-
define ENTRYPOINT_CMD_DESCRIPTION
39-
##############################
40-
41-
See Dockerfile.sample for image definition.
42-
43-
This series of docker run commands is meant
44-
to help you understand the interaction between
45-
CMD and ENTRYPOINT.
46-
47-
The image has the following:
48-
49-
ENTRYPOINT [ "echo", "Hey Team 👋 (entrypoint)" ]
50-
CMD [ "+ (cmd)" ]
51-
52-
##############################
53-
endef
54-
export ENTRYPOINT_CMD_DESCRIPTION
55-
5622
.PHONY: run-sample-entrypoint-cmd
5723
run-sample-entrypoint-cmd: build-sample
5824
@echo "$$ENTRYPOINT_CMD_DESCRIPTION"
@@ -70,4 +36,24 @@ run-sample-entrypoint-cmd: build-sample
7036

7137
@echo "##############################"
7238
@echo "Overriden entrypoint with arguments (CMD is ignored):"
73-
docker run --entrypoint echo sample "Hey Team 👋 (Overriden entrypoint + arguments)"
39+
docker run --entrypoint echo sample "Hey Team 👋 (Overriden entrypoint + arguments)"
40+
41+
define ENTRYPOINT_CMD_DESCRIPTION
42+
43+
✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
44+
45+
See Dockerfile.sample for image definition.
46+
47+
This series of docker run commands is meant
48+
to help you understand the interaction between
49+
CMD and ENTRYPOINT.
50+
51+
The image has the following:
52+
53+
ENTRYPOINT [ "echo", "Hey Team 👋 (entrypoint)" ]
54+
CMD [ "+ (cmd)" ]
55+
56+
✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
57+
58+
endef
59+
export ENTRYPOINT_CMD_DESCRIPTION

‎06-building-container-images/client-react/Dockerfile.5‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1.5
2+
13
FROM node:19.4-bullseye AS build
24

35
# Specify working directory other than /

‎07-container-registries/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Container Registries
2+
3+
A container registry is a repository, or collection of repositories, used to store and access container images. They serve as a place to store and share container images between developer systems, continuous integration servers, and deployment environments.
4+
5+
![](./images/container-registry.jpg)
6+
7+
Examples of popular container registries include:
8+
9+
- [Dockerhub](https://hub.docker.com)
10+
- [Github Container Registry (ghcr.io.)](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry)
11+
- [Gitlab Container Registry](https://docs.gitlab.com/ee/user/packages/container_registry/)
12+
- [Google Container Registry (gcr.io)](https://cloud.google.com/container-registry)
13+
- [Amazon Elastic Container Registry (ECR)](https://aws.amazon.com/ecr/)
14+
- [Azure Container Registry (ACR)](https://azure.microsoft.com/en-us/products/container-registry)
15+
- [jFrog Container Registry](https://jfrog.com/container-registry/)
16+
- [Nexus](https://blog.sonatype.com/nexus-as-a-container-registry)
17+
- [Harbor](https://goharbor.io/)
18+
19+
## Authenticating to Container Registries
20+
21+
While you can pull many public images from registries without authenticating, in order to push images to a registry, or pull a private image, you will need to authentic
22+
23+
Docker can login directly to some registries with basic authentication (username/password) or call out to separate programs known as credential helpers. For example, to authenticate to the Google Container Registry, docker uses the `gcloud` command line utility from GCP (https://cloud.google.com/container-registry/docs/advanced-authentication#gcloud-helper).
24+
25+
If available, Docker can also store the credentials in a secure store (`macOS keychain`, `Windows Credential Manager`) to help protect those credentials.
26+
27+
![](./images/credential-helper.jpg)
28+
77.1 KB
Loading
31.7 KB
Loading

‎08-running-containers/Makefile‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
### DOCKER COMPOSE COMMANDS
2+
3+
.PHONY: compose-build
4+
compose-build:
5+
docker compose build
6+
7+
.PHONY: compose-up
8+
compose-up:
9+
docker compose up
10+
11+
.PHONY: compose-up-build
12+
compose-up-build:
13+
docker compose up --build
14+
15+
.PHONY: compose-down
16+
compose-down:
17+
docker compose down
18+
19+
### DOCKER CLI COMMANDS
20+
21+
DOCKERCONTEXT_DIR:=../05-example-web-application/
22+
DOCKERFILE_DIR:=../06-building-container-images/
23+
24+
.PHONY: docker-build-all
25+
docker-build-all:
26+
docker build -t client-react-vite -f ${DOCKERFILE_DIR}/client-react/Dockerfile.3 ${DOCKERCONTEXT_DIR}/client-react/
27+
28+
docker build -t client-react-ngnix -f ${DOCKERFILE_DIR}/client-react/Dockerfile.5 ${DOCKERCONTEXT_DIR}/client-react/
29+
30+
docker build -t api-node -f ${DOCKERFILE_DIR}/api-node/Dockerfile.7 ${DOCKERCONTEXT_DIR}/api-node/
31+
32+
docker build -t api-golang -f ${DOCKERFILE_DIR}/api-golang/Dockerfile.6 ${DOCKERCONTEXT_DIR}/api-golang/
33+
34+
.PHONY: docker-run-all
35+
docker-run-all:
36+
echo "$$DOCKER_COMPOSE_NOTE"
37+
38+
# Stop and remove all running containers to avoid name conflicts
39+
$(MAKE) docker-stop
40+
41+
$(MAKE) docker-remove
42+
43+
docker run -d \
44+
--name db \
45+
-e POSTGRES_PASSWORD=foobarbaz \
46+
-p 5432:5432 \
47+
--restart unless-stopped \
48+
postgres:15.1-alpine
49+
50+
docker run -d \
51+
--name api-node \
52+
-e DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres \
53+
-p 3000:3000 \
54+
--restart unless-stopped \
55+
--link=db \
56+
api-node
57+
58+
docker run -d \
59+
--name api-golang \
60+
-e DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres \
61+
-p 8080:8080 \
62+
--restart unless-stopped \
63+
--link=db \
64+
api-golang
65+
66+
docker run -d \
67+
--name client-react-vite \
68+
-v ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js \
69+
-p 5173:5173 \
70+
--restart unless-stopped \
71+
--link=api-node \
72+
--link=api-golang \
73+
client-react-vite
74+
75+
docker run -d \
76+
--name client-react-nginx \
77+
-p 5174:80 \
78+
--restart unless-stopped \
79+
--link=api-node \
80+
--link=api-golang \
81+
client-react-ngnix
82+
83+
docker-stop:
84+
-docker stop db
85+
-docker stop api-node
86+
-docker stop api-golang
87+
-docker stop client-react-vite
88+
-docker stop client-react-nginx
89+
90+
docker-remove:
91+
-docker container rm db
92+
-docker container rm api-node
93+
-docker container rm api-golang
94+
-docker container rm client-react-vite
95+
-docker container rm client-react-nginx
96+
97+
define DOCKER_COMPOSE_NOTE
98+
99+
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
100+
101+
❯ NOTE:
102+
103+
This command runs the example app with a bunch
104+
of individual docker run commands. This is much
105+
easier to manage with docker-compose (see
106+
docker-compose.yml and compose make targets above)
107+
108+
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
109+
110+
111+
endef
112+
export DOCKER_COMPOSE_NOTE

‎08-running-containers/README.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Running Containers (with Docker)
2+
3+
Documentation: https://docs.docker.com/engine/reference/run/
4+
5+
Options everyone should know:
6+
```
7+
-d
8+
--entrypoint
9+
--env, -e, --env-file
10+
--init
11+
--interactive, -i
12+
--mount, --volume, -v
13+
--name
14+
--network, --net
15+
--platform
16+
--publish, -p
17+
--restart
18+
--rm
19+
--tty, -t
20+
```
21+
22+
Less commonly used by worth knowing:
23+
24+
```bash
25+
--cap-add, --cap-drop
26+
--cgroup-parent
27+
--cpu-shares
28+
--cpuset-cpus (pin execution to specific CPU cores)
29+
--device-cgroup-rule,
30+
--device-read-bps, --device-read-iops, --device-write-bps, --device-write-iops
31+
--gpus (NVIDIA Only)
32+
--health-cmd, --health-interval, --health-retries, --health-start-period, --health-timeout
33+
--memory , -m
34+
--pid, --pids-limit
35+
--privileged
36+
--read-only
37+
--security-opt
38+
--userns
39+
```
40+
41+
## Example web app
42+
43+
### individual docker run commands
44+
45+
See Makefile:
46+
```bash
47+
make docker-build-all
48+
make docker-run-all
49+
```
50+
51+
- Uses the default docker bridge network
52+
- Uses `--link` to enable easy host name for network connections
53+
- Publishing ports useful to connect to each service individually from host, but only necessary to connect to the frontend
54+
- Named containers make it easier to reference (e.g. with link), but does require removing them to avoid naming conflict
55+
- Restart policy allows docker to restart the container (for example if database weren't up yet causing one of the api servers to crash)
56+
57+
### docker compose
58+
59+
See Makefile:
60+
```bash
61+
make compose-build
62+
make compose-up
63+
```
64+
65+
Using docker compose allows encoding all of the logic from the `docker build` and `docker run` commands into a single file. Docker compose also manages naming of the container images and containers, attaching to logs from all the containers at runtime, etc...
File renamed without changes.

06-building-container-images/docker-compose.yml renamed to 08-running-containers/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
2-
client-react:
2+
client-react-vite:
33
build:
44
context: ../05-example-web-application/client-react/
55
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
@@ -11,7 +11,7 @@ services:
1111
client-react-nginx:
1212
build:
1313
context: ../05-example-web-application/client-react/
14-
dockerfile: ../../06-building-container-images/client-react/Dockerfile.4
14+
dockerfile: ../../06-building-container-images/client-react/Dockerfile.5
1515
init: true
1616
ports:
1717
- 5174:80

0 commit comments

Comments
 (0)