Docker Tutorial for Beginner

6/1/2022 Docker

# 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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# Volume


# 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 prune

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

view details: docker volume (opens new window)

# Network


# 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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

view details: docker network (opens new window)

# 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> -d
1
2
3

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"
services: 
  redis: 
    image: "redis"
    container_name: "redis"
    ports:
      - "6379:6379"
    volumes:
      - redis:/data
  mysql:
    image: "mysql"
    container_name: "mysql"
    ports:
      - "3306:3306"
    volumes:
      - mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password

volumes:
  mysql:
  redis:

# volumes:
#   mysql:
#     external: true
#   redis:
#     external:
#       name: ""
#   postgre:
#     driver: local
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Tips

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 (opens new window)

# Docker Build Own Image & Push to Repository

# Build from Dockerfile

dockerfile example

FROM nginx

COPY ./index.html /usr/share/nginx/html
1
2
3

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>]
1
2
3
4
5
6

view details: docker build (opens new window)

# 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: