Docker quick start


Useful docker commands

 1docker run (-d) - Run a command in a new container 
 2docker attach - Attach to a running container
 3docker start - Start one or more stopped containers
 4docker stop - Stop one or more running containers
 5docker ps -a - List containers
 6docker images - List images
 7docker inspect - Return low-level information on Docker objects
 8docker top - Display the running processes of a container
 9docker stats - Display a live stream of container(s) resource usage statistics
10docker logs - Fetch the logs of a container
11docker pull - Pull an image or a repository from a registry

Containers Management:

 1docker image ls             
 2docker image pull nginx         
 3docker image inspect 08393e824c3
 4docker container run busybox
 5docker container ls -a
 6docker container run -P -d nginx
 7docker container port 55590ea1f3b4
 8docker container ps
 9docker container inspect 030f0f31e94b
10docker container top 030f0f31e94b
11docker container attach 030f0f31e94b
12docker container exec -it 030f0f31e94b /bin/bash
13docker container exec -it 030f0f31e94b ls /usr/share/nginx/
14docker container rm -f 030f0f31e94b
16docker container run -d nginx
17docker container run -it nginx /bin/bash
18docker container exec -it ad8b407b51de ls /usr/share/nginx/
20docker stop $(docker ps -a -q)
21docker rm $(docker ps -a -q)
22docker container run -d --expose 3000 -p 80:3000 nginx
23docker container run -d --expose 3000 -p 8080:80 nginx
24docker container port 0b45e49dd5f3
25docker container run -d -p 8081:80/tcp -p 8081:80/udp nginx

Networking Commands

 1docker network ls
 2docker network inspect bridge
 3docker network create br0
 4docker network rm br0
 5docker container run -d --name network-test -p 8081:80 nginx
 6docker network connect br1 network-test
 7docker network disconnect
 8docker network create -d bridge --internal localhost
 9docker network connect <NETWORK> <CONTAINER> # adding contaner to a network
11docker network create --subnet --gateway br2
12docker network create --subnet --gateway --ip-range= --driver=bridge --label=host4network br4
13docker container run -d --name network-test02 --ip --network br4 nginx


Volume Commands


 1docker volume create      Create a volume
 2docker volume inspect     Display detailed information on one or more volumes
 3docker volume ls          List volumes
 4docker volume prune       Remove all unused local volumes
 5docker volume rm          Remove one or more volumes
 7docker volume create test-volume1
 8docker volume inspect test-volume1
 9docker volume rm test-volume1
10docker volume prune !!!

Bind Mounts:

1docker container run -d --name nginx-bind-mount1 --mount type=bind,source=/root/dir1,target=/app nginx

Volumes for Persistent Storage:

1docker volume create volume3
2docker container run -d --name nginx-volume3 --mount type=volume,source=volume3,target=/usr/share/nginx/html/ nginx
3docker volume inspect volume3 
4sudo ls /var/snap/docker/common/var-lib-docker/volumes/volume3/_data 
6docker container run -d --name nginx-volume3 --mount type=volume,source=volume3,target=/usr/share/nginx/html,readonly nginx  #readonly

Dockerfile & Building Images

Best practices for writing Dockerfiles

  • keep containers as ephemeral as possible
  • follow principle 6 of the 12 factor app
  • avoid including unnecessary file
  • use .dockerignore
  • use multi-storage builds
  • decouple applications
  • minimize the number of layers
  • sort multi-line arguments
  • leverage the build cache

More info:

  • FROM creates a layer from the ubuntu:18.04 Docker image.
  • COPY adds files from your Docker client’s current directory.
  • RUN builds your application with make.
  • CMD specifies what command to run within the container.


1# syntax=docker/dockerfile:1
2FROM ubuntu:18.04
3COPY . /app
4RUN make /app
5CMD python /app/
1# syntax=docker/dockerfile:1
2FROM node:12-alpine
3RUN apk add --no-cache python g++ make
5COPY . .
6RUN yarn install --production
7CMD ["node", "src/index.js"]
1docker image build -t 'testapp:v1' .

Using Environment Variables:

 1 # Create an image
 2 FROM node
 3 LABEL org.label-schema.version=v1.1
 5 ENV NODE_ENV="development"
 6 ENV PORT 3000
 8 RUN mkdir -p /var/node
 9 ADD src/ var/node/
10 WORKDIR /var/note
11 RUN npm install
13 CMD .bin/www
1docker container run -d --name appName-dev -p 8082:3001 --env PORT=3001 test/web-app:v2
2docker container run -d --name appName-dev -p 8082:3001 --env PORT=3001 --env NODE_ENV=prod test/web-app:v2

Some others commands:


1docker image history node:latest 

Having Containers Start Automatically

1docker container run -d --name container_10.2 --restart always centos /bin/bash

Docker Events

1docker system events
2docker system events --filter type=container --since '1h' 

Managing Stopped Container

1docker container ls -a -q -f status=exited