Let's Learn docker commands:
Create and run a container in the background
$ 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 containerdocker/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 namenginx
- The image to use
General commands
| List running containers |
| List all containers |
| List running containers |
| List all images |
| Connecting to container |
| Shows container's console log |
| Stop a container |
| Restart a container |
| Remove a container |
| Shows container's port mapping |
| List processes |
| Kill a container |
Parameter <container>
can be container id or name
#Docker Containers
Starting & Stopping
| Starting |
| Stopping |
| Restarting |
| Pausing |
| Unpausing |
| Blocking a Container |
| Sending a SIGKILL |
| Connecting to an Existing Container |
Information
| List running containers |
| List all containers |
| Container Logs |
| Inspecting Containers |
| Containers Events |
| Public Ports |
| Running Processes |
| Container Resource Usage |
| Lists the changes made to a container. |
Creating
docker create [options] IMAGE
-a, --attach # attach stdout/err
-i, --interactive # attach stdin (interactive)
-t, --tty # pseudo-tty
--name NAME # name your image
-p, --publish 5000:5000 # port map (host:container)
--expose 5432 # expose a port to containers
-P, --publish-all # publish all ports
--link container:alias # linking
-v, --volume `pwd`:/app # mount (absolute paths needed)
-e, --env NAME=hello # env vars
Example
$ docker create --name my_redis --expose 6379 redis:3.0.2
Manipulating
Renaming a Container
docker rename my-nginx my-nginx
Removing a Container
docker rm my-nginx
Updating a Container
docker update --cpu-shares 512 -m 300M my-nginx
#Docker Images
Manipulating
| Listing images |
| Removing an image |
| Loading a tarred repository |
| Loading a tarred repository |
| Save an image to a tar archive |
| Showing the history of an image |
| Save a container as an image. |
| Tagging an image |
| 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
Removing a network
docker network rm MyOverlayNetwork
Listing networks
docker network ls
Getting information about a network
docker network inspect MyOverlayNetwork
Connecting a running container to a network
docker network connect MyOverlayNetwork nginx
Connecting a container to a network when it starts
docker run -it -d --network=MyOverlayNetwork nginx
Disconnecting a container from a network
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
Cleans up dangling images, containers, volumes, and networks (ie, not associated with a container)
docker system prune
Additionally, remove any stopped containers and all unused images (not just dangling images)
docker system prune -a
Containers
Stop all running containers
docker stop $(docker ps -a -q)
Delete stopped containers
docker container prune
Images
Remove all dangling (not tagged and is not associated with a container) images:
docker image prune
Remove all images which are not used by existing containers
docker image prune -a
Volumes
docker volume prune
Remove all volumes not used by at least one container
#Miscellaneous
Docker Hub
| Search docker hub for images. |
| Downloads an image from docker hub. |
| Authenticate to docker hub |
| Uploads an image to docker hub. |
Registry commands
Login to a Registry
$ docker login
$ docker login localhost:8080
Logout from a Registry
$ docker logout
$ docker logout localhost:8080
Searching an Image
$ docker search nginx
$ docker search nginx --stars=3 --no-trunc busybox
Pulling an Image
$ docker pull nginx
$ docker pull eon01/nginx localhost:5000/myadmin/nginx
Pushing an Image
$ docker push eon01/nginx
$ docker push eon01/nginx localhost:5000/myadmin/nginx
Batch clean
| Stopping all containers |
| Removing all containers |
| Removing all images |
Volumes
Check volumes
$ docker volume ls
Cleanup unused volumes
$ docker volume prune
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future
I would like to connect with you at https://medium.com/@madhuripatiletc48