Skip to content

Instantly share code, notes, and snippets.

@stephenleo
Last active March 19, 2022 04:46
Show Gist options
  • Save stephenleo/ce21f75bc0a57c730da5ec4ef6e85302 to your computer and use it in GitHub Desktop.
Save stephenleo/ce21f75bc0a57c730da5ec4ef6e85302 to your computer and use it in GitHub Desktop.
Docker & Kubernetes
  1. Create and Run a container: docker run -p <localhost port>:<docker container port> <image-name>
  2. If you dont want console output: docker run -d <image-name>
  3. List Running containers: docker ps
  4. List all containers that have run: docker ps --all
  5. Restart a stopped container: docker start <container-id>
  6. Delete stopped containers and downloaded images: docker system prune
  7. Stop a container: docker stop <container-id>
  8. Execute additional commands in a running container (other than the starup command): docker exec -it <container-id> <command>
    Eg: docker exec -it <container-id> sh
  9. Exit from a container: Ctrl + d or exit
  10. Build a docker image from a Dockerfile in current directory: docker build -t <docker-id/project:version> .
  11. While running a custom built docker image, we can drop the version to run the latest version: docker run <docker-id/project>
  12. Dockerfile commands:
    FROM
    WORKDIR
    COPY
    RUN
    CMD
    EXPOSE # To inform user/ CICD the port to expose
    
  13. Build using a dockerfile with custom name Dockerfile.dev: docker build -f Dockerfile.dev .
  14. Mapping current working directory to a directory in the container: docker run -v $(pwd):/<directory in container> <image-name>
  15. Mapping pwd without mapping an existing directory in the container: docker run -v <existing directory in container> -v $(pwd):/<directory in container> <image-name>
  16. To prune all images, containers, networks and volumes that are not being used: docker system prune --volumes
  1. Scripting DockerCLI commands and Starting up multiple containers with networking
  2. Run the image defined in docker-compose.yml: docker-compose up
  3. Rebuild the images defined in docker-compose.yml: docker-compose up --build
  4. Run docker compose in the background: docker-compose up -d
  5. Stop all containers started up by docker compose: docker-compose down
  6. Docker will automatically restart stopped or crashed containers if we add a restart policy to that service: restart: <'no'/always(eg, for web apps)/on-failure(when exit code !=0, eg, for schedule jobs)/unless-stopped>
  7. Docker compose ps (only from same working directory as docker-compose.yml): docker-compose ps
  1. Apply a config to kubernetes cluster: kubectl apply -f <config_file.yaml>
  2. Get running pods: kubectl get pods
  3. Describe any k8s object: kubectl describe <object-type> <object-name>
  4. Use kind: Deployment instead of kind: Pod. Deployment is in apiVersion: apps/v1
  5. Get running deployments: kubectl get deployments
  6. Delete any k8s object: kubectl delete -f <config_file.yaml>
  7. Updating a deployment to pull latest image: kubectl rollout restart -f <config_file.yaml>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment