01. Basic Usuage - Container
Load image remotely
docker pull ubuntu
Check all the containers
docker ps -a
Container Operating
Start Container
docker run -it ubuntu /bin/bash
parameters
- -i: interactive mode
- -t: using the terminal
- ubuntu: using ubuntu image
- /bin/bash: command for the image, here we use bash for the terminal shell
Exit from the terminal
exit
Rename a container
docker rename <old_name_or_id> <new_name>
Start a stopped container
docker start <container_id/name>
Run container in background
docker run -itd --name ubuntu-test ubuntu /bin/bash
Stop a container
docker stop <container_id/name>
Restart a container
docker restart <container_id/name>
Enter a running container
docker attach <container_id/name>
: This approach will lead to stopping after exit the container.
docker exec -it <container_id/name> /bin/bash
: This approach will not cause stopping of the container
Export container
docker export 1e560fca3906 > ubuntu.tar
Import container snapshot
cat ubuntu.tart
| docker import - test/ubuntu:v1
Delete container
docker rm -f <container_id/name>
Port Mapping
Same mapping
docker run -d -P training/webapp python app.py
docker run -d -p 8080:8080 training/webapp python app.py
Parameters
- -d: run the container in background
- -P: map random port of container to the host
- -p: map a specified ports between container and host
Specified Mapping Port
docker run -d -p 5000:5000 training/webapp python app.py
View all the mapping port
docker port <container_id/container_name>
Container Monitoring
Standard I/O
docker logs -f <container_id/container_name>
Check Processes
docker top <container_id/container_name>
Volume(Load Local Path)
docker run -p 9999:9999 -it -v {host path}:{container path} ubuntu
docker run -p 9999:9999 -it -v D:\workspace\12.docker:/data ubuntu
Add privilege
Add read only limitationdocker run -p 9999:9999 -it --previleged -v D:\workspace\12.docker:/data:ro ubuntu
Read and Writedocker run --previleged -it -v D:\workspace\12.docker:/data:rw ubuntu
Info
- --previleged=true
Docker privileged mode grants a Docker container root capabilities to all devices on the host system.
Inherit Volume
docker run -it --previleged=true --volumes-from {parent_container} --name {new_container} ubuntu
Share Folder With Host
Common Commands
Stop all containers docker stop $(docker ps -q)
Delete all containersdocker rm $(docker ps -aq)
Delete all imagesdocker rmi -f $(docker images -q)