Docker Tutorial for Beginner

June 1, 2022

Docker Base Command

# pull a image from docker hub or another private registry
docker pull redis:4.0

# -d: detached mode
# -p: binding host and docker container port
# --name: setting the name of container
# run an image, it will create a container as the environment
docker run -d -p 6379:6379 --name redis40 redis:4.0

# -a: show all container process info(including the container ID, name, etc.), even if the container already exited
docker ps -a

# go to container inner with an interactive terminal
docker exec -it <containerID> /bin/bash

# get container logs
# -f: follow mode
docker logs -f <containerID>

# start / stop / restart a comtainer with containerID or name
docker start / stop / restart <containerID | container name>

# remove container
# -v also remove the volumn of container
docker rm <containerID>

# show images info
docker images

# remove images
docker image rm <imageID>sh


# create a volume
docker volume create <volume name>

# ls: show all volumes
# -f: equals to --filter
# -f "dangling=true" filter all volumes that are not used by at least one container
docker volume ls

# inspect a volume
docker volume inspect <volumeID or name>

# start a container with volume, you can also use in docker run command
docker run -d --name nginx -v myvol:/app nginx

# remove a volume
docker volumn rm <volume ID or name>

# remove all volumes which are not binding to container (can binding more than one container)
docker volumn prunesh
view details: docker volume


# show all network
docker network ls

# there are three default network (1. bridge 2. host 3. none)
# by default, docker container will use the bridge network

# to inspect network status
docker network inspect bridge

# to find the container ip address, you can use
# in windows, change the grep to findstr
docker inpect <containerID> | grep -i "ipaddress"
# or
docker network inspect <the network your container in>

# create network
# driver : 1. bridge 2. host 3. none
docker network create --driver bridge <network name>

# now you can run some image with --network to specify the network, etc.
docker run -d --name redis --network <network name> -p 6379:6379 redis

# delete network
docker network rm <network name>sh
view details: docker network

Docker Compose

For running mutiple docker containers.
docker-compose -f <config file path> <up or down> -d
# you also can use compose in docker CLI like below if your docker version is recently released
docker compose -f <config file path> <up or down> -dsh
Normally, you also need a configuration file to tell docker-compose which action you want to do.
Note the configuration file type is yaml.
For example.
version: "2.6.1"
    image: "redis"
    container_name: "redis"
      - "6379:6379"
      - redis:/data
    image: "mysql"
    container_name: "mysql"
      - "3306:3306"
      - mysql:/var/lib/mysql
      MYSQL_ROOT_PASSWORD: password


# volumes:
#   mysql:
#     external: true
#   redis:
#     external:
#       name: ""
#   postgre:
#     driver: localyaml
docker compose will create a network with bridge driver by default, and all services(containers) will running in the same network.
view details: docker compose

Docker Build Own Image & Push to Repository

Build from Dockerfile

dockerfile example
FROM nginx

COPY ./index.html /usr/share/nginx/htmlplaintext
then docker build -t alightyoung/mynginx:1.0 .
use docker image ls to show image.
and test with docker run -d -p 8080:80 alightyoung/mynginx:1.0
To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web.
# build
docker build -t <hub-user>/<repo-name>[:<tag>]
# re-tagging
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
# commit changes
docker commit <existing-container> <hub-user>/<repo-name>[:<tag>]sh
view details: docker build

Push to Docker Hub Repository

First, you need to register an account of docker hub and create a repository.
note the repo name must be the same as the name of the image you have just built.
then use docker login to verify your account.
by using docker push alightyoung/mynginx:1.0 to push local image to docker hub.
if your image is public then anyone can pull your image using docker pull alightyoung/mynginx:1.0
view details:

Build new image from existed image

  • run existed image by docker run -it --rm <image_id>
  • copy or exec in container by docker cp /local/data containerId:/root/data/ or docker exec -it <container_id> bash
  • commit a new image from the container by docker commit <container_id> imagename:tagname
You can not exit the container started by docker run before commit successful.
The End