Workflow
# First time setup: docker-compose build # Builds your custom image from Dockerfile docker-compose up -d # Starts containers using your custom image # If you change the Dockerfile later: docker-compose build # Rebuild the image docker-compose up -d # Recreate containers with new image # Or do both at once: docker-compose up -d --build
File Operations
- copy files to docker container:
docker cp foo.txt container_id:/foo.txt - copy files from docker container:
docker cp container_id:/foot.txt foo.txt - prune unneeded files (save disk space):
sudo docker system prune -a -f
Docker Maintenance
docker system df: show overall space usage by categorydocker inspect $(docker ps -q) | grep -A 10 Mounts: see what volume goes with what containerdocker inspect CONTAINER_NAME --format '{{range .Mounts}}{{.Name}} -> {{.Destination}}{{println}}{{end}}': list what volume is associated with the containerdocker ps: list docker containers
Housekeeping
- When removing a service use
docker compose down -vinstead ofdocker compose down.- The
-vremoves the associated volumes at the same time.
- When updating images, after pulling and restarting, run
docker image pruneto remove the dangling images docker ps -a: see all containers, even the ones that are stopped
Watchtower
- Watchtower is a container which auto updates Docker containers
- It will only update containers with updates that have the same tag
- to always get the latest version use the tag
latest - you can check which images have which tags with
docker images- some images might have
latestbut not be base images so they won't be updated
- check the logs:
docker logs watchtower - if you want Watchtower to skip trying to update an image add to
docker-compose.yml
services:
db:
# label the image as each image gets its own label
image: mariadb:latest # an example
labels:
- "com.centurylinklabs.watchtower.enable=false" # ADD THIS LINE
- run watchtower immediately:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --run-once
Basic Operations
docker run: run container- use the
-dflag for “detach” i.e. run in background - use the
-pflag to forward the port i.e.-p 9000:80maps port 80 in the container to 9000 on the host - user the
--nameflag to name the container (otherwise it's auto generated)
docker stop <ID>docker logs <ID>docker start <ID>: restart a containerdocker build <DIR_WHERE_Dockerfile_IS_LOCATED>: build the image-t: add name and tag. Ex:docker build -t node-app:1.0 .
Docker Registry
- Docker Hub is the default registry and it is the biggest public registry
- you can store a private or public registry on Docker Hub
- Each application gets its on repo in the registry
creating a Dockerfile
- All dockerfiles start with a parent image or “base image”
- define the base image with
FROM - copy files into the container:
COPY- example:
COPY package.json /app/
- set the working directory:
WORKDIR- example:
WORKDIR /app
- next come the dependencies:
RUN <dependencies here>- example:
RUN npm install
- for the last command in the docker file i.e. start the process:
CMD- example:
CMD ["node", "server.js"]