Docker is a computer program that performs operating-system-level virtualization, also known as “containerization”.
1. Publish nginx server container with a single command
docker container run --publish 80:80 nginx
Executing the above command does the following:
- Downloads image nginx; from docker hub
- Starts a new container from that image
- Opens port 80 on the host IP
- Routes that traffic to the container IP, port 80
2. Running a container in the background:
docker container run --publish 80:80 --detach nginx
Executing the above command runs nginx server in the background, detach option tells Docker to run the container in the background.
3. List all running containers
docker container ls
The above command lists all the containers in docker and their status.
4. Stop a container
docker container stop {containerID}
The containerID can be obtained using ls list all containers command. The container ID can be few digits, just enough for it to be unique.
5. Run new container with specified name
docker container run --publish 80:80 --detach --name linuxhost nginx
A new container with nginx web server image is started with the name linuxhost.
6. View container logs
docker container logs {containerName}
The above command displays logs for the container name specified. The container name can be obtained using the ls list all containers command.
7. View processes running within a container
docker container top {containerName}
This command lists all the processes running within the container by container name. The container name can be obtained using the ls list all containers command.
8. List all commands using help command
docker container --help
This command lists all of the commands we can perform on a container.
9. List all containers
docker container ls -a
This command lists all the containers with any status within docker.
10. Remove containers by container IDs
docker container rm {containerID1} {containerID2} ...
This command removes all non-running container ID’s specified seprated by space.
11. Force remove running container
docker container rm -f {containerID}
The -f option to rm command allows removing of a running container in docker.
12. Remove all containers with a wildcard alternative
docker stop $(docker ps -a -q)
docker rm $ (docker ps -a -q)
This command stops all containers first and then removes all of them. On Windows 10 use powershell instead of command prompt and the above command will work.