= Cheatsheet Docker =
**Summary**: Docker hints, tips, oneliners and best practices. \\
**Date**: 8 December 2024 \\
{{tag>cheatsheet docker}}
== Docker version ==
> Show the docker version
docker version
== Docker Images ==
> Working with images from docker hub
# Logout from docker hub
docker logout
# Login to docker hub
docker login -u username -p password
# Pull the image from docker hub repository
docker pull image-info
# Pull the image from docker hub repository
docker pull stacksimplify/springboot-helloworld-rest-api:2.0.0-RELEASE
# Remove the docker image
docker rmi image-id
== Docker Containers ==
> Show running container
docker ps
# or
docker container ls
\\
> Show all containers, including stopped ones
docker ps -a
\\
> docker stop/start/restart containers
# Stop
docker stop container_id
# default grace time = 10
docker stop --time=5 container_id
# Start
docker start container_id
# Restart
# Restart apache container by name
docker container restart httpd_web
\\
> Remove containers
# Remove the stopped container
docker rm container-id or name
# Remove the running container forcefully
docker rm -f container-id or name
\\
> Get docker container info
docker inspect container_id
\\
> Get docker container compose info
docker inspect container_id | grep com.docker.compose
\\
> Copy a file from or to a container
docker cp tool_db:/var/lib/postgresql/data/pgdata/postgresql.conf ./postgresql.conf
== Logging and Monitoring ==
> Get logging from docker container
docker logs --tail 50 --follow --timestamps httpd_web
\\
> grep in logging
docker logs 2>&1 | grep
Note that docker logs to stderr do you need to redirect this to stdout
\\
> Get container stats (Display the running processes of a container)
docker top
\\
> Get docker stats
docker stats
== Work Inside a Container ==
> Work in a container
docker exec -it container_id /bin/bash
# Connect to linux container and execute commands in container
docker exec -it container-name /bin/sh
\\
> As a different user
docker exec -u postgres -it container_id /bin/bash
== Docker Compose ==
> [[https://docs.docker.com/compose/|Docker compose]] files are used to start containers. By default, a docker-compose.yml or a docker-compose.yaml file is used.
> Start and stop containers from docker-compose file
docker compose up -d
docker compose down
Note the -d switch to start the containers in the background.
\\
> See logging
# the logs from each of the services interleaved into a single stream
docker compose logs -f
# the logs just for app
docker compose logs -f app
== Docker Build ==
>Docker build, run and show logs of a custom app called privacy
# Build an image called privacyimage
docker build -t privacyimage .
# Run a container called privacycontainer from the image
docker run -d --name privacycontainer privacyimage
# Show the logs from the container
docker logs privacycontainer
\\
# Show all steps in plain output and redo every step (don't use cached steps)
docker build --progress=plain --no-cache -t consoleappdbtestimage .
\\
> Save an image to disk and import somewhere else
# Create a tar file from the consoleappimage
docker save -o ./consoleappimage.tar consoleappimage
# import the image
docker load -i /consoleappimage.tar
== Dockerfile ==
A [[https://docs.docker.com/engine/reference/builder/|Dockerfile]] is used to create images:
Default .net app dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /App
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerConsoleApp.dll"]
== Docker Azure DevOps Pipeline Task ==
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(repository)
dockerfile: $(dockerFilePath)
containerRegistry: "containerRegistry"
tags: |
$(imageName)
$(Build.BuildId)
latest