Docker Commands Quick Start
Docker Quickstart#
Build a dockerfile:
docker build -t <name> .
List the available images:
docker images
Run an image:
docker run <image_name>:<tag>
If you want to run it in the background use -d
docker run elastalert:latest -d
If you want to publish a containers ports to the host use -p:
docker run <image>:<version> -d -p <port on host>:<port on container> -p <port on host>:<port on container> --net="host"
Eg.
docker run elastalert:latest -d -p 3030:3030 -p 3333:3333 --net="host"
If you want to bind a volume use -v:
docker run elastalert:latest -d -p 3030:3030 -v $HOME/myContainer/configDir:/myImage/configDir
Use --name to assign a name to the container
More info on the Docker run reference
View all running and non-running containers:
docker ps -a
Remove an image:
docker rm <name>
Get the reason why a Docker container exited:
docker logs <container-id>
Inspect the container:
docker inspect <container-id>
Ensure a container is removed when it exits or stops, use --rm:
docker run --rm -d --network host --name my_nginx nginx
Key Concepts#
You got Images and Containers, and you need to know what state you are at:
- Does the Docker image exist?
- Does the Docker container exist?
- Is the Docker container running?
If we are talking containers your command will look like:
docker container <command>
These are container commands:
create- Create a container from an imagestart- Start an existing containerrun- Create a new container and start itls- List running containersinspect- See lots of info about a containerlogs- Print logsstop- Gracefully stop running containerkill- Stop main process in container abruptlyrm- Delete a stopped container
If we are dealing with images:
docker image <command>
These are image commands:
build- Build an imagepush- Push an image to a remote registryls- List imageshistory- See intermediate image infoinspect- See lots of info about an image, including the layersrm- Delete an image
Misc commands:
docker version- List info about your Docker Client and Server versionsdocker login- Log in to a Docker registrydocker system prune- Delete all unused containers, unused networks, and dangling images
Container#
Create a container from an image
docker container create my_repo/my_image:my_tag
You can use -a for attach (for STDIN, STDOUT or STDERR)
docker container create -a STDIN my_image
Start an existing container
docker container start my_container
A container can be refered to by its id or name
Run combines both create and start:
docker container run my_image
Some options:
-ior--interactive- Keep STDIN open even if unattached-tor--tty- Allocates a pseudo terminal that connects your terminal with the container’s STDIN and STDOUT-por--port- The port is the interface with the outside world.1000:8000maps the Docker port8000to port1000on your machine / host. Eg. go tolocalhost:1000on your host--rm- automatically remove container when it stops running-dor--detach- Run the container in the background as a daemon
List all containers and sizes:
docker container ls -a -s
Inspect a container:
docker container inspect my_container
Print a containers log:
docker container logs my_container
Stop a running container:
docker container stop my_container
Stop a running container immediately (abruptly):
docker container kill my_container
Kill all running containers:
docker container kill $(docker ps -q)
Delete one or more containers:
docker container rm my_container
Image#
Build a Docker image from a dockerfile located at a specific path:
docker image build -t my_repo/my_image:my_tag .
Pushing an image to a registry
docker image push my_repo/my_image:my_tag
List images and sizes
docker image ls
View the history of an image:
docker image history my_image
Show details and layers of your image:
docker image inspect my_image
Remove an image
docker image rm my_image
Admin#
Get Docker system info
docker system info
Delete unused containers, unused networks and dangling images
docker system prune
Show Docker disk usage
docker system df