- docker ps — Lists running containers. Some useful flags include:
-allfor all containers (default shows just running) and
-qto list just their ids (useful for when you want to get all the containers).
- docker pull — Most of your images will be created on top of a base image from the Docker Hub registry. Docker Hub contains many pre-built images that you can
pulland try without needing to define and configure your own. To download a particular image, or set of images (i.e., a repository), use
- docker build — The
docker buildcommand builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified
URL. Use the
-tflag to label the image, for example
docker build -t my_container .with the
.at the end signalling to build using the currently directory.
- docker run — Run a docker container based on an image, you can follow this on with other commands, such as
-it bashto then run bash from within the container. Also see Top 10 options for docker run — a quick reference guide for the CLI command.
docker run my_image -it bash
- docker logs — Use this command to display the logs of a container, you must specify a container and can use flags, such as
--followto follow the output in the logs of using the program.
docker logs --follow my_container
- docker volume ls — This lists the volumes, which are the preferred mechanism for persisting data generated by and used by Docker containers.
- docker rm — Removes one or more containers.
docker rm my_container
- docker rmi — Removes one or more images.
docker rmi my_image
- docker stop — Stops one or more containers.
docker stop my_containerstops one container, while
docker stop $(docker ps -a -q)stops all running containers. A more direct way is to use
docker kill my_container, which does not attempt to shut down the process gracefully first.
- Use them together, for example to clean up all your docker images and containers:
- kill all running containers with
docker kill $(docker ps -q)
- delete all stopped containers with
docker rm $(docker ps -a -q)
- delete all images with
docker rmi $(docker images -q)
Create new container
Start a new docker image with a given name
You can start a new container by using the run command and specify the desired image
$ docker run -it --name playground ubuntu:17.10 /bin/bash .... root@c106fbb48b20:/# exit
As a result, you are in the container at the bash command line
Reconnect to image
$ docker attach playground
Commit changes in container
$ docker start playground $ docker attach playground root@c106fbb48b20:/# echo 1.0 >VERSION root@c106fbb48b20:/# exit $ docker commit playground playground:1.0 $ docker tag playground:1.0 playground:latest $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE playground 1.0 01703597322b Less than a second ago 94.6MB playground latest 01703597322b Less than a second ago 94.6MB
Add tools and utilities
$ apt-get update $ apt-get upgrade $ apt-get install python3 python3-pip
$ apt-get install default-jre
Links and Resources
Docker quick reference guides
- Run bash or any command in a Docker container
- Top 10 options for docker run — a quick reference guide for the CLI command
- Docker’s detached mode for beginners: How to run containers in the background of your terminal
Docker in more depth
- Clean out your Docker images, containers and volumes with single commands
- How I filter and grep Docker containers, images, and volumes, and how you can too
- Expose vs publish: Docker port commands explained simply
- Docker run vs exec: deep-dive into their differences
- How to use Entrypoint with Docker and Docker Compose