Skip to main content

docker

Pull and Push Images

COMMANDDESCRIPTION
docker loginLogin to a registry
docker logoutLogout from a registry
docker imagesList all local images
docker pull REPO:[TAG]pull an image/repo from a registry
docker inspect IMAGEShow information (json formatted)
docker tag IMAGE TAGTag an image
docker push REPO:[TAG]push and image/repo to a registry
docker rmi IMAGEDelete images
...
> cat pass.txt | docker login -u 'user-name'  "registry.us-east-2.network" --password-stdin

or after configuring aws access

> aws sso login
> aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 780300....dkr.ecr.us-east-2.amazonaws.com
...
> docker tag other/service:develop mypersonalrepo/service:latest
> docker push mypersonalrepo/service:latest

Run Containers

COMMANDDESCRIPTION
docker run -d IMAGEStart a new container (-d detach) from the image. If not present, it will download it
docker run --name CONTAINER_NAME IMAGEStart a new container and set a name
docker run -p HOSTPORT:CONTAINERPORT IMAGEStart a new container with mapped ports
docker run -P IMAGEStart a new container and map all ports

Inspect Containers

COMMANDDESCRIPTION
docker ps -aList all containers, including stopped
docker statsShow statistics of all containers
docker logs -f --tail 100 CONTAINERFollow a container output (last 100 lines)
docker-compose logs -f eth1 2>>&1 / tee eth1.logFollow service eth1 and write it into a log
docker top CONTAINERList the processes running in a container
docker diffShow the differences with the image (modified files)
docker inspectShow information of a container (json formatted)
docker inspect -f '{{ json .Config.Env }}' container_nameShow specific variable

Interact with Containers

COMMANDDESCRIPTION
docker exec -it CONTAINER /bin/bashOpen an interactive shell inside a container
docker cp CONTAINER:PATH HOSTPATHCopy files from the container
docker cp HOSTPATH CONTAINER:PATHCopy files into the container
docker attach CONTAINERAttach to a container
docker run --entrypoint="" -it IMAGE shEnter to container when there are multiples services at different entrypoints

Stop Containers

COMMANDDESCRIPTION
docker stop CONTAINERGraceful stop a container
docker kill CONTAINERKill (SIGKILL) a container
docker restart CONTAINERGraceful stop and restart a container
docker rm CONTAINERDestroy a container

Clean up

COMMANDDESCRIPTION
docker kill $(docker ps -a -q)To kill all running and stopped containers
docker rm $(docker ps -q -f status=exited)To remove the containers
docker system prune -aTo delete all used and unused images
docker system prune --volumesTo delete all docker volumes
docker system pruneTo delete all dangling and unused images, containers, cache and volumes
sudo ln -s ~$USER/.rd/docker.sock /var/run/docker.sockTo reset docker service

Volumes

COMMANDDESCRIPTION
docker volume lsList all volumes
docker volume create VOLUMECreate a volume
docker volume inspect VOLUMEShow information (json formatted)
docker volume rm VOLUMEDestroy a volume
docker volume ls --filter="dangling=true"List all dangling volumes (not referenced by any container)
docker volume pruneDelete all volumes (not referenced by any container)
> docker volume create <volume_name>    
> docker run -d --name <container_name> —mount src=<volume_name>, dst= <container_data_path> <image_name>

Networks

COMMANDDESCRIPTION
docker network create --attachable <network_name>To create a network and allow other containers to join

Dockerfile


FROM registry/library/ubuntu:focal-20221130

# Install node16
RUN apt-get update && apt-get install -y curl && \
curl -sL https://deb.nodesource.com/setup_16.x | bash - && \
apt-get install -y nodejs unzip

# Set working directory
WORKDIR /opt/app

# Install the project dependencies
COPY package*.json tsconfig.json ./

RUN npm install -g ts-node && \
npm install -g typescript && \
npm ci --only=production

# Remove useless files from the current layer
RUN rm -rf /var/lib/apt/lists/* && \
rm -rf /var/lib/apt/lists.d/* && \
apt-get autoremove && \
apt-get clean && \
apt-get autoclean && \
npm cache clean --force

# Install playwright chromium dependencies
RUN npx playwright install --with-deps chromium

# Copy repository files
COPY . .

CMD ["sh", "-c", "/usr/bin/npm run test:all"]

> docker build -t image_name:tag_version . (context '.' the current path)

info

CMD is the PID 0, so when it finished the container exits automatically

RancherDesktop notes

If problems when using RancherDesktop:

(> mv docker services/docker)
> docker -v
> docker info
> ...
> sudo ln -s ~$USER/.rd/docker.sock /var/run/docker.sock