Docker Container
When a Docker image is run, it creates a Docker container, which is a running instance of the image. Multiple containers can be created from the same image, each with its own isolated environment and resources, making it easy to run multiple instances of an application on the same machine.
A container is an isolated place where an application runs without affecting the rest of the system and without the system impacting the application. Therefore containers are well-suited for securely running software like databases or web applications that need access to sensitive resources without giving access to every user on the system.
State
Docker containers have six states which represent a piece of valuable information about the container:
- Created
- Running
- Restarting
- Paused
- Exited
- Dead
Create
Docker assign the Create stater for containers when it is just created and never started
Hence, no CPU or memory is used by the containers in this state.
So it will be after creating a container from the docker image by running
docker create
docker container ls -a
the first command will create a container, and the second will list the container with the state Created.
Running
You can run a container that is already created or exited by running
docker start
And you can join an already running container by the attach command
docker attach
Restarting
This is a state for the container when it is restarting, you can achieve this state when you restart a container by
docker restart
Paused
You can pause an already-running container
Hence, The paused container reserves the same memory but it is not consuming CPU
docker pause
Exited
This state can occur in two cases:
-
if the container has been stopped by the docker
docker stop
- There is an exception happened in the container or the process execution inside the container has been done.
Dead
The dead state of a Docker container means that the container is non-functioning. This state is achieved when we try to remove the container, but it cannot be removed because some resources are still in use by an external process. Hence, the container is moved to the dead state.
Containers in a dead state cannot be restarted. and they do not consume any memory or CPU.
Generate Container
To generate a container to work with Docker has pre-steps to do so:
- You have to have an Image
- Run the image to have a container from it (you can consider it as an instance of the image)
We will assume here that you already have an Image generated container from it, and we will discuss the image creation in the next topic, but for now, assume that we have a docker image.
List image
so we can list the docker images by:
docker image ls
Create a container
then create a container from the image
docker create
List containers
After this command, you will have a new container, and you can list the containers by
docker container ls -a
this command will list all containers (the running and the stopped containers), and list only the running containers
docker container ls
Start container
To start an ideal container
docker run
To join to a running container
docker attach
To get stdin, stdout and tty with container
docker run -i -t
To share folder between the host and the container
docker run -v :
To install or run command in container
docker exec -it [container] pip3 install pandas
this will install pandas for python3 for example.
Stop container
To stop already running container
docker stop
Now you have enough information to understand the docker container, we will cover more container commands and specs in the next topics, so see you around
Delete containers
To delete container
docker rm
To delete all containers
docker rm $(docker ps -a -q)
Containers inspect
Get information about the container
docker inspect
To get the container IP for example
docker inspect | grep "IPAddress"
Containers exec
To run command in running container
docker exec
To access the docker container
docker exec -it /bin/bash
Containers Rename
To rename container
docker rename
Create Image from Container
To make an image from existed container
docker commit
Add volume to container
To make a shared volume between the host and docker image
docker run -dit --name -v :
Git logs of the container
To get the latest logs of the running container
docker logs
You can limit the logs for specific lines by -n
and the number of lines, or --tail
docker logs -n 10
You can keep track the logs by -f, --follow
docker logs -n 10 -f