Skip to content

Instantly share code, notes, and snippets.

@spdrman
Last active October 12, 2023 23:23
Show Gist options
  • Save spdrman/239892e8ff2418a995a04e89c1e553a9 to your computer and use it in GitHub Desktop.
Save spdrman/239892e8ff2418a995a04e89c1e553a9 to your computer and use it in GitHub Desktop.
Docker Workflow
# -------------------------------------------------------------------------------------------------------------------------
# DOCKER
#
# https://docs.docker.com/
#
# AWS ECR (Elastic Container Registry) used to store different versions of the same image (tags)
# image is like a template, and a container is what uses the template to start an environment you can interact with
# -------------------------------------------------------------------------------------------------------------------------
docker pull "image_name:version" # default is to pull from dockerhub
docker images # list of images "prune" arg after images will delete all unused
docker run -d "image_name:version" # startup an image_name:version (or can use its hash)
docker ps -a # list all containers
docker stats # list all RUNNING containers and their status
docker network ls # list all networks and their status
docker logs "container_name" # show logs for container_name (or can use its hash)
docker exec -it "container_name" /bin/bash # open bash shell of container image (sometimes it's /bin/sh
docker stop "container_name" # stops a container with container_name (or can use its hash)
docker rm -f "container_name" # delete container with container_name (or can use its hash)
docker rmi "image_name:version" # delete image (all containers using must be stopped & deleted first)
docker build . # build an image based on the Dockerfile in the current dir
docker tag "image_name" "image_name:version" # take existing built image and tag for push to repo
# use AWS docker login to login first
# then use as image_name:version -> ...amazon.com/image_name:version
docker build -t "image_name:version" . # build & tag an image based on the Dockerfile in the current dir
# use ...amazon.com/image_name:version for AWS ECR publishing
docker push "image_name:version" # use ...amazon.com/image_name:version for AWS ECR publishing
docker system prune -a # Clear docker system & cache of all images/containers/volumes
# -------------------------------------------------------------------------------------------------------------------------
# DOCKER-COMPOSE
#
# https://docs.docker.com/compose/compose-file/compose-file-v3/
#
# -------------------------------------------------------------------------------------------------------------------------
docker-compose -f "docker-compose.yml" up # builds & starts container based on the docker-compose.yaml config
# add -d at end to force into background on start
docker-compose -f "docker-compose.yml" down # stops container of images based on the docker-compose.yaml config
# -------------------------------------------------------------------------------------------------------------------------
# VOLUMES
#
# https://docs.docker.com/storage/volumes/
#
# -------------------------------------------------------------------------------------------------------------------------
docker volume create \ # creates host stored volume named "volume_name"
--driver local \ # default is local, so can skip it, otherwise check
#https://docs.docker.com/storage/storagedriver/select-storage-driver/
--name "volume_name" \ # used in docker-compose.yaml as named volume
--label "volume_label" \ # optional -- like alias to override OS designated volume IDs
--opt "" # optional -- linux volume type, device, size, etc...
docker volume ls # list all volumes
docker volume inspect "volume_name" | "container_name" # show volume config, including location of data on host
docker volume rm "volume_name" # delete volume
# -------------------------------------------------------------------------------------------------------------------------
# LINKS
# -------------------------------------------------------------------------------------------------------------------------
Node Live Reload: https://www.freecodecamp.org/news/how-to-enable-live-reload-on-docker-based-applications/
# Instructions to build a CONTAINER
# more info at https://docs.docker.com/compose/compose-file/compose-file-v3/
# and at https://docs.docker.com/engine/reference/run/
version: '2'
services:
webapp_db:
container_name: "webapp_db:1.0" # use ...amazon.com/image_name:version for AWS ECR published images
image: mysql:9.0.1
networks:
- webapp_network
ports:
- "3336:3306" # host:container
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=demodb
volumes:
- db_data:
# data in db_data on host will be replicated in container at each run
webapp:
container_name: "webapp:1.0" # use ...amazon.com/image_name:version for AWS ECR published images
build:
- context: /path/to/DockerfileDirectory
- dockerfile: DockerfileFileName
networks:
- webapp_network
ports:
- "8080:80" # host:container
volumes:
- /path/from/host:/path/to/container #host volume -- great for dev live-reload of changes to code
- container_n: /path/to/docker/namedvol #named volume -- best for production
- /path/to/docker/anonvol #anonymous volume -- recommend not using this type
environment:
- bootstrap.memory_lock=true
depends_on:
- webapp_db #service name, not container name
volumes:
db_data: /var/lib/mysql #volume_name: docker container file path
#driver: local #this tells docker to store the files in a dir on the host
#external: true #this tells docker that the volume was created via "docker volume create"
# more info at https://docs.docker.com/compose/compose-file/compose-file-v3/#volume-configuration-reference
# Instructions to build an IMAGE
# https://docs.docker.com/engine/reference/builder/
FROM python:3.7
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."
ENV bootstrap.memory_lock=true \
alias ls='ls -al'
WORKDIR /home/app # mkdir -p /home/app & cd /home/app
COPY requirements.txt ./ # or "COPY . ." <- copy everything from current dir to WORKDIR
RUN pip install -r requirements.txt
EXPOSE 8050 # Port to use as default for image
CMD [“python”, “index.py”] # Command, Args to run when image starts up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment