Container
Understand how to perform container operations.
All changes that are made to containers or networks are logged under the Docker System Events.
Create container
When we create a new container, Docker will create a new directory /var/lib/docker/containers/<id>.json
and all the container logs will be stored under that file by default.
After the container is created, you have to start the container.
List container details
Start container
You can get container id from docker container ls
.
Run a container
run container = create container + start container
Some options:
-it
= interactive terminal (creates and enter the terminal session with the command you specify. This means you can execute commands inside the container while it is still running. Useful for debugging purposes.)-d
= detach (Run container in background)--name
= container name--rm
= remove container after the process is done--hostname
= setup container hostname--user
- setup container username-p
= port mapping--env
or-e
or--env-file
= setup environment variables--health-cmd
and--health-interval
= check container state--privileged
= Login as root user, the Privileged container has full access to all the host's devices and files--restart
= Restart policyRestart Options Description no The container will never be restarted on-failure When the container fails, it will restart the container always The container will always be restarted unless-stopped It's very similar to "always" option, but the container will not restart when it was manually stopped and not even when the Docker Daemon restarts
Expose container port (Capital P)
Normally it will auto-publish the ports of the container on the host, but what port?
- So with the Capital P option, it will expose all the ports configured in the Dockerfile (expose instruction) when the image is being built.
- Docker uses IPTables to map a port on a container to a port on the host and it uses Docker IPTables chains to modify or configure port mapping on a host.
Rename container
Use docker ps
command to get the current container name.
Run a new command in a running container
Attach the terminal's I/O to a running container
When you attach the terminal's I/O to a running container, you enter the command, it will display the result to all the users who attach back the container. As an example, if you exit the container, all people will leave the container at the same time.
Inspect container
Display a live stream of containers resource usage statistics
It will list containers with CPU, memory, network, and disk consumption.
Display running processes of a container
Display the processes and their process IDs on the Docker host.
Container Logs
Pause and Unpause container
Restart container
Update container
Stop, remove, and prune the container
SIGTERM -> SIGKILL -> Terminate container process
When executing the docker stop
command for Docker containers, Docker initiates the SIGTERM signal to the container initially. If the container does not stop within a grace period, Docker will then send the SIGKILL signal to forcibly terminate the process running within the container.