Docker Tutorial for Beginner
June 1, 2022
Docker Base Command
docker pull redis:4.0
docker run -d -p 6379:6379 --name redis40 redis:4.0
docker ps -a
docker exec -it <containerID> /bin/bash
docker logs -f <containerID>
docker start / stop / restart <containerID | container name>
docker rm <containerID>
docker images
docker image rm <imageID>
sh
Volume
docker volume create <volume name>
docker volume ls
docker volume inspect <volumeID or name>
docker run -d --name nginx -v myvol:/app nginx
docker volumn rm <volume ID or name>
docker volumn prune
sh
Network
docker network ls
docker network inspect bridge
docker inpect <containerID> | grep -i "ipaddress"
docker network inspect <the network your container in>
docker network create --driver bridge <network name>
docker run -d --name redis --network <network name> -p 6379:6379 redis
docker network rm <network name>
sh
Docker Compose
For running mutiple docker containers.
docker-compose -f <config file path> <up or down> -d
docker compose -f <config file path> <up or down> -d
sh
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:
yaml
TIP
docker compose will create a network with bridge driver by default, and all services(containers) will running in the same network.
Docker Build Own Image & Push to Repository
Build from Dockerfile
dockerfile example
FROM nginx
COPY ./index.html /usr/share/nginx/html
plaintext
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
WARNING
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.
docker build -t <hub-user>/<repo-name>[:<tag>]
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
docker commit <existing-container> <hub-user>/<repo-name>[:<tag>]
sh
Push to Docker Hub Repository
First, you need to register an account of docker hub and create a repository.
WARNING
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
WARNING
You can not exit the container started by docker run before commit successful.