Docker
Getting Started
Getting started
$ docker run -d -p 80:80 docker/getting-started
-d – Run the container in detached mode
-p 80:80 – Map port 80 to port 80 in the container
docker/getting-started – The image to use
Create and run a container in foreground
$ docker run -it -p 8001:8080 --name my-nginx nginx
-it – Interactive bash mode
-p 8001:8080 – Map port 8001 to port 8080 in the container
--name my-nginx – Specify a name
nginx – The image to use
General commands
| Example |
Description |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker ps -s |
List running containers (with CPU / memory) |
docker images |
List all images |
docker exec -it <container> bash |
Connecting to container |
docker logs <container> |
Shows container’s console log |
docker stop <container> |
Stop a container |
docker restart <container> |
Restart a container |
docker rm <container> |
Remove a container |
docker port <container> |
Shows container’s port mapping |
docker top <container> |
List processes |
docker kill <container> |
Kill a container |
Docker Containers
Starting & Stopping
| Description |
Example |
| Starting |
docker start my-nginx |
| Stopping |
docker stop my-nginx |
| Restarting |
docker restart my-nginx |
| Pausing |
docker pause my-nginx |
| Unpausing |
docker unpause my-nginx |
| Blocking a Container |
docker wait my-nginx |
| Sending a SIGKILL |
docker kill my-nginx |
| Connecting to an Existing Container |
docker attach my-nginx |
Information
| Example |
Description |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker logs my-nginx |
Container Logs |
docker inspect my-nginx |
Inspecting Containers |
docker events my-nginx |
Containers Events |
docker port my-nginx |
Public Ports |
docker top my-nginx |
Running Processes |
docker stats my-nginx |
Container Resource Usage |
docker diff my-nginx |
Lists the changes made to a container. |
Docker Images
Manipulating
| Example |
Description |
docker images |
Listing images |
docker rmi nginx |
Removing an image |
docker load < ubuntu.tar.gz |
Loading a tarred repository |
docker load --input ubuntu.tar |
Loading a tarred repository |
docker save busybox > ubuntu.tar |
Save an image to a tar archive |
docker history |
Showing the history of an image |
docker commit nginx |
Save a container as an image. |
docker tag nginx eon01/nginx |
Tagging an image |
docker push eon01/nginx |
Pushing an image |
Building Images
$ docker build .
$ docker build github.com/creack/docker-firefox
$ docker build - < Dockerfile
$ docker build - < context.tar.gz
$ docker build -t eon/my-nginx .
$ docker build -f myOtherDockerfile .
$ curl example.com/remote/Dockerfile | docker build -f - .
Docker Networking
Manipulating
docker network rm MyOverlayNetwork
docker network ls
docker network inspect MyOverlayNetwork
docker network connect MyOverlayNetwork nginx
docker run -it -d --network=MyOverlayNetwork nginx
docker network disconnect MyOverlayNetwork nginx
Creating Networks
docker network create -d overlay MyOverlayNetwork
docker network create -d bridge MyBridgeNetwork
docker network create -d overlay \
--subnet=192.168.0.0/16 \
--subnet=192.170.0.0/16 \
--gateway=192.168.0.100 \
--gateway=192.170.0.100 \
--ip-range=192.168.1.0/24 \
--aux-address="my-router=192.168.1.5" \
--aux-address="my-switch=192.168.1.6" \
--aux-address="my-printer=192.170.1.5" \
--aux-address="my-nas=192.170.1.6" \
MyOverlayNetwork
Clean Up
Clean All
docker system prune
Additionally, remove any stopped containers and all unused images (not just dangling images)
docker system prune -a
Containers
docker stop $(docker ps -a -q)
docker container prune
Images
docker image prune
docker image prune -a
Volumes
docker volume prune
Miscellaneous
### Docker Hub
| Docker Hub |
Description |
docker search search_word |
Search docker hub for images. |
docker pull user/image |
Downloads an image from docker hub |
docker login |
Authenticate to docker hub |
docker push user/image |
Uploads an image to docker hub. |
Registry commands
docker login
docker logout
docker search nginx
docker pull nginx
docker push eon01/nginx
Batch clean
| Example |
Description |
docker stop -f $(docker ps -a -q) |
Stopping all containers |
docker rm -f $(docker ps -a -q) |
Removing all containers |
docker rmi -f $(docker images -q) |
Removing all images |
Volumes
docker volume ls
docker volume prune