A List of Useful Docker Commands

Quick Tip

Here a list of useful Docker commands collected when working with containers.


  • Create (run) containers
docker run --name contenedor --detach image_name

detach option asks Docker to run this container in the background.

docker run --name contenedor -d --publish porthost:portcontainer image_name
docker run -d -p 7706:3306 --name contenedordb image_name

publish option exposes container port to a host port for remote access.

  • List running containers
docker ps
docker container ls
  • List running and stopped containers
docker ps -a
docker container ls -a
  • Handling containers
docker start contenedor
docker restart contenedor
docker stop contenedor
docker kill contenedor
  • Start/stop multiple containers
docker start contenedor1 contenedor2
docker stop contenedor0 contenedor1 contenedor2
  • Delete containers
docker container rm contenedor
Access Containers
  • Accessing container as root
docker exec -it contenedor bash
  • Accessing container as normal user
docker exec -it --user pato contenedordb bash
Running Commands from Outside the Container
  • Running installed program
docker exec -it patomaxscale maxctrl list servers
  • Running sql query
docker exec -it patomariadb mysql -e "select * from taller.tg1"
Copy Files From/To Container
  • Copy from container to host directory
docker cp patomaster:/home/pato/masterfull.sql .
  • Copy from host to container directory
docker cp masterfull.sql patoslave:/home/pato/masterfull.sql
Container Info
  • Check container log
docker logs patomaxgalera | tail

This output is from the main service/program registered in the CMD parameter of the image used by the container.

  • Check container port mapping:
docker port contenedor
  • Search and pull image from Docker Hub
docker search mariadb
docker pull mariadb
  • List images
docker images
docker image ls
  • Create an image from a current container
docker commit contenedor
docker tag image_id patomx/imagen
docker commit contenedor patomx/imagen
  • Upload own image to Docker Hub
docker login
docker push patomx/imagen
  • Build an image using Dockerfile
vi Dockerfile
FROM operating_system (ubuntu)
ENV environment_variables
RUN apt install -y package 
RUN wget https://... && \
    dpkg -i 
COPY host_file container_file
EXPOSE ports
CMD service ... start & tail -F /var/log/__.log
docker build --tag patomx/patomaxscale .
  • Delete images
docker image rm imagen
  • Create volumes
pato@patocontainer ~ $ docker volume create --name volnew

Volumes can also be created when creating a container with the command
docker run ... -v newvolume:path

  • List volumes
docker volume ls
  • Use local filesystem as volume
docker run -d --name contenedorl -v /home/pato/data:/var/lib/mysql patomx/imagen
docker run -d --name contenedor2 -v /home/pato/config:/etc/mysql -v patovolgm:/var/lib/mysql patomx/imagen
  • Copy volume content to another volume
docker run --rm -i -t -v patovol1:/origen -v patovol2:/destino alpine sh -c "cp -avr /origen/* /destino"
  • Using a container as a shared data volume
docker run --name contenedor2 --volumes-from contenedor1 imagen
  • Delete volumes
docker volume rm volumen1
  • Remove all volumes not used by a container
docker volume prune
  • Create network (type bridge)
docker network create --driver bridge pato-net
  • List networks
docker network ls
  • Delete networks
docker network rm pato-net
Global Docker Commands

Show detailed information on space usage

docker system df -v

Remove all unused containers, networks, images

docker system prune
Google Cloud Container-Optimized OS

When using virtual instance as a Docker host for deploying containers I use to add swap area to the Container-Optimized OS

  • Create swap for the first time
sudo sysctl vm.disk_based_swap=1
sudo fallocate -l 512M /var/swapfile
sudo chmod 600 /var/swapfile
sudo mkswap /var/swapfile
sudo swapon /var/swapfile
  • Recreate swap for rebuild host instances
sudo sysctl vm.disk_based_swap=1
sudo swapon /var/swapfile