Getting Started with Docker: Essential Commands for Beginners
Docker has revolutionized the world of software development by making containerization accessible to developers of all backgrounds. Whether you're a seasoned pro or just starting your journey into the world of containers, mastering essential Docker commands is a must.
In this comprehensive guide, we will walk you through the fundamental Docker commands that every newcomer should know. We'll provide clear explanations and practical code examples to help you get started with Docker and build a solid foundation for containerized applications.
Docker Basics
Before we dive into the commands, let's cover some Docker basics. Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and isolated environments that package applications and their dependencies. Docker simplifies the process of creating, deploying, and managing these containers.
Use Docker:
+ Pulling Docker Images
docker pull image_name
Explanation:
- The
docker pull
command is used to download Docker images from a registry (like Docker Hub) to your local machine. - Replace
image_name
with the name of the Docker image you want to pull.
Exampledocker pull ubuntu:latest
+ Build docker:
docker-compose build
+ Build docker no cache:
docker-compose --no-cache
+ View docker container: The docker ps
command lists all the running containers on your system.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1b7b4dd455a ubuntu:latest "/bin/bash" 5 minutes ago Up 5 minutes modest_joliot
+ View docker image:
docker images
+ Remove a container:
docker rm container_id
Explanation:
- The
docker rm
the command deletes a stopped container. - Replace
container_id
with the ID or name of the container you want to remove.
Example:docker rm c1b7b4dd455a
+ Remove all containers:
docker rm $(docker ps -a -q)
+ Remove all images:
docker rmi $(docker images -a -q)
+ Stop all docker containers:
docker stop $(docker ps -a -q)
+ Start all docker container:
docker start $(docker ps -a -q)
+ Run all docker in docker-compose:
docker-compose up
docker-compose up -d
+ Down all docker in docker-compose:
docker-compose down
+ Run docker by docker name cmd:
docker exec -it container_name bash
+ Remove all networks between containers
docker network prune
+ Other exec docker mysql by docker name
- docker exec -it {docker_name} bash
- mysql -u root -p
Note: If version mysql > 8: Add permission MySQL:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
+ Import file SQL:
sudo docker-compose exec -T {name_mysql} mysql -u{username} -p{password} {database_name} --binary-mode < {path}/file_name.sql
ERROR 1227 (42000) at line 18: Access denied; you need (at least one of) the SUPER privilege(s) for this operation
Find and Comment:
-- SET @@SESSION.SQL_LOG_BIN= 0;
-- SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ '';
-- SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
Note: ERROR: Pool overlaps with other one on this address space
sudo docker network rm $(sudo docker network ls -q)
Conclusion
Congratulations! You've taken your first steps into the world of Docker with these essential commands. Docker's flexibility and efficiency make it a powerful tool for modern software development and deployment.
As you continue your Docker journey, you'll discover more commands and features that will enable you to build, ship, and run containerized applications seamlessly. Stay curious, explore Docker's ecosystem, and enjoy the benefits of containerization in your development projects.