Docker Containers
PUBLISHED ON: Tuesday, Aug 2, 2022
#
Images vs Containers
Images | Containers |
---|---|
An Image is the application we want to run | A container is an instance of that image running as a process |
There can be many containers running off the same image |
Docker's default image registry is called Docker Hub
#
Docker container commands
Start a new container
👉 To start a new container from an image, use docker container run
.
Let's have a look at how to start a container from an image. Here we run an nginx
container as follows:
# 80:80 = <host-IP>:<container-IP>
# port 80 on host IP routes traffic to container IP on port 80
docker container run --publish 80:80 nginx
This is what the above command does:
- Looks for an
nginx
image locally in image cache - Did it find the image?
- No?
- looks for the same in remote image repository (defaults to Docker Hub)
- downloads the latest version (
nginx:latest
) - stores it in the image cache and continues.
- Yes?
- Continues to next step
- Creates a new container based on that image and starts
- Assigns it a virtual IP on a private network inside the docker engine
- Opens port 80 on host IP and routes traffic to container IP on port 80
❗ This runs the process in foreground.
🛑 To send a stop signal to a process running in the foreground, press Ctrl + C.
👉 To run the same command in background, run the above command with the detach
option as follows:
# 80:80 = <host-IP>:<container-IP>
# port 80 on host IP routes traffic to container IP on port 80
docker container run --publish 80:80 --detach nginx
This will return a unique container ID.
Use a specific version of the image
docker container run --publish 80:80 --d nginx:1.11
💡 d
is short for detach
Start an existing container
# docker container start e6a
docker container start <first-few-digits-of-container-id>
List all running containers
docker container ls
# Alternate command
docker ps
This shows all running containers in the following format:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6ac9bd67074 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp heuristic_joliot
Stop a running container
# docker container stop e6a
docker container stop <first-few-digits-of-container-id>
Name a container
To name a container, use name
option as follows:
docker container run --publish 80:80 --detach --name webhost nginx
Get logs for a specific container
# docker container logs webhost
# docker container logs e6a
docker container logs <first-few-digits-of-container-id | container-name>
Remove container(s)
docker container rm d34 e6a fb0 9c1
❗ Be sure to stop running containers before trying to remove them.
💡 There is always the option to force remove running containers with f
option.
docker container rm -f d34 e6a fb0 9c1
View container processes
To list running processes in a specific container:
docker container top <first-few-digits-of-container-id | container-name>
Inspect containers
To see metadata about the container (startup, config, volumes, networking, etc.)
docker container inspect <first-few-digits-of-container-id | container-name>
View container usage statistics
Display a live stream of container(s) resource usage statistics
docker container stats <first-few-digits-of-container-id | container-name>
Running commands on a container
# docker container run -it --name ubuntu ubuntu
# docker container run -it --name proxy mariadb bash
# -t simulates a real terminal, like what SSH does
# -i Keeps session open so that we can run more commands
docker container run -it <first-few-digits-of-container-id | container-name> <command>
This will open a shell inside the container as follows:
# root means root on the container
# the number after `root@` is the container ID
root@3eeb605c1258:/#
👉 To start an existing container interactively:
# -a attach session and forward signals to the container
docker container start -ai <first-few-digits-of-container-id | container-name>
👉 To run additional processes in a running container without affecting the running container:
# docker container exec -it mariadb bash
docker container exec -it <first-few-digits-of-container-id | container-name> <command>
💡 Exiting the command in the above case won't stop the container.
#
Containers vs Virtual machines
A container is just a process running on top of an image, i.e., they have access to limited resources and a process can be stopped.
A container is ❌ not exactly a virtual machine.
💡 To monitor all processes running on your system, use ps aux
.
💡 To view all processes matching a string, use ps aux | grep <search-string>
.