Installation
Linux
Install script provided by Docker:
curl -sSL https://get.docker.com/ | shOr see Installation instructions for your Linux distribution.
Create Docker VM with Docker Machine
You can use Docker Machine to:
Install and run Docker on Mac or Windows
Provision and manage multiple remote Docker hosts
Provision Swarm clusters
A simple example to create a local Docker VM with VirtualBox:
docker-machine create --driver=virtualbox defaultdocker-machine lseval "$(docker-machine env default)"
Then start up a container:
docker run alpine echo "hello-world"That's it, you have a running Docker container.
Container lifecycle
Create a container:
docker create imageName.Create and start a container in one operation:
docker run imageNameRemove the container after it stops
--rm:docker run --rm alpine ls /usr/libAttach the container stdin/stdout to the current terminal use
-it:docker run -it ubuntu bashTo mount a directory on the host to a container add
-voption, e.g.docker run -v $HOSTDIR:$DOCKERDIR imageNameTo map a container port use
-p $HOSTPORT:$CONTAINERPORT:docker run -p 8080:80 nginxTo run the container in background use
-dswitch:docker run -d -p 8080:80 nginxSet container name:
docker run --name myContainerName imageNameExample to run nginx web server to serve files from html directory on port 8080:
docker run -d -v $(pwd)/html:/usr/share/nginx/html -p 8080:80 --name myNginx nginx# access the webservercurl http://localhost:8080
In some cases containers need extended privileges. Add privileges with the
--cap_addswitch, e.g.docker run --cap-add SYS_ADMIN imageName. The flag--cap_dropis used to remove privileges.Rename a container:
docker rename name newNameDelete a container:
docker rm containerIDDelete all unused containers:
docker ps -q -a | xargs docker rmRemove the volumes associated with a container:
docker rm -v containerName
Update container resource limits:
docker update --cpu-shares 512 -m 300MList running containers:
docker psList all containers:
docker ps -aList all container IDs:
docker ps -q -a
Starting and stopping containers
Start a container:
docker start containerName*docker stopRestart a container:
docker restart containerNamePause a container ("freeze"):
docker pause containerNameUnpause a container:
docker unpause containerNameStop and wait for termination:
docker wait containerNameKill a container (sends SIGKILL):
docker kill containerName
Executing commands in containers and apply changes
Execute a command in a container:
docker exec -it containerName commandCopy files or folders between a container and the local filesystem:
docker cp containerName:path localFileordocker cp localPath containerName:pathApply changes in container file systems:
docker commit containerName
Logging and monitoring
Logging on Docker could be challenging - check Top 10 Docker Logging Gotchas.
Show container logs:
docker logs containerNameShow only new logs:
docker logs -f containerNameShow CPU and memory usage:
docker statsShow CPU and memory usage for specific containers:
docker stats containerName1 containerName2Show running processes in a container:
docker top containerNameShow Docker events:
docker eventsShow storage usage:
docker system dfTo run a container with a custom logging driver (i.e., to syslog), use:
docker run -–log-driver syslog –-log-opt syslog-address=udp://syslog-server:514 \alpine echo hello world
Use Logagent for log collection and Sematext Agent for metrics and event monitoring. Create Docker Monitoring App & Logs App in Sematext Cloud to get required tokens and use them instead the placeholders
YourContainerToken,YourInfraTokenandYourLogsTokenin commands below. Start collecting all container metrics, host metrics and Docker events:
docker run -d --restart always --privileged -P --name st-agent \-v /sys/kernel/debug:/sys/kernel/debug \-v /var/run/:/var/run/ \-v /proc:/host/proc:ro \-v /etc:/host/etc:ro \-v /sys:/host/sys:ro \-v /usr/lib:/host/usr/lib:ro \-e CONTAINER_TOKEN=YourContainerToken \-e INFRA_TOKEN=YourInfraToken \-e JOURNAL_DIR=/var/run/st-agent \-e LOGGING_WRITE_EVENTS=false \-e LOGGING_REQUEST_TRACKING=false \-e LOGGING_LEVEL=info \-e NODE_NAME=`hostname` \-e CONTAINER_SKIP_BY_IMAGE=sematext \sematext/agent:latest
Collect all container logs:
docker run -d --name st-logagent \-e LOGS_TOKEN=YourLogsToken \-v /var/run/docker.sock:/var/run/docker.sock \sematext/logagent
The commands above will collect all container metrics, host metrics, Docker events, and container logs.
Exploring Docker information
Show Docker info:
docker infoList all containers:
docker ps -aList all images:
docker image lsShow all container details:
docker inspect containerNameShow changes in the container's files:
docker diff containerName
Manage Docker images
List all images:
docker imagesSearch in registry for an image:
docker search searchTermPull image from a registry:
docker pull imageNamepulls an image from registry to local machineCreate image from Dockerfile:
docker buildRemove image:
docker rmi imageNameExport container into tgz file:
docker export myContainerName -o myContainerNameCreate an image from a tgz file:
docker import file
Docker networks
List existing networks:
docker network lsCreate a network:
docker network create netNameRemove network:
docker network rm netNameShow network details:
docker network inspectConnect container to a network:
docker network connect networkName containerNameDisconnect network from container:
docker network disconnect networkName containerName
Data cleanup
Data management commands:
Remove unused resources:
docker system pruneRemove unused volumes:
docker volume pruneRemove unused networks:
docker network pruneRemove unused containers:
docker container pruneRemove unused images:
docker image prune
Comments
Post a Comment