Ubuntu Linux:
sudo apt install docker.io docker-compose
sudo usermod -a -G docker $USER # re-login or restart
Mac:
brew install --cask docker
Windows: good luck! …
choco install docker-desktop
Let’s run some containers:
docker run hello-world # execute image's default CMD
docker run debian cat /etc/os-release # execute specific command
docker run -it debian # interactive TTY
apt update && apt install -y htop
), run it and watch the processes.docker search mysql
docker ps # list running containers
docker ps -a # list all containers
docker stop CONTAINER_ID
docker restart CONTAINER_ID
docker kill CONTAINER_ID
docker rm CONTAINER_ID # remove a container
docker exec -it mysql bash # execute `bash` in a running container
mysql -h 127.0.0.1 -p
docker pull ubuntu:20.04 # use a tag
docker images
docker rmi ubuntu:20.04
docker run
does automatically a pull if it doesn’t find the image.latest
docker system df
docker system prune
docker system info
Let’s persist the data
docker run --name mysql -v ~/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pw mysql
docker stop mysql
docker logs mysql
docker logs mysql --since 10m
docker rm mysql
We can mount in read-only mode too:
docker run -ti -v /etc/passwd:/tmp/passwd:ro busybox sh
docker run -d -p 3306:3306 --name mysql -v ~/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pw mysql
mysql -h 127.0.0.1 -p -u root
curl localhost # should fail
docker run --rm -d --network host --name my_nginx nginx
curl localhost # should work
-ti # allocate terminal; interactive
--name NAME # set a NAME for the container instead of a generated one
-d # run on the background (daemon)
-e VAR=VALUE # set environment `VAR` to `VALUE`.
--env-file FILE # set the environment values from a `FILE`.
--rm # automatically remove container when it exists
man docker-run
tldr docker-run
Dockerfile
- recipe to cook our own image.FROM node:20-alpine
WORKDIR /usr/src/app
USER node
COPY . .
EXPOSE 8080
CMD node app.js
Let’s use it:
docker build -t helloworld-demo-node .
docker run -d -p 8080:8080 helloworld-demo-node
curl localhost:8080
docker tag SOURCE_IMAGE TARGET_IMAGE # create a new tag (keeps old)
docker rmi OLD_TAG # remove a tag / image
docker build --platform linux/arm64 # cross build for Apple M1/M2
docker build -f DOCKERFILE . # if we have multiple Dockerfiles
FROM golang
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /main
EXPOSE 8080
ENTRYPOINT ["/main"]
docker build -f Dockerfile -t helloworld-go-demo .
docker run -p 8080:8080 helloworld-go-demo
docker images | grep helloworld # 913MB ...
FROM golang AS build-stage
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /main
# Run the tests in the container
FROM build-stage AS run-test-stage
RUN go test -v ./...
# Deploy the application binary into a lean image
FROM gcr.io/distroless/base-debian11 AS build-release-stage
WORKDIR /
COPY --from=build-stage /main /main
EXPOSE 8080
USER nonroot:nonroot
ENTRYPOINT ["/main"]
And check it out:
docker build -f Dockerfile.multistage -t helloworld-go-demo-light .
docker images | grep helloworld # 27 MB!
curl localhost:8080 # make sure it still works
version: "3.5"
services:
web-fe:
build: .
command: python app.py
ports:
- target: 5000
published: 5000
networks:
- counter-net
volumes:
- type: volume
source: counter-vol
target: /code
redis:
image: "redis:alpine"
networks:
counter-net:
networks:
counter-net:
volumes:
counter-vol:
docker-compose up
docker-compose up -d
docker-compose down
volumes
and ports
.docker save IMAGE > image.tar
docker load < image.tar
debian
image; remove it from images; load it. Now verify it is there among the images.docker exec -it container-ID bash
# do your changes
docker commit container-ID image-name
TASK: add the htop
package to your debian image
docker inspect IMAGE
mysql
image? What mounts does it have? What ports does it expose?docker update `docker ps -q` --restart no
docker kill `docker ps -q`
docker run --rm -ti \
--name=ctop \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
quay.io/vektorlab/ctop:latest