Docker and Docker-Compose Advanced
Prakash Pun - December 22, 2022
• 12 min read
Prakash Pun - December 22, 2022
• 12 min read
docker ps -l --format=$FORMAT # List the most recent container with custom format
docker tag docker_id image_name # Name containers for better organization
docker commit test test1 # Create a new image from container changes
docker run --rm -ti ubuntu sleep 5 ## Remove after exit (terminal interactive)
docker run -ti ubuntu bash -c "sleep 3; echo all done" # Chain commands
docker run -d -ti ubuntu bash # d -> detach (run in background)
docker attachdocker psdocker attach container_namedocker execdocker exec -ti container_name bashdocker logsdocker logs container_namedocker run --name example -d ubuntu bash -c "lose /etc/password"docker kill container_namedocker rm container_namedocker ps -ldocker run --memory maximum-allowed-memory image-name commanddocker run --cpu-shares (relative to other containers)docker run --cpu-quota (absolute limits)docker run --rm -ti -p 45678:45678 -p 45679:45679 --name echo-server ubuntu:14.04 bash
# Inside container:
nc -lp 45678 | nc -lp 45679
# On host:
nc localhost 45678
nc localhost 45679
# From another container:
docker run --rm -ti ubuntu:14.04
nc host.docker.internal 45678 # Use host.docker.internal or actual IP on Windows
docker run --rm -ti -p 45678 -p 45679 --name echo-server ubuntu:14.04 bash
docker port echo-server # Shows port mappings
/udp suffixdocker run -p 1234:1234/udp # UDP port mapping
docker run --rm -ti -p 45678/udp --name echo-server ubuntu:14.04 bash
nc -ulp 45678 # UDP listener inside container
docker port echo-server # Check port mappings
docker run --rm -ti ubuntu:14.04 bash
nc -u host.docker.internal 34545 # Connect to UDP port
docker network lsdocker network create learningdocker run --rm -ti --net learning --name catserver ubuntu:14.04 bash
# Inside container:
ping catserver # DNS resolution works automatically
docker run --rm -ti --net learning --name dogserver ubuntu:14.04 bash
nc -lp 1234 # Start a listener
# On catserver:
nc dogserver 1234 # Connect to dogserver
docker network create catsonly
docker network connect catsonly catserver
docker run --rm -ti --net catsonly --name bobcatserver ubuntu:14.04 bash
docker run --rm -ti -e SECRET=theinternetlovescat --name catserver ubuntu:14.04 bash
docker run --rm -ti --link catserver --name dogserver ubuntu:14.04 bash
# Check:
nc -lp 4321
nc dogserver 4321
nc catserver 4321
env # View shared environment variables
docker commit automatically tags imagesdocker commit container_id name_you_want_to_giveregistry.example.com:port/organization/image-name:version-tag
organization/image-name:tag is often sufficientdocker pull - Download images (automatic with docker run)docker push - Upload images to registriesdocker rmi image-name:tag
docker rmi image-id
docker run -ti -v D:\\Projects\\example:/shared-folder ubuntu bash
volumes-from to share container volumesdocker run -ti -v /shared-data ubuntu bash
docker run -ti --volumes-from nervous_galois ubuntu bash
docker search image-namedocker pull debian:siddocker tag debian:sid prakashpun7/test-image:v99.9docker push prakashpun7/test-image:v99.9docker build -t name-of-result . # '.' is the build context path
# FROM debian:sid
# RUN apt-get -y update
# RUN apt-get -y install nano
# CMD ["bin/nano", "/tmp/notes"]
FROM example/nanoer
ADD notes.txt /notes.txt
CMD ["bin/nano", "/notes.txt"]
FROM java:8MAINTAINER Firstname Lastname <[email protected]>RUN unzip install.zip /opt/install/
RUN echo Hello
ADD run.sh /run.sh
ADD project.tar.gz /install/
ADD https://project.example.com/download/1.0/project.rpm /project/
ENV DB_HOST=db.production.example.com
ENV DB_PORT=5432
nano notes.txt["/bin/nano", "notes.txt"]EXPOSE 8080VOLUME ["/host/path/", "/container/path/"]
VOLUME ["/shared-data"]
WORKDIR /install/USER arthur
USER 100
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -y install curl
RUN curl https://google.com | wc -c > google-size
ENTRYPOINT echo google is this big; cat google-size
FROM ubuntu:16.04 as builder
RUN apt-get update
RUN apt-get -y install curl
RUN curl https://google.com | wc -c > google-size
FROM alpine
COPY /google-size /google-size
ENTRYPOINT echo google is this big; cat google-size
docker run -ti --rm --net=host ubuntu:16.04 bash
apt-get update && apt-get install bridge-utils
brctl show
docker network create my-network
docker run --net=host options image-name commanddocker run -ti --rm --net=host --privileged=true ubuntu bash
apt-get update && apt-get install iptables
iptables -n -L -t nat
docker inspect --format '{{.State.Pid}}' container-name
kill 7538 # Where 7538 is the process ID
mount -o bind other-work workdocker save -o my-images.tar.gz debian:sid busybox ubuntu:14.04
docker rmi debian:sid busybox ubuntu:14.04
docker load -i my-images.tar.gz
docker savedocker loadkubectlFROM node:12
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=4000
EXPOSE 4000
CMD ["npm", "start"]
Build and run:
docker build -t prakashpun/demoapp:1.0 .
docker run -p 5000:4000 container_id
Dockerfile => Blueprint for building docker images
Image => Template for running docker containers
Container => Running process with isolated environment