Docker: Your pivotal tool in containerization. Master fundamental commands for efficiency at any level.
If you're navigating the dynamic landscape of containerization, Docker stands as a pivotal tool. Seamlessly replicating specific operating environments atop host OS, Docker empowers software packaging and deployment. Whether you're a seasoned developer or a newcomer, mastering Docker's fundamental commands is your gateway to enhanced efficiency and control.
Docker Essentials: images and containers
Docker, the bedrock of modern virtualisation, hinges on two fundamental components – images and containers.
Image lays the foundation for containers, providing the blueprint for their creation. Containers, on the other hand, breathe life into images, becoming the live instances that make software tangible.
Navigating Containers and Images
Elevate your container insights with must-know Docker commands.
Starting with the basics, such as retrieving the installed Docker version and summoning help for specific commands, you'll move into inspecting containers. Distinguish running containers from idle ones, and delve into Docker's rich palette of insightful columns – from IDs and images to ports and names.
Note: Anything enclosed within the square brackets is optional.
1. $ docker --version
This command returns the currently installed version of docker.
2. $ docker <command> --help
Gives information on a particular command.
Inspecting Docker Containers
1. $ docker ps
It only displays a list of the running containers.
2. $ docker ps -a
Lists all existing containers (running and not running).
3. $ docker ps -aq
Lists containers by their ID use –aq (quiet).
4. docker ps -s
Shows the total file size of each container, use -s (size).
The ps command gives several columns of information:
-
- Container ID – a unique alphanumeric number for each container
- Image – The base operating system image the container is based on
- Command – The command that launched the container
- Created – How long ago the container was created
- Status – Uptime or downtime
- Ports – Specifies any ports forwarded to the container for networking
- Name – A memorable name assigned by the Docker software
To manipulate the output format of certain commands and log drivers, Docker uses Go templates. A set of basic functions helps to manipulate template elements.
Example (output formatting):
$ docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"
Limits the output to the three fields
5. $ docker logs CONTAINER NAME
Displays logs of a container.
Example (logging):
$ docker container logs --tail 100 CONTAINER NAME
Prints the last 100 lines of a container’s logs
Inspecting Docker Images
1. $ docker image ls or $ docker images
This command shows a list all Docker Images.
Docker basic commands for managing images and containers.
Docker Container Interactions
Starting and Stopping Containers
1. $ docker start [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]
This command starts any stopped container(s) by specifying the first few unique characters of its container ID or by specifying its name.
2. $ docker restart [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]
Restarts any running container(s).
3. $ docker stop [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]
Stops any running container(s) by specifying the container ID or name.
4. $ docker stop $(docker ps -a -q)
Stops all running containers.
Attempts to stop the container gracefully by sending a SIGTERM signal to the container.
5. docker kill CONTAINER ID/NAME
Stops the container immediately by sending a SIGKILL signal.
Removing Containers
1. $ docker rm [options] CONTAINER ID/NAME [CONTAINER ID/NAME...]
Deletes a specific container. The containers need to be in a stopped state in order to be deleted.
2. $ docker rm $(docker ps -a -q)
Deletes all containers (only if stopped).
Interacting with Containers
1. $ docker attach [options] CONTAINER ID
Connects to a running container.
2. $ docker exec CONTAINER ID <command>
Executing commands inside a running container
Example (accessing the running container):
$ docker exec -it CONTAINER ID bash
Accesses the running container to run commands
3. $ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
- used to copy files from container to host
$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
- used to copy files from host to container
Copies files/folders between a container and the local filesystem
Docker Image Commands
Creating Containers
1. $ docker create [options] IMAGE NAME [commands] [arguments]
Creates a new container without starting it. Docker will return the container ID.
Example:
$ docker create -it ubuntu bash
The options -it instruct Docker to allocate a terminal to the container to allow the user to interact with the container. It also instructs Docker to execute the bash command whenever the container is started.
2. $ docker run [options] IMAGE NAME [commands] [arguments]
Creates a new container from an image and start it. This command is a combination of the docker create and the docker start command.
To start the container in interactive mode, we need to specify the options: -it. Than, the docker presents the terminal to interact with the container by typing in appropriate commands. To be eligible for running in interactive mode, the container has to be configured to start an interactive program on startup.
Executing docker run -it node or docker run -it python should land us directly on the node or python REPL program. In order to come out of the container, you need to type exit in the terminal.
The --rm option will clean up container after use.
Example (starting a container and allocating a terminal to it):
$ docker run -it ubuntu
In a case you do not define a name for newly created container, the demon will generate a random string name.
To define container name, use the --name option
Example (naming container):
$ docker run --name=UbuntuTest ubuntu:latest
To access a port inside a container, it is necessary to map that port to a port on the host system using the -p or --port option.
Generic syntax:
$ docker run -p <host port:container port> IMAGE NAME
Example (accessing a port):
$ docker run -p 80:80 nginx
Deleting images from the local image store
1. $ docker image rm IMAGE NAME
Deletes a specific image.
2. $ docker rmi [options] IMAGE NAME/ID [IMAGE NAME/ID...]
Removes an image(s) from the Docker Host.
3. $ docker image rm $(docker images -a -q)
Deletes all existing images.
4. $ docker system prune
Cleans up all Docker objects (images, containers, networks, build cache).
Docker will ask for confirmation. Use the -f or --force option to skip this confirmation step. The command will show the amount of reclaimed space at the end of its successful execution.
Creating Images
1. $ docker build -t myimage:latest .[path to docker file]
Builds an image from the Dockerfile in the current directory and tag the image
2. $ docker commit CONTAINER NAME NEW_IMAGE_NAME
Creates an image from a container
3. $ docker push myrepo/myimage:latest
Pushes an image to a registry
4. $ docker pull IMAGE NAME
Pulls an image from a registry.
Docker network-related commands
1. $ docker network ls
Lists networks.
2. $ docker network rm [NETWORK]
Removes one or more networks.
3. $ docker network inspect [NETWORK]
Shows information on one or more networks.
4. $ docker network connect [NETWORK] [CONTAINER]
Connects a container to a network.
5. $ docker network disconnect [NETWORK] [CONTAINER]
Disconnects a container from a network.