Docker basic commands

Conainer commands

Commands to run a container:

docker container ls -a
docker container rm 123 456 # you only need first 2-3 characters for identification
docker container run --publish 8080:80 --name ng nginx
docker run -d -p 3801:3306 --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD='yes' mysql:5.7

Check information related to a container:

docker container top nginx
docker container inspect nginx
docker container stats nginx

Run a bash shell in a container:

docker container exec -it mysql bash # interactive, tty
docker container run -it -p 8080:80 --name nginx nginx

Container Network

  • Network is an interface that connects internally to container, and externally to ethernet.
  • "Bridge" is the default driver for docker network.
  • One container can connect to multiple networks
  • One port in a network can only be occupied by one container
docker network create my
docker container run -d -p 8080:80 --network my --name nginx nginx
docker network connect my mysql
docker network inspect my --format "{{.Containers}}"
docker network disconnect my mysql
  • Don't use ip to talk between containers
  • Docker engine has built-in DNS resolution, so a connection to a container name will be resolved to connect to that container
docker container run -d --name nginx2 --network my nginx:alpine
docker container exec -it nginx2 ping nginx # should be successful

Noted that basic os image usually does not run any application, which results into exiting immediately when running an container, we need to run interactive mode to actually access the shell of the os.

Also, --rm flag will remove the container after it stops.

docker container run --rm -it --name centos centos:7 bash

--net-alias adds a DNS id to the network so whenever other containers try to connect to the name in that network, it randomly picks among all containers connect to that name. For example, use the following commands to run 2 elasticsearch containers using the same DNS:

docker container run -d --name e1 --network my elasticsearch:2
docker container run -d --name e2 --network my --net-alias e1 elasticsearch:2
docker container run -it --name centos --network my centos:7 bash 

In the centos container, do "curl -s e1:9200", which will return different results on different requests.


A docker image is an application or binary, not a complete os like VM. All images are pushed to by default.

We can use history command to view the history of an image.

docker image history nginx:latest

Docker image is composed of layers like git history, including Dockerfile change & file system change so that when pulling, if an image layer already exists, you don't download that layer again.

File changes in a container is only a "copy on write" that action on the top of the image, but not chaning the image itself.

We can use inspect command to check the info of an image.

docker image inspect nginx:latest # See useful info like Cmd, Env, and ExposedPorts

Image Tagging

Use the following commands to tag an existing image to your own, and push to docker hub.

docker image tag nginx adlerhsieh/nginx:default
docker image push adlerhsieh/nginx:default

Docker Hub

docker login will save a config at: ~/.docker/config.json

docker login
cat .docker/config.json


Volume is a file system lives outside a container. Bind mounts is to link a container path to a host path.

docker volume ls
docker volume prune # clean volumes

The following mounts a volume to a container:

# add a volume to a container. Format: name:path
docker container run -d --name mysql2 -p 3334:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true -v my:/var/lib/mysql mysql:5.7

The following use the current path as the volume path:

docker container run -d -p 8080:80 --name nginx -v $(pwd):/usr/share/nginx/html nginx
  • file created in the container will not show in the host, but
  • file created on the host will show in container
  • file deleted on both sides will be removed on the other side as well

Docker Compose

docker-compose up -d
docker-compose top
docker-compose ps
docker-compose down
docker-compose down --rmi all
docker-compose down -v
docker-compose up --build