-d
Running in the background (detached)
-p 8080:80
Map port 8080 of your PC to port 80 of the container
--name [name]
a custom name is assigned
command to create and start a container:
docker run [image]
List active containers
(Use -a to also see those that are stopped):docker ps
Stop a container:
docker stop [id_or_name]
Start one that was already created:
docker start [id_or_name]
Delete a container (it must be stopped):
docker rm [id_or_name]
List the images you have downloaded:
docker images
Download a Docker Hub image:
docker pull [image]
Delete an image from your local disk:
docker rmi [image]
It shows what’s happening inside the container (the console):
docker logs [name]
It puts you “inside” the container to run commands in its terminal:
docker exec -it [name] bash
Docker Compose: Simple Orchestration
Docker Compose is used when you have multiple containers working together (for example, an app and a database)
Everything is defined in a docker-compose.yml file
Read the .yml file and start all services:
docker-compose up
They lift them up in the background:
-d
Stops and removes the containers, networks, and images defined in the file:
docker-compose down
Current project service status:
docker-compose ps
Track all services in real time:
docker-compose logs -f
Restart the services:
docker-compose restart
System Cleaning (The Housekeeping)
Docker tends to consume a lot of disk space if it is not cleaned regularly.
Remove stuck containers, unused nets, and hanging pictures:
docker system prune
Delete volumes that are not being used by any container (Be careful with your data!):
docker volume prune
It shows in real time how much CPU and RAM each container is using:
docker stats
If you want to delete all the stopped containers at once, you can use:
docker container prune
foreground and background
By default, Docker tends to stay in the foreground (linked to your terminal), but you can choose how you want it to behave depending on the situation
If you execute a command without additional parameters like:
docker run
or
docker-compose up
- The container takes over your terminal. You’ll see all output messages (logs) in real time
- You do not regain control of the terminal to type other commands
- Pressing Ctrl+C will usually stop the container (or the services in Compose)
with the option:
-d
Docker starts the container, gives you a long ID, and immediately releases the terminal so you can continue working
installation with interactive console
If you’re running a script that requires you to write data (an installer, for example), the foreground isn’t enough
You need the flags -i (interactive) and -t (TTY/simulated terminal)
docker run -it ubuntu
Without the -it, even if you see the foreground, the container will not “listen” to what you type on your keyboard