Docker Basic Commands
Bài viết này mình chia sẻ các command docker căn bản (Docker Basic commands).
1) docker
Example:
docker [option] [command] [arguments]
2) docker version
Example:
docker version
3) docker info
Example
docker info
4) docker pull
Example:
docker pull ubuntu
5) docker build
Example:
docker build <options> <directory path> OR <URL>
docker build .
6) docker run
Example:
docker run -i -t ubuntu /bin/bash
-i – Để chạy một container.
-t – Chạy và xuát stdout tty.
ubuntu–Docker image được dùng để tạo container.
bash (or /bin/bash)– Lệnh để chạy một Ubuntu container.
Note- Nếu muốn chạy ở chế độ máy host thêm tham số -d. Để thoát khỏi container dùng tổ hợp CTRL + P + Q.
Chạy Docker Container trong background.
docker run -i -t --name=Ubuntu-Linux -d ubuntu /bin/bash
7) docker commit
Example:
docker commit [options] <container-id> [REPOSITORY[:TAG]]
Commit 1 container đang chạy
docker commit 023828e786e0 ubuntu-apache
8) docker ps
Examples:
docker ps
Liệt kê Docker Containers bao gồm các container đang dừng
docker ps -a
9) docker start
docker start <container-id>
10) docker stop
docker stop <container-id>
11) docker logs
$ docker logs <Container ID>
12) docker rename
docker rename <Old_Name> <New_Name>
13) docker rm
docker rm <CONTAINER ID>
Xoá tất cả các container đã dừng
sudo docker rm -f $(sudo docker ps -a -q)
Xoá tất cả images ko gắn tag
sudo docker images | grep none | awk '{ print $3; }' | xargs sudo docker rmi
#1. Docker Image Commands
Docker Image được đóng gói sẵn theo ứng dụng để chạy một Docker container.
1) docker build – To build Docker Image from Dockerfile
Example:
docker build <options> <directory path> OR <URL>
docker build .
Thêm tag cho một Docker Image
docker build -t vietcalls/nodejs:1.0
2. docker pull – To pull Docker Image from Docker Hub Registry
docker pull [OPTIONS] Image_Name[:TAG]
Examples:
docker pull ubuntu
Docker pull Image from Private Registry
Login UserName and Password
docker login hub.docker.com --username=USERNAME
Pull the Docker Image
docker pull hub.docker.com/nodejs
Gán tag khi pull
docker pull "repoName"/"image_name"[:tag]
Pull docker image from private IP based repository
docker image pull 192.168.100.50:5000 /ubuntu:latest
3. docker tag – To add Tag to Docker Image
docker tag IMAGE ID image/TAG
Examples:
docker tag nodejsdocker vietcalls/nodejsdocker:v1.0
4. docker images – To list Docker Images
Docker command to list images
Examples
docker images
docker image ls
docker images list
To filter Docker Images list
docker images --filter "<key>=<value>"
Dùng “–filter”:
- dangling– Images are not used
- label– List the Docker Images those you added a label
- before– List the Docker Images which is created in specific time
- since– Created in specific time with another image creation
- reference – List Docker Images which has name or Tag
docker images -a
5. docker push – To push Docker Images to repository
docker push [OPTIONS] NAME[:TAG]
Examples:
docker tag nodejs my_docker_registry.com/nodejs:v1.0
To push a Docker Image to Private Registry
docker login hub.docker.com --username=USERNAME
docker tag ubuntu hub.docker.com/linux/ubuntu:latest
6. docker history – To show history of Docker Image
docker image history [OPTIONS] IMAGE
Examples:
docker history <image-id> --no-trunc
Get full history in tabular format:
docker history <image-id> --format "table{{.ID}}, {{.CreatedBy}}" --no-trunc
7. docker inspect – To show complete information in JSON format
docker inspect IMAGE_ID OR CONTAINER_ID
8. docker save – To save an existing Docker Image
docker save ubuntu_image:tag | gzip > ubuntu_image.tar.gz
9. docker import – Create Docker Image from Tarball
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Examples:
docker import ./ubuntu_image.tar.gz ubuntu:latest
Tạo “ubuntu:latest” images từ file nén
Import một Docker container
cat docker_container.tar.gz | docker import - my_image:tag
10. docker export – To export existing Docker container
docker export container_id | gzip > new_container.tar.gz
11. docker load – To load Docker Image from file or archives
docker load < ubuntu_image.tar.gz
12. docker rmi– To remove docker images
docker rmi IMAGE_ID
To remove all Docker Images
docker rmi $(docker images -q)
To remove All Docker Images forcefully
docker rmi -f $(docker images -q)
To clean docker images, builds , ..etc
docker system prune
We have covered Docker Basic commands for Docker Image.
#2. Docker Container Commands
1) docker start – To start a Docker container
docker start [OPTIONS] CONTAINER [CONTAINER]
Examples:
docker start container_id
Nếu muốn xuất ra màn hình
docker start -ai container_id
2) docker stop – To stop a running docker container
docker stop [-t|--time[=10]] CONTAINER [CONTAINER]
[-t|–time[=10]– wait before stopping the container
Examples:
docker stop container_id
To stop all the containers
docker stop 'docker ps -q'
To stop all Docker containers
docker stop $(docker ps -a -q)
3) docker restart – To restart docker container
docker restart container_id
4) docker pause – To pause a running container
docker pause container_id
Docker pause vs stop ?
Docker pause suspends all processes in the defined container .
Docker stop sends SIGTERM to the container’s main process to stop and stops the container.
5) docker unpause – To unpause a running container
Syntax:
docker unpause CONTAINER [CONTAINER…]
Example:
docker unpause CONTAINER_ID
6) docker run – Creates a docker container from docker image
docker run [OPTIONS] IMAGE [COMMAND] [ARG]
docker container run command is used to create a docker container from docker images. Below are the example of docker run container with commands
To run Docker container in foreground
docker run ubuntu
you will see output of ubuntu docker container on your terminal, To stop the container type “CTRL + C”.
To run Docker container in detached mode/in background OR you want to keep the docker container running when terminal exit , use option “-d“
docker container run -d ubuntu
To run Docker container under specific name
Syntax:
docker container run --name [CONTAINER_NAME] [DOCKER_IMAGE]
Example:
docker run -i -t --name=Ubuntu-Linux -d ubuntu
To run Docker container in interactive mode/ you can enter commands inside docker container while it is runnning
docker run -i -t --name=Ubuntu-Linux -d ubuntu /bin/bash
Expose Docker Container ports and access Apache outside
docker run -p 81:80 -itd 4e5021d210f6
-p – Exposes 80 ra ngoài
Vào trình duyệt apache với port 81
http://localhost_ip:81/
7) docker ps – To list Docker containers
docker ps
docker ps -a
8) docker exec – To Access the shell of Docker Container
docker exec -i -t 023828e786e0 /bin/bash
docker exec -i -t Ubuntu-Linux /bin/bash
Để update the System Packages từ Docker Container
docker exec 023828e786e0 apt-get update
Cài Apache2 từ docker container
docker exec 023828e786e0 apt-get install apache2 -y
Kiểm tra apache2 service status inside Docker Container
docker exec 023828e786e0 service apache2 status
Start Apache2 service inside Docker Container
docker exec 023828e786e0 service apache2 start
9) docker logs – To view logs of Docker container
To view Logs for a Docker Container
docker logs <Containe ID>
10) docker rename – To rename Docker container
To rename Docker Container
docker rename <Old_Name> <New_Name>
11) docker rm – To remove Docker container
To remove the Docker Container, stop it first and then remove it
docker rm <CONTAINER ID>
Run below command to remove all stopped containers
sudo docker rm -f $(sudo docker ps -a -q)
To remove untagged docker images
sudo docker images | grep none | awk '{ print $3; }' | xargs sudo docker rmi
12) docker inspect – Docker container info command
Syntax:
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
OR
docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker inspect 023828e786e0
To get Docker container IP Address
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $DOCKER_CONTAINER_NAME
To get list of all ports binds to Docker container
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' $DOCKER_INSTANCE_NAME
12) docker attach – Attach Terminal to Running container
Docker attach command is used to attach your terminal to running container to control Input/Output/Error operations.
Syntax:
docker attach [OPTIONS] CONTAINER_ID / CONTAINER_NAME
Example:
docker attach nodejs
12) docker kill – To stop and remove Docker containers
Syntax:
docker kill [OPTIONS] CONTAINER [CONTAINER…]
Example:
To stop all docker containers
docker kill $(docker ps -q)
To remove all docker containers
docker rm $(docker ps -a -q)
To remove all docker containers forcefully
docker rm -f $(docker ps -a -q)
13) docker cp – To copy files or folders between a container and from local filesystem.
Syntax:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Examples:
copy directory from Docker host to container
sudo docker cp ./directory_path 023828e786e0:/home/ubuntu
copy directory from docker container to host
sudo docker cp 023828e786e0:/etc/apache2/sites-enabled .
To copy files from Docker container to host
sudo docker cp 023828e786e0:/etc/apache2 .
To copy files from Host to Docker container
Syntax:
docker cp SOURCE_HOST_PATH CONTAINER:DESTINATION_PATH
Example:
sudo docker cp ./test.vietcalls.com.conf 023828e786e0:/etc/apache2/sites-enabled
#3. Docker Compose Commands
1) docker-compose build – To build docker compose file
Example:
docker-compose build
2) docker-compose up – To run docker compose file
docker-compose up
To run docker compose in background
docker-compose up -d
To re-run containers which has stopped in states
docker-compose up --no-recreate
3) docker-compose ls – To list docker images declared inside docker compose file
docker-compose ps
4) docker-compose start – To start containers which are already created using docker compose file
docker-compose start
docker-compose up – It creates new docker containers which are defined docker-compose file.
docker-compose start– used to only to restart docker containers which are already created using docker-compose file, never created new containers.
5) docker-compose run – To run one one of application inside docker-compose.yml
docker-compose run nodejs
6) docker-compose rm – To remove docker containers from docker compose
docker-compose rm -f
To auto remove docker containers with docker-compose.yml
docker-compose up && docker-compose rm -fsv
To stop docker containers and then remove
docker-compose stop && docker-compose rm -f
To stop a specific docker container from docker compose
docker-compose stop nodejs
To remove docker containers data inside docker compose
docker-compose rm -f nodejs
To remove volume which is attached to docker container
docker-compose rm -v
7) docker-compose ps – To check docker container status from docker compose
docker-compose ps
We have covered docker basic commands for Docker Compose.
#4. Docker Volume Commands
1) docker volume create – To create docker volume
docker volume create <volume_name>
2) docker volume inspect – To inspect docker volume
docker volume create <volume_name>
3) docker volume rm – To remove docker volume
First remove the docker container
docker rm –f $(docker ps -aq)
then remove the docker volume
docker volume rm <volume_name>
To delete all docker volumes at once
docker volume prune
We have covered Docker Basic Commands for Docker Volume.
#5. Docker Networking Commands
1) docker network create – To create docker network
docker network create --driver=bridge --subnet=192.168.100.0/24 br0
2) docker network ls – To list docker networls
docker network ls
3) docker network inspect – To view network configuration details
docker network inspect bridge
We have covered Docker Basic Commands for Docker Networking.
#6. Docker Logs and Monitoring Commands
1) docker ps -a – To show running and stopped containers
docker ps -a
2) docker logs – To show Docker container logs
docker logs
3) docker events – To get all events of docker container
docker events
4) docker top – To show running process in docker container
docker top
5) docker stats – To check cpu, memory and network I/O usage
docker stats <container_id>
6) docker port – To show docker containers public ports
docker port <container_id>
We have covered Docker Basic Commands for Docker Logs and Monitoring.
#7. Docker Prune Commands
Dùng Docker prune chúng ta xoá docker không dùng hoặc treo bao gồm containers, Images , volumes and networks
Làm sạch hệ thống với docker containers
docker system prune
Làm sạch bao gồm các container đã dừng
docker system prune -a
To remove Dangling Docker images
docker image prune
docker image prune -a
To remove all unused docker containers
docker container prune
To remove all unused docker volumes
docker volume prune
To remove all unused docker networks
docker network prune
#8. Docker Hub Commands
To search docker image
docker search ubuntu
To pull image from docker hub
docker pull ubuntu
Pushing Docker Image to Docker Hub Repository
Login to https://hub.docker.com with ID and password using command line
docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: vietcalls Password: Login Succeeded
Now push Docker Image to Docker Hub Repository
docker push nodejsdocker
Error: denied: requested access to the resource is denied:docker
Nếu báo lỗi ta thêm tag
docker tag nodejsdocker vietcalls/nodejsdocker
Push lại Docker Image
docker push vietcalls/nodejsdocker
To logout from Docker Hub Registry
docker logout
Lời kết
Qua bài viết mình chía sẻ các bạn một số command cơ bản strong docker và docker-compose. Chúc các bạn thành công.!