Docker: A Comprehensive Guide to Essential Commands
Navigating the extensive range of Docker commands can pose a challenge for both newcomers and seasoned professionals. This guide provides a comprehensive overview of essential Docker commands, to assist you to confidently manage and orchestrate containers within your development workflow. Let’s get into it.
Get information about the docker setup
sudo docker info
Login into your docker account:
sudo docker login
Search For Docker Images:
sudo docker search centos
To pull the Ubuntu image:
sudo docker pull ubuntu
Run Container & Enter The Shell
sudo docker run -i -t ubuntu /bin/bash
Starting a Stopped Container:
sudo docker start CONTAINER_NAME
To stop a container:
sudo docker stop container_id
To restart a container:
sudo docker restart container_id
To connect to a running container:
sudo docker attach container_id
To show running containers. With the -a
option, it shows running and stopped containers:
sudo docker ps
To show container information like IP address:
sudo docker inspect container_id
To copy a file from a container to the host:
sudo docker cp containerId:/file/path/within/container /host/path/target
Copying files from host to docker container:
sudo docker cp mediumblog.txt mycontainer:/mediumblog.txt
sudo docker cp mycontainer:/mediumblog.txt mediumblog.txt
How to mount a host directory in a docker container:
sudo docker run -v /host/directory:/container/directory image_name command_to_run
sudo docker run -d -it -v /usr/jeremiah/main_folder:/test_container ubuntu /bin/bash
To delete a container:
sudo docker rm container_id
While the essential Docker commands provide a solid foundation for container management, true mastery lies in understanding and utilizing advanced techniques. This section as a quick introduction that delves into the realm of networking, equipping you enough to continue your journey and find out more from Dockers documentation on networking which you you can visit https://docs.docker.com/network/ for more commands and features.
Create a Bridge:
sudo docker network create -d bridge demo-bridge
sudo docker run --network=demo-bridge -itd --name=container3 busybox
Listing Docker Networks:
sudo docker network ls
Running a Container with Network Mode:
sudo docker run -h container_id -i -t --net="demo-bridge" ubuntu /bin/bash
Bind Host Port to container Port
sudo docker run -d --name test example/test --bind 127.0.0.1
Inspect a Docker Network
sudo docker network inspect demo-bridge
Further Exploration and Advanced Usage:
These core commands equips you to manage fundamental container operations. However, Docker offers a rich ecosystem of functionalities to explore. Delve into image creation with docker build, network configuration with docker network, and persistent volume management with docker volume to utilize the full potential of containerization.
Hopefully these commands acted as a refresher and enough for you to discover more on what Docker truly is capable of and enable you to leverage containerization for efficient and scalable application deployment.
Until next time,
Jeremiah