Image vs container
A container is an running instance of an image.
Just like how in OOPs an object is an instance of a class. The class defines the blueprint. The object is an instance of the class.
You can have may containers running on the same image.
Docker’s default image registry is called Docker hub (hub.docker.com).
Managing containers
Starting/running container
docker container run --publish 1234:80 nginxThe publish part has the syntax : It routes the traffic from host IP to container IP.
run starts a new container.
But the above command starts it in foreground. To start a container in background, use the --detach option.
The -d flag detaches the container to run in the background and returns you the shell prompt.
docker container run --publish 1234:80 --detach nginxStopped/inactive containers
If the container already exists but is stopped (inactive), run:
docker start <container_name_or_id>To see all containers (including stopped ones):
docker ps -aRestart a container:
docker restart <container_name_or_id>Listing containers
To see running containers:
docker container lsTo see active + inactive containers:
docker container ls -lThe container id, name are unique. Id is created, name can be provided. If not it’s created automatically from an FOSS list of scientists & hackers.
Know more about a running container
Seeing logs
To see a one time snapshot of log (not running/streaming of logs):
docker container logs <name_of_container>Seeing processes
Display the running processes of a container
Aliases: docker container top, docker top
docker container top <container name>Nginx has a master process that spawns worker processes.
Stop running containers
docker container stop <1st 3 letters of container id>Delete containers
To delete inactive container:
docker container rm <id>You can as well specify multiple ids.
To delete active container:
docker container rm -f <id>BTS of docker run command
What happens behind the scenes (BTS) of docker container run command:
- Looks for the image in the image cache
- If not found, it downloads from the remote repository (docker hub - default registry)
- Downloads the latest version (if no version defined)
- Creates new container based on the image
- Assigns a virtual IP to container - on a private network inside docker engine
- Opens port 1234 on host & forwards it to port 80 in container.
- Start container using the
CMDcommand specified in the image Dockerfile.
You can specify all of these:
docker container run --publish 1234:80 --name hayeee -d nginx:1.11 nginx -T1.11 - image version
nginx -T - CMD run on start
Misconceptions
- Containers a re not mini VMs. They are processes with restricted resource.
Windows containers
Seeing the challenges my consulting teams have in supporting Windows Containers over the years, I still can’t recommend Windows Containers in most cases. The .NET teams I know are moving their codebase to .NET 5 on Linux, and adopting Linux containers.
Quick question
Q: I ran ‘docker container run -p 80:80 nginx’ and my command line is gone and everything looks frozen. Why?
A: Because you didn’t specify the -d flag to detach it in the background, and there aren’t any Nginx logs yet.
Everything is normal. You didn’t specify the -d flag to detach it in the background, and there aren’t any logs coming across the screen because you haven’t connected to the published port yet, so Nginx has nothing to log about.
Container performance
docker container stats - overview of all container processes
docker container inspect <id> - metadata about a container
docker container top <id> - processes inside container
Getting a shell inside containers
There is no need of SSH! Useful for troubleshooting.
Two Docker commands:
- Start a new container interatively
- Run additional command in existing container
Two options:
-i- interactive (i flag keeps the container’s STDIN open)-t- TTY, allocate a psudo TTY
Note: docker container exec runs additional process on existing container. For example it’s not going to affect root process of your mysql daemon.
Quick question.
Q: docker run container alpine bash will give error. Why?
A: bash isn’t included in alpine :) But it surely has sh which you can use.
