Docker commands
Docker is a containerization platform that allows SDETs to create consistent, isolated test environments. It enables fast setup of test dependencies ( services, browsers), ensures reproducible test runs, simplifies CI/CD pipelines, and improves scalability of automated testing.
Getting Started
Create and run a container in background
docker run -d -p 80:80 docker/getting-started-d: Detached mode-p 80:80: Map port 80 on host to port 80 in containerdocker/getting-started: Image to use
Create and run a container in foreground
docker run -it -p 8001:8080 --name my-nginx nginx-it: Interactive mode with terminal-p 8001:8080: Map port 8001 on host to 8080 in container--name my-nginx: Name the containernginx: Image to use
General Commands
List running containers
List all containers
List running containers (with CPU/memory)
List all images
Connect to container
Show container logs
Stop a container
Restart a container
Remove a container
Show port mappings
List running processes
Kill a container
<container> can be container ID or name.
Docker Containers
Starting & Stopping
Start:
docker start my-nginxStop:
docker stop my-nginxRestart:
docker restart my-nginxPause:
docker pause my-nginxUnpause:
docker unpause my-nginxKill:
docker kill my-nginxAttach:
docker attach my-nginxWait (blocking):
docker wait my-nginx
Information
List running:
docker psList all:
docker ps -aLogs:
docker logs my-nginxInspect:
docker inspect my-nginxEvents:
docker events my-nginxPublic Ports:
docker port my-nginxRunning processes:
docker top my-nginxStats (resources):
docker stats my-nginxChanges:
docker diff my-nginx
Creating
-a, --attach: Attach stdout/err-i, --interactive: Attach stdin-t, --tty: Pseudo-tty--name NAME: Name the container-p, --publish 5000:5000: Port mapping--expose 5432: Expose a port-P, --publish-all: Publish all ports--link container:alias: Link containers-v, --volume $(pwd):/app: Mount a volume-e, --env NAME=hello: Set environment variables
Example:
Manipulating
Rename:
docker rename old-name new-nameRemove:
docker rm my-nginxUpdate:
docker update --cpu-shares 512 -m 300M my-nginx
Docker containers are lightweight, isolated environments that run applications and their dependencies. They share the host OS kernel but have their own filesystem, making them fast to start and efficient in resource usage. Containers provide consistency across development, testing, and production environments, simplifying deployment and scalability.
Docker Images
Manipulating
List images:
docker imagesRemove image:
docker rmi nginxLoad image:
docker load < ubuntu.tar.gzordocker load --input ubuntu.tarSave image:
docker save busybox > ubuntu.tarShow history:
docker history nginxCommit container:
docker commit nginxTag image:
docker tag nginx user/nginxPush image:
docker push user/nginx
Building Images
Current directory:
docker build .From GitHub:
docker build github.com/creack/docker-firefoxFrom Dockerfile via stdin:
docker build - < DockerfileFrom tar context:
docker build - < context.tar.gzTag build:
docker build -t user/nginx .Custom Dockerfile:
docker build -f MyDockerfile .From remote:
curl example.com/Dockerfile | docker build -f - .
Docker images are read-only templates used to create Docker containers. They contain the application code, libraries, dependencies, and runtime environment needed for the application to run. Images can be pulled from Docker Hub or built locally from a Dockerfile. Once created, images are versioned, shared, and reused to ensure consistency across environments.
Docker Networking
Manipulating Networks
List networks:
docker network lsInspect network:
docker network inspect MyOverlayNetworkRemove network:
docker network rm MyOverlayNetworkConnect running container:
docker network connect MyOverlayNetwork nginxConnect at start:
docker run -it -d --network=MyOverlayNetwork nginxDisconnect:
docker network disconnect MyOverlayNetwork nginx
Docker Networking allows containers to communicate with each other and with the outside world. It provides various network types such as bridge, host, and overlay, each suited for different use cases. Networking features include connecting containers, exposing ports, managing network security, and handling communication between containers in a multi-host setup.
Docker Cleanup
Clean All
Remove unused containers, volumes, networks:
docker system pruneRemove all unused images too:
docker system prune -a
Containers
Stop all containers:
docker stop $(docker ps -a -q)Delete stopped containers:
docker container prune
Images
Remove dangling images:
docker image pruneRemove all unused images:
docker image prune -a
Volumes
List volumes:
docker volume lsPrune unused volumes:
docker volume prune
Docker volumes are used to persist data generated by and used by Docker containers. Unlike container filesystems, volumes are stored outside of containers, ensuring data remains intact even when containers are stopped or deleted. Volumes can be shared between containers, making them ideal for storing databases, configuration files, and logs.
Miscellaneous
Docker Hub
Search images:
docker search nginxPull image:
docker pull user/imageLogin:
docker loginPush image:
docker push user/image
Docker Hub is a cloud-based registry where users can store, share, and manage Docker images. It provides access to both public and private repositories, enabling easy distribution of containerized applications.
Batch Cleanup
Stop all containers:
docker stop -f $(docker ps -a -q)Remove all containers:
docker rm -f $(docker ps -a -q)Remove all images:
docker rmi -f $(docker images -q)
Last updated