Docker for Dummies: Quick and Easy Guide
Linux Tips
Here are the basic commands to get you started with Docker, explained simply.
1. Install and Run MongoDB with Docker
docker run -d -p 27017:27017 --name mongo mongo
-d
: runs the container in the background (detached)-p 27017:27017
: maps port 27017 of the container to your computer--name mongo
: gives the container a namemongo
: uses the official MongoDB image (latest version)
Or if you want a specific version:
docker run -d -p 27017:27017 --name dbNoSQL mongo:4
2. Basic Docker Commands
- Run the test container:
docker run hello-world
- See running containers only:
docker ps
- See all containers (running and stopped):
docker ps -a
- Get detailed info about a container (use container ID or name):
docker inspect <ID_or_name>
3. Manage Containers
- Create a container with a custom name:
docker run --name hello-vass hello-world
- Rename a container:
docker rename hello-vass hola-pablo
- Delete a container:
docker rm <ID_or_name>
- Delete all stopped containers (cleanup):
docker container prune
4. Work with Ubuntu Container
- Run Ubuntu but don’t open its console:
docker run ubuntu
- List all containers to check their status:
docker ps -a
- Run Ubuntu and enter its interactive shell:
docker run -it ubuntu
What do -i
and -t
mean?
-i
: interactive mode (lets you type commands)-t
: opens a terminal session
Once inside, you can try commands like:
cat /etc/lsb-release
(to see the Linux version inside the container)
5. Keep a Container Running Without Doing Anything
To keep an Ubuntu container running all the time:
docker run --name alwaysup -d ubuntu tail -f /dev/null
tail -f /dev/null
: keeps the container running idle
To enter this always-running container:
docker exec -it alwaysup bash
6. Inspect Container Processes
To find the main process ID (PID) inside a container:
docker inspect --format '{{.State.Pid}}' alwaysup
Note: On Linux, you can kill this process with kill -9
, but this might not work on Mac.
7. Run ActiveMQ with Docker
docker run -p 61616:61616 -p 8161:8161 -d --name amq rmohr/activemq
This runs an ActiveMQ server and exposes the required ports.
Final Tips
- Always use custom names for your containers to manage them easier.
- Clean up stopped containers with
docker container prune
to save disk space. - To enter any container’s shell and work inside:
docker exec -it <name> bash
.
Want me to make a cheat sheet or examples for a specific project? Just ask!
How’s this?